Need help with nested dropdowns

Need help with nested dropdowns
0.0 0


Hi there,

I have been trying to create a directory tree structure lookalike on the front end. but I do not know why it does not seem to work. Suppose I extract my folder details/path from :

This is what I get once I get the path of files and folders(blob and trees).

Images/Tut img 1.PNG
Images/Tut img 2.PNG
Images/Tut img 3.PNG
Images/Tut img 4.PNG
Images/Tut img 5.PNG

I am trying to make it look like it has folders as dropdowns or lookalike, and click on each folder slides down to show its contents. This is what I wrote

    cache: false,
    dataType: "json",
    success: function(data) {
      for (let i = 0; i < data.tree.length; i++) {
          if (data.tree[i].type == "tree")
              if (!data.tree[i].path.includes("/")) {
                  //$("#localRepo").append("<table class='table' id='" + data.tree[i].path + "'><thead><tr><th>" + data.tree[i].path + "</th></tr></thead ></table>");
                  $("#localRepo").append("<div class='dropdown'><button class='btn btn-default dropdown-toggle' type ='button' id ='"
                      + data.tree[i].path + "' data-toggle='dropdown'> "
                      + data.tree[i].path + "<span class='caret'>"
                      + "</span ></button > <ul class='dropdown-menu dropdown-menu-right' role='menu' id='" + data.tree[i].path+"_menu' aria-labelledby="
                      + data.tree[i].path + "></ul>");
              else { 
                  var elementSplitList = data.tree[i].path.split("/");
                  var parent = elementSplitList[elementSplitList.length - 2];
                  var child = elementSplitList[elementSplitList.length - 1];
                  $("#" + parent + "_menu").append("<li class='dropdown-submenu'>"+
                      "<a class='' tabindex='-1' href='#'>"+ child +" <span class='caret'></span></a>"+
                      "<ul id='" + child + "_menu' class='dropdown-menu'></ul></li>");
        else { 
              var elementSplitList = data.tree[i].path.split("/");
              var parent = elementSplitList[elementSplitList.length - 2];
              var blob = elementSplitList[elementSplitList.length - 1];
              $("#" + parent + "_menu").append("<li role='presentation'><a role='menuitem' tabindex='-1' href='#'>" + blob + "</a></li>");
    error: function(e, xhr) {
      console.log("Oops! Error");

But any nested dropdown on clicking, closes its parent as well. Hence I am unable to view the folder contents after a particular level. I would appreciate if anyone could help.


You’ve got data-toggle targeting the class dropdown. Everything has that class, so everything gets targeted. You can modify the class and target by concatenating the loop’s index. Something like:

$("element").append("<div class='dropdown" + i + "'>...")