The combo box drop down tree with javascript?











up vote
0
down vote

favorite












After I wrote it, I found that the order of the drop-down boxes is reversed. This is not the right thing to do. How to use the method to make the order of the drop-down boxes is correct.



But It must be done according to the effect.



 one
├ two
│ ├ five
│ └ six
├ three
└ four
│ └ seven
one-2
├ two-2
│ └ five-2
│ │ └ four-2
│ │ │ └ four-3
├ five-3
│ ├ four-3
│ ├ four-4
│ └ four-5
└ six-2


Look at the picture: enter image description here






var data = [{
"id": 1,
"name": "one",
"pid": 0,
"level": 0,
"children": [{
"id": 2,
"name": "two",
"pid": 1,
"level": 1,
"children": [{
"id": 3,
"name": "five",
"pid": 2,
"level": 2,
"children":
},
{
"id": 4,
"name": "six",
"pid": 2,
"level": 2,
"children":
}
]
},
{
"id": 5,
"name": "three",
"pid": 1,
"level": 1,
"children":
},
{
"id": 6,
"name": "four",
"pid": 1,
"level": 1,
"children": [{
"id": 7,
"name": "seven",
"pid": 6,
"level": 2,
"children":
}]
}
]
},
{
"id": 8,
"name": "one-2",
"pid": 0,
"level": 0,
"children": [{
"id": 9,
"name": "two-2",
"pid": 8,
"level": 1,
"children": [{
"id": 10,
"name": "five-2",
"pid": 9,
"level": 2,
"children": [{
"id": 11,
"name": "four-2",
"pid": 10,
"level": 3,
"children": [{
"id": 12,
"name": "four-3",
"pid": 11,
"level": 4,
"children":
}]
}]
}]
},
{
"id": 13,
"name": "five-3",
"pid": 8,
"level": 1,
"children": [{
"id": 14,
"name": "four-3",
"pid": 13,
"level": 2,
"children":
},
{
"id": 15,
"name": "four-4",
"pid": 13,
"level": 2,
"children":
},
{
"id": 16,
"name": "four-5",
"pid": 13,
"level": 2,
"children":
}
]
},
{
"id": 17,
"name": "six-2",
"pid": 8,
"level": 1,
"children":
}
]

}
]

var icon = new Array('', '├ ', '└ ', '│ ');

//Processing tree hierarchy data
function getDeep(data) {
(function rec(data, depth) {
var str = "";
var _prefix = (new Array(depth)).join(icon[3]);
for (i in data) {
if (data[i].name || '') {
if (depth === 0) {
str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
console.log(data[i].name);
} else {
if (i < data.length - 1) {
str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
"</option>";
console.log(_prefix + icon[1] + data[i].name);
} else {

str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
"</option>";
console.log(_prefix + icon[2] + data[i].name);
}

}

}
if (data[i].hasOwnProperty('children') && data[i].children.length) {
rec(data[i].children, depth + 1);
}
}
$("#selectbox").append(str);
})(data, 0);
}

$(function() {
getDeep(data);
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selectbox"></select>












share|improve this question




























    up vote
    0
    down vote

    favorite












    After I wrote it, I found that the order of the drop-down boxes is reversed. This is not the right thing to do. How to use the method to make the order of the drop-down boxes is correct.



    But It must be done according to the effect.



     one
    ├ two
    │ ├ five
    │ └ six
    ├ three
    └ four
    │ └ seven
    one-2
    ├ two-2
    │ └ five-2
    │ │ └ four-2
    │ │ │ └ four-3
    ├ five-3
    │ ├ four-3
    │ ├ four-4
    │ └ four-5
    └ six-2


    Look at the picture: enter image description here






    var data = [{
    "id": 1,
    "name": "one",
    "pid": 0,
    "level": 0,
    "children": [{
    "id": 2,
    "name": "two",
    "pid": 1,
    "level": 1,
    "children": [{
    "id": 3,
    "name": "five",
    "pid": 2,
    "level": 2,
    "children":
    },
    {
    "id": 4,
    "name": "six",
    "pid": 2,
    "level": 2,
    "children":
    }
    ]
    },
    {
    "id": 5,
    "name": "three",
    "pid": 1,
    "level": 1,
    "children":
    },
    {
    "id": 6,
    "name": "four",
    "pid": 1,
    "level": 1,
    "children": [{
    "id": 7,
    "name": "seven",
    "pid": 6,
    "level": 2,
    "children":
    }]
    }
    ]
    },
    {
    "id": 8,
    "name": "one-2",
    "pid": 0,
    "level": 0,
    "children": [{
    "id": 9,
    "name": "two-2",
    "pid": 8,
    "level": 1,
    "children": [{
    "id": 10,
    "name": "five-2",
    "pid": 9,
    "level": 2,
    "children": [{
    "id": 11,
    "name": "four-2",
    "pid": 10,
    "level": 3,
    "children": [{
    "id": 12,
    "name": "four-3",
    "pid": 11,
    "level": 4,
    "children":
    }]
    }]
    }]
    },
    {
    "id": 13,
    "name": "five-3",
    "pid": 8,
    "level": 1,
    "children": [{
    "id": 14,
    "name": "four-3",
    "pid": 13,
    "level": 2,
    "children":
    },
    {
    "id": 15,
    "name": "four-4",
    "pid": 13,
    "level": 2,
    "children":
    },
    {
    "id": 16,
    "name": "four-5",
    "pid": 13,
    "level": 2,
    "children":
    }
    ]
    },
    {
    "id": 17,
    "name": "six-2",
    "pid": 8,
    "level": 1,
    "children":
    }
    ]

    }
    ]

    var icon = new Array('', '├ ', '└ ', '│ ');

    //Processing tree hierarchy data
    function getDeep(data) {
    (function rec(data, depth) {
    var str = "";
    var _prefix = (new Array(depth)).join(icon[3]);
    for (i in data) {
    if (data[i].name || '') {
    if (depth === 0) {
    str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
    console.log(data[i].name);
    } else {
    if (i < data.length - 1) {
    str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
    "</option>";
    console.log(_prefix + icon[1] + data[i].name);
    } else {

    str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
    "</option>";
    console.log(_prefix + icon[2] + data[i].name);
    }

    }

    }
    if (data[i].hasOwnProperty('children') && data[i].children.length) {
    rec(data[i].children, depth + 1);
    }
    }
    $("#selectbox").append(str);
    })(data, 0);
    }

    $(function() {
    getDeep(data);
    })

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select id="selectbox"></select>












    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      After I wrote it, I found that the order of the drop-down boxes is reversed. This is not the right thing to do. How to use the method to make the order of the drop-down boxes is correct.



      But It must be done according to the effect.



       one
      ├ two
      │ ├ five
      │ └ six
      ├ three
      └ four
      │ └ seven
      one-2
      ├ two-2
      │ └ five-2
      │ │ └ four-2
      │ │ │ └ four-3
      ├ five-3
      │ ├ four-3
      │ ├ four-4
      │ └ four-5
      └ six-2


      Look at the picture: enter image description here






      var data = [{
      "id": 1,
      "name": "one",
      "pid": 0,
      "level": 0,
      "children": [{
      "id": 2,
      "name": "two",
      "pid": 1,
      "level": 1,
      "children": [{
      "id": 3,
      "name": "five",
      "pid": 2,
      "level": 2,
      "children":
      },
      {
      "id": 4,
      "name": "six",
      "pid": 2,
      "level": 2,
      "children":
      }
      ]
      },
      {
      "id": 5,
      "name": "three",
      "pid": 1,
      "level": 1,
      "children":
      },
      {
      "id": 6,
      "name": "four",
      "pid": 1,
      "level": 1,
      "children": [{
      "id": 7,
      "name": "seven",
      "pid": 6,
      "level": 2,
      "children":
      }]
      }
      ]
      },
      {
      "id": 8,
      "name": "one-2",
      "pid": 0,
      "level": 0,
      "children": [{
      "id": 9,
      "name": "two-2",
      "pid": 8,
      "level": 1,
      "children": [{
      "id": 10,
      "name": "five-2",
      "pid": 9,
      "level": 2,
      "children": [{
      "id": 11,
      "name": "four-2",
      "pid": 10,
      "level": 3,
      "children": [{
      "id": 12,
      "name": "four-3",
      "pid": 11,
      "level": 4,
      "children":
      }]
      }]
      }]
      },
      {
      "id": 13,
      "name": "five-3",
      "pid": 8,
      "level": 1,
      "children": [{
      "id": 14,
      "name": "four-3",
      "pid": 13,
      "level": 2,
      "children":
      },
      {
      "id": 15,
      "name": "four-4",
      "pid": 13,
      "level": 2,
      "children":
      },
      {
      "id": 16,
      "name": "four-5",
      "pid": 13,
      "level": 2,
      "children":
      }
      ]
      },
      {
      "id": 17,
      "name": "six-2",
      "pid": 8,
      "level": 1,
      "children":
      }
      ]

      }
      ]

      var icon = new Array('', '├ ', '└ ', '│ ');

      //Processing tree hierarchy data
      function getDeep(data) {
      (function rec(data, depth) {
      var str = "";
      var _prefix = (new Array(depth)).join(icon[3]);
      for (i in data) {
      if (data[i].name || '') {
      if (depth === 0) {
      str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
      console.log(data[i].name);
      } else {
      if (i < data.length - 1) {
      str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
      "</option>";
      console.log(_prefix + icon[1] + data[i].name);
      } else {

      str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
      "</option>";
      console.log(_prefix + icon[2] + data[i].name);
      }

      }

      }
      if (data[i].hasOwnProperty('children') && data[i].children.length) {
      rec(data[i].children, depth + 1);
      }
      }
      $("#selectbox").append(str);
      })(data, 0);
      }

      $(function() {
      getDeep(data);
      })

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <select id="selectbox"></select>












      share|improve this question















      After I wrote it, I found that the order of the drop-down boxes is reversed. This is not the right thing to do. How to use the method to make the order of the drop-down boxes is correct.



      But It must be done according to the effect.



       one
      ├ two
      │ ├ five
      │ └ six
      ├ three
      └ four
      │ └ seven
      one-2
      ├ two-2
      │ └ five-2
      │ │ └ four-2
      │ │ │ └ four-3
      ├ five-3
      │ ├ four-3
      │ ├ four-4
      │ └ four-5
      └ six-2


      Look at the picture: enter image description here






      var data = [{
      "id": 1,
      "name": "one",
      "pid": 0,
      "level": 0,
      "children": [{
      "id": 2,
      "name": "two",
      "pid": 1,
      "level": 1,
      "children": [{
      "id": 3,
      "name": "five",
      "pid": 2,
      "level": 2,
      "children":
      },
      {
      "id": 4,
      "name": "six",
      "pid": 2,
      "level": 2,
      "children":
      }
      ]
      },
      {
      "id": 5,
      "name": "three",
      "pid": 1,
      "level": 1,
      "children":
      },
      {
      "id": 6,
      "name": "four",
      "pid": 1,
      "level": 1,
      "children": [{
      "id": 7,
      "name": "seven",
      "pid": 6,
      "level": 2,
      "children":
      }]
      }
      ]
      },
      {
      "id": 8,
      "name": "one-2",
      "pid": 0,
      "level": 0,
      "children": [{
      "id": 9,
      "name": "two-2",
      "pid": 8,
      "level": 1,
      "children": [{
      "id": 10,
      "name": "five-2",
      "pid": 9,
      "level": 2,
      "children": [{
      "id": 11,
      "name": "four-2",
      "pid": 10,
      "level": 3,
      "children": [{
      "id": 12,
      "name": "four-3",
      "pid": 11,
      "level": 4,
      "children":
      }]
      }]
      }]
      },
      {
      "id": 13,
      "name": "five-3",
      "pid": 8,
      "level": 1,
      "children": [{
      "id": 14,
      "name": "four-3",
      "pid": 13,
      "level": 2,
      "children":
      },
      {
      "id": 15,
      "name": "four-4",
      "pid": 13,
      "level": 2,
      "children":
      },
      {
      "id": 16,
      "name": "four-5",
      "pid": 13,
      "level": 2,
      "children":
      }
      ]
      },
      {
      "id": 17,
      "name": "six-2",
      "pid": 8,
      "level": 1,
      "children":
      }
      ]

      }
      ]

      var icon = new Array('', '├ ', '└ ', '│ ');

      //Processing tree hierarchy data
      function getDeep(data) {
      (function rec(data, depth) {
      var str = "";
      var _prefix = (new Array(depth)).join(icon[3]);
      for (i in data) {
      if (data[i].name || '') {
      if (depth === 0) {
      str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
      console.log(data[i].name);
      } else {
      if (i < data.length - 1) {
      str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
      "</option>";
      console.log(_prefix + icon[1] + data[i].name);
      } else {

      str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
      "</option>";
      console.log(_prefix + icon[2] + data[i].name);
      }

      }

      }
      if (data[i].hasOwnProperty('children') && data[i].children.length) {
      rec(data[i].children, depth + 1);
      }
      }
      $("#selectbox").append(str);
      })(data, 0);
      }

      $(function() {
      getDeep(data);
      })

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <select id="selectbox"></select>








      var data = [{
      "id": 1,
      "name": "one",
      "pid": 0,
      "level": 0,
      "children": [{
      "id": 2,
      "name": "two",
      "pid": 1,
      "level": 1,
      "children": [{
      "id": 3,
      "name": "five",
      "pid": 2,
      "level": 2,
      "children":
      },
      {
      "id": 4,
      "name": "six",
      "pid": 2,
      "level": 2,
      "children":
      }
      ]
      },
      {
      "id": 5,
      "name": "three",
      "pid": 1,
      "level": 1,
      "children":
      },
      {
      "id": 6,
      "name": "four",
      "pid": 1,
      "level": 1,
      "children": [{
      "id": 7,
      "name": "seven",
      "pid": 6,
      "level": 2,
      "children":
      }]
      }
      ]
      },
      {
      "id": 8,
      "name": "one-2",
      "pid": 0,
      "level": 0,
      "children": [{
      "id": 9,
      "name": "two-2",
      "pid": 8,
      "level": 1,
      "children": [{
      "id": 10,
      "name": "five-2",
      "pid": 9,
      "level": 2,
      "children": [{
      "id": 11,
      "name": "four-2",
      "pid": 10,
      "level": 3,
      "children": [{
      "id": 12,
      "name": "four-3",
      "pid": 11,
      "level": 4,
      "children":
      }]
      }]
      }]
      },
      {
      "id": 13,
      "name": "five-3",
      "pid": 8,
      "level": 1,
      "children": [{
      "id": 14,
      "name": "four-3",
      "pid": 13,
      "level": 2,
      "children":
      },
      {
      "id": 15,
      "name": "four-4",
      "pid": 13,
      "level": 2,
      "children":
      },
      {
      "id": 16,
      "name": "four-5",
      "pid": 13,
      "level": 2,
      "children":
      }
      ]
      },
      {
      "id": 17,
      "name": "six-2",
      "pid": 8,
      "level": 1,
      "children":
      }
      ]

      }
      ]

      var icon = new Array('', '├ ', '└ ', '│ ');

      //Processing tree hierarchy data
      function getDeep(data) {
      (function rec(data, depth) {
      var str = "";
      var _prefix = (new Array(depth)).join(icon[3]);
      for (i in data) {
      if (data[i].name || '') {
      if (depth === 0) {
      str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
      console.log(data[i].name);
      } else {
      if (i < data.length - 1) {
      str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
      "</option>";
      console.log(_prefix + icon[1] + data[i].name);
      } else {

      str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
      "</option>";
      console.log(_prefix + icon[2] + data[i].name);
      }

      }

      }
      if (data[i].hasOwnProperty('children') && data[i].children.length) {
      rec(data[i].children, depth + 1);
      }
      }
      $("#selectbox").append(str);
      })(data, 0);
      }

      $(function() {
      getDeep(data);
      })

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <select id="selectbox"></select>





      var data = [{
      "id": 1,
      "name": "one",
      "pid": 0,
      "level": 0,
      "children": [{
      "id": 2,
      "name": "two",
      "pid": 1,
      "level": 1,
      "children": [{
      "id": 3,
      "name": "five",
      "pid": 2,
      "level": 2,
      "children":
      },
      {
      "id": 4,
      "name": "six",
      "pid": 2,
      "level": 2,
      "children":
      }
      ]
      },
      {
      "id": 5,
      "name": "three",
      "pid": 1,
      "level": 1,
      "children":
      },
      {
      "id": 6,
      "name": "four",
      "pid": 1,
      "level": 1,
      "children": [{
      "id": 7,
      "name": "seven",
      "pid": 6,
      "level": 2,
      "children":
      }]
      }
      ]
      },
      {
      "id": 8,
      "name": "one-2",
      "pid": 0,
      "level": 0,
      "children": [{
      "id": 9,
      "name": "two-2",
      "pid": 8,
      "level": 1,
      "children": [{
      "id": 10,
      "name": "five-2",
      "pid": 9,
      "level": 2,
      "children": [{
      "id": 11,
      "name": "four-2",
      "pid": 10,
      "level": 3,
      "children": [{
      "id": 12,
      "name": "four-3",
      "pid": 11,
      "level": 4,
      "children":
      }]
      }]
      }]
      },
      {
      "id": 13,
      "name": "five-3",
      "pid": 8,
      "level": 1,
      "children": [{
      "id": 14,
      "name": "four-3",
      "pid": 13,
      "level": 2,
      "children":
      },
      {
      "id": 15,
      "name": "four-4",
      "pid": 13,
      "level": 2,
      "children":
      },
      {
      "id": 16,
      "name": "four-5",
      "pid": 13,
      "level": 2,
      "children":
      }
      ]
      },
      {
      "id": 17,
      "name": "six-2",
      "pid": 8,
      "level": 1,
      "children":
      }
      ]

      }
      ]

      var icon = new Array('', '├ ', '└ ', '│ ');

      //Processing tree hierarchy data
      function getDeep(data) {
      (function rec(data, depth) {
      var str = "";
      var _prefix = (new Array(depth)).join(icon[3]);
      for (i in data) {
      if (data[i].name || '') {
      if (depth === 0) {
      str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
      console.log(data[i].name);
      } else {
      if (i < data.length - 1) {
      str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
      "</option>";
      console.log(_prefix + icon[1] + data[i].name);
      } else {

      str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
      "</option>";
      console.log(_prefix + icon[2] + data[i].name);
      }

      }

      }
      if (data[i].hasOwnProperty('children') && data[i].children.length) {
      rec(data[i].children, depth + 1);
      }
      }
      $("#selectbox").append(str);
      })(data, 0);
      }

      $(function() {
      getDeep(data);
      })

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <select id="selectbox"></select>






      javascript jquery html






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 11:44









      Rory McCrossan

      240k29203244




      240k29203244










      asked Nov 20 at 11:43









      wen tian

      987




      987
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Just move the str var outside your rec function






          var data = [{
          "id": 1,
          "name": "one",
          "pid": 0,
          "level": 0,
          "children": [{
          "id": 2,
          "name": "two",
          "pid": 1,
          "level": 1,
          "children": [{
          "id": 3,
          "name": "five",
          "pid": 2,
          "level": 2,
          "children":
          },
          {
          "id": 4,
          "name": "six",
          "pid": 2,
          "level": 2,
          "children":
          }
          ]
          },
          {
          "id": 5,
          "name": "three",
          "pid": 1,
          "level": 1,
          "children":
          },
          {
          "id": 6,
          "name": "four",
          "pid": 1,
          "level": 1,
          "children": [{
          "id": 7,
          "name": "seven",
          "pid": 6,
          "level": 2,
          "children":
          }]
          }
          ]
          },
          {
          "id": 8,
          "name": "one-2",
          "pid": 0,
          "level": 0,
          "children": [{
          "id": 9,
          "name": "two-2",
          "pid": 8,
          "level": 1,
          "children": [{
          "id": 10,
          "name": "five-2",
          "pid": 9,
          "level": 2,
          "children": [{
          "id": 11,
          "name": "four-2",
          "pid": 10,
          "level": 3,
          "children": [{
          "id": 12,
          "name": "four-3",
          "pid": 11,
          "level": 4,
          "children":
          }]
          }]
          }]
          },
          {
          "id": 13,
          "name": "five-3",
          "pid": 8,
          "level": 1,
          "children": [{
          "id": 14,
          "name": "four-3",
          "pid": 13,
          "level": 2,
          "children":
          },
          {
          "id": 15,
          "name": "four-4",
          "pid": 13,
          "level": 2,
          "children":
          },
          {
          "id": 16,
          "name": "four-5",
          "pid": 13,
          "level": 2,
          "children":
          }
          ]
          },
          {
          "id": 17,
          "name": "six-2",
          "pid": 8,
          "level": 1,
          "children":
          }
          ]

          }
          ]

          var icon = new Array('', '├ ', '└ ', '│ ');

          //Processing tree hierarchy data
          function getDeep(data) {
          var str = "";
          (function rec(data, depth) {
          var _prefix = (new Array(depth)).join(icon[3]);
          for (i in data) {
          if (data[i].name || '') {
          if (depth === 0) {
          str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
          console.log(data[i].name);
          } else {
          if (i < data.length - 1) {
          str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
          "</option>";
          console.log(_prefix + icon[1] + data[i].name);
          } else {

          str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
          "</option>";
          console.log(_prefix + icon[2] + data[i].name);
          }

          }

          }
          if (data[i].hasOwnProperty('children') && data[i].children.length) {
          rec(data[i].children, depth + 1);
          }
          }
          })(data, 0);
          $("#selectbox").append(str);
          }

          $(function() {
          getDeep(data);
          })

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <select id="selectbox"></select>








          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392274%2fthe-combo-box-drop-down-tree-with-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            Just move the str var outside your rec function






            var data = [{
            "id": 1,
            "name": "one",
            "pid": 0,
            "level": 0,
            "children": [{
            "id": 2,
            "name": "two",
            "pid": 1,
            "level": 1,
            "children": [{
            "id": 3,
            "name": "five",
            "pid": 2,
            "level": 2,
            "children":
            },
            {
            "id": 4,
            "name": "six",
            "pid": 2,
            "level": 2,
            "children":
            }
            ]
            },
            {
            "id": 5,
            "name": "three",
            "pid": 1,
            "level": 1,
            "children":
            },
            {
            "id": 6,
            "name": "four",
            "pid": 1,
            "level": 1,
            "children": [{
            "id": 7,
            "name": "seven",
            "pid": 6,
            "level": 2,
            "children":
            }]
            }
            ]
            },
            {
            "id": 8,
            "name": "one-2",
            "pid": 0,
            "level": 0,
            "children": [{
            "id": 9,
            "name": "two-2",
            "pid": 8,
            "level": 1,
            "children": [{
            "id": 10,
            "name": "five-2",
            "pid": 9,
            "level": 2,
            "children": [{
            "id": 11,
            "name": "four-2",
            "pid": 10,
            "level": 3,
            "children": [{
            "id": 12,
            "name": "four-3",
            "pid": 11,
            "level": 4,
            "children":
            }]
            }]
            }]
            },
            {
            "id": 13,
            "name": "five-3",
            "pid": 8,
            "level": 1,
            "children": [{
            "id": 14,
            "name": "four-3",
            "pid": 13,
            "level": 2,
            "children":
            },
            {
            "id": 15,
            "name": "four-4",
            "pid": 13,
            "level": 2,
            "children":
            },
            {
            "id": 16,
            "name": "four-5",
            "pid": 13,
            "level": 2,
            "children":
            }
            ]
            },
            {
            "id": 17,
            "name": "six-2",
            "pid": 8,
            "level": 1,
            "children":
            }
            ]

            }
            ]

            var icon = new Array('', '├ ', '└ ', '│ ');

            //Processing tree hierarchy data
            function getDeep(data) {
            var str = "";
            (function rec(data, depth) {
            var _prefix = (new Array(depth)).join(icon[3]);
            for (i in data) {
            if (data[i].name || '') {
            if (depth === 0) {
            str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
            console.log(data[i].name);
            } else {
            if (i < data.length - 1) {
            str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
            "</option>";
            console.log(_prefix + icon[1] + data[i].name);
            } else {

            str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
            "</option>";
            console.log(_prefix + icon[2] + data[i].name);
            }

            }

            }
            if (data[i].hasOwnProperty('children') && data[i].children.length) {
            rec(data[i].children, depth + 1);
            }
            }
            })(data, 0);
            $("#selectbox").append(str);
            }

            $(function() {
            getDeep(data);
            })

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <select id="selectbox"></select>








            share|improve this answer

























              up vote
              1
              down vote



              accepted










              Just move the str var outside your rec function






              var data = [{
              "id": 1,
              "name": "one",
              "pid": 0,
              "level": 0,
              "children": [{
              "id": 2,
              "name": "two",
              "pid": 1,
              "level": 1,
              "children": [{
              "id": 3,
              "name": "five",
              "pid": 2,
              "level": 2,
              "children":
              },
              {
              "id": 4,
              "name": "six",
              "pid": 2,
              "level": 2,
              "children":
              }
              ]
              },
              {
              "id": 5,
              "name": "three",
              "pid": 1,
              "level": 1,
              "children":
              },
              {
              "id": 6,
              "name": "four",
              "pid": 1,
              "level": 1,
              "children": [{
              "id": 7,
              "name": "seven",
              "pid": 6,
              "level": 2,
              "children":
              }]
              }
              ]
              },
              {
              "id": 8,
              "name": "one-2",
              "pid": 0,
              "level": 0,
              "children": [{
              "id": 9,
              "name": "two-2",
              "pid": 8,
              "level": 1,
              "children": [{
              "id": 10,
              "name": "five-2",
              "pid": 9,
              "level": 2,
              "children": [{
              "id": 11,
              "name": "four-2",
              "pid": 10,
              "level": 3,
              "children": [{
              "id": 12,
              "name": "four-3",
              "pid": 11,
              "level": 4,
              "children":
              }]
              }]
              }]
              },
              {
              "id": 13,
              "name": "five-3",
              "pid": 8,
              "level": 1,
              "children": [{
              "id": 14,
              "name": "four-3",
              "pid": 13,
              "level": 2,
              "children":
              },
              {
              "id": 15,
              "name": "four-4",
              "pid": 13,
              "level": 2,
              "children":
              },
              {
              "id": 16,
              "name": "four-5",
              "pid": 13,
              "level": 2,
              "children":
              }
              ]
              },
              {
              "id": 17,
              "name": "six-2",
              "pid": 8,
              "level": 1,
              "children":
              }
              ]

              }
              ]

              var icon = new Array('', '├ ', '└ ', '│ ');

              //Processing tree hierarchy data
              function getDeep(data) {
              var str = "";
              (function rec(data, depth) {
              var _prefix = (new Array(depth)).join(icon[3]);
              for (i in data) {
              if (data[i].name || '') {
              if (depth === 0) {
              str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
              console.log(data[i].name);
              } else {
              if (i < data.length - 1) {
              str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
              "</option>";
              console.log(_prefix + icon[1] + data[i].name);
              } else {

              str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
              "</option>";
              console.log(_prefix + icon[2] + data[i].name);
              }

              }

              }
              if (data[i].hasOwnProperty('children') && data[i].children.length) {
              rec(data[i].children, depth + 1);
              }
              }
              })(data, 0);
              $("#selectbox").append(str);
              }

              $(function() {
              getDeep(data);
              })

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <select id="selectbox"></select>








              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                Just move the str var outside your rec function






                var data = [{
                "id": 1,
                "name": "one",
                "pid": 0,
                "level": 0,
                "children": [{
                "id": 2,
                "name": "two",
                "pid": 1,
                "level": 1,
                "children": [{
                "id": 3,
                "name": "five",
                "pid": 2,
                "level": 2,
                "children":
                },
                {
                "id": 4,
                "name": "six",
                "pid": 2,
                "level": 2,
                "children":
                }
                ]
                },
                {
                "id": 5,
                "name": "three",
                "pid": 1,
                "level": 1,
                "children":
                },
                {
                "id": 6,
                "name": "four",
                "pid": 1,
                "level": 1,
                "children": [{
                "id": 7,
                "name": "seven",
                "pid": 6,
                "level": 2,
                "children":
                }]
                }
                ]
                },
                {
                "id": 8,
                "name": "one-2",
                "pid": 0,
                "level": 0,
                "children": [{
                "id": 9,
                "name": "two-2",
                "pid": 8,
                "level": 1,
                "children": [{
                "id": 10,
                "name": "five-2",
                "pid": 9,
                "level": 2,
                "children": [{
                "id": 11,
                "name": "four-2",
                "pid": 10,
                "level": 3,
                "children": [{
                "id": 12,
                "name": "four-3",
                "pid": 11,
                "level": 4,
                "children":
                }]
                }]
                }]
                },
                {
                "id": 13,
                "name": "five-3",
                "pid": 8,
                "level": 1,
                "children": [{
                "id": 14,
                "name": "four-3",
                "pid": 13,
                "level": 2,
                "children":
                },
                {
                "id": 15,
                "name": "four-4",
                "pid": 13,
                "level": 2,
                "children":
                },
                {
                "id": 16,
                "name": "four-5",
                "pid": 13,
                "level": 2,
                "children":
                }
                ]
                },
                {
                "id": 17,
                "name": "six-2",
                "pid": 8,
                "level": 1,
                "children":
                }
                ]

                }
                ]

                var icon = new Array('', '├ ', '└ ', '│ ');

                //Processing tree hierarchy data
                function getDeep(data) {
                var str = "";
                (function rec(data, depth) {
                var _prefix = (new Array(depth)).join(icon[3]);
                for (i in data) {
                if (data[i].name || '') {
                if (depth === 0) {
                str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
                console.log(data[i].name);
                } else {
                if (i < data.length - 1) {
                str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
                "</option>";
                console.log(_prefix + icon[1] + data[i].name);
                } else {

                str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
                "</option>";
                console.log(_prefix + icon[2] + data[i].name);
                }

                }

                }
                if (data[i].hasOwnProperty('children') && data[i].children.length) {
                rec(data[i].children, depth + 1);
                }
                }
                })(data, 0);
                $("#selectbox").append(str);
                }

                $(function() {
                getDeep(data);
                })

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <select id="selectbox"></select>








                share|improve this answer












                Just move the str var outside your rec function






                var data = [{
                "id": 1,
                "name": "one",
                "pid": 0,
                "level": 0,
                "children": [{
                "id": 2,
                "name": "two",
                "pid": 1,
                "level": 1,
                "children": [{
                "id": 3,
                "name": "five",
                "pid": 2,
                "level": 2,
                "children":
                },
                {
                "id": 4,
                "name": "six",
                "pid": 2,
                "level": 2,
                "children":
                }
                ]
                },
                {
                "id": 5,
                "name": "three",
                "pid": 1,
                "level": 1,
                "children":
                },
                {
                "id": 6,
                "name": "four",
                "pid": 1,
                "level": 1,
                "children": [{
                "id": 7,
                "name": "seven",
                "pid": 6,
                "level": 2,
                "children":
                }]
                }
                ]
                },
                {
                "id": 8,
                "name": "one-2",
                "pid": 0,
                "level": 0,
                "children": [{
                "id": 9,
                "name": "two-2",
                "pid": 8,
                "level": 1,
                "children": [{
                "id": 10,
                "name": "five-2",
                "pid": 9,
                "level": 2,
                "children": [{
                "id": 11,
                "name": "four-2",
                "pid": 10,
                "level": 3,
                "children": [{
                "id": 12,
                "name": "four-3",
                "pid": 11,
                "level": 4,
                "children":
                }]
                }]
                }]
                },
                {
                "id": 13,
                "name": "five-3",
                "pid": 8,
                "level": 1,
                "children": [{
                "id": 14,
                "name": "four-3",
                "pid": 13,
                "level": 2,
                "children":
                },
                {
                "id": 15,
                "name": "four-4",
                "pid": 13,
                "level": 2,
                "children":
                },
                {
                "id": 16,
                "name": "four-5",
                "pid": 13,
                "level": 2,
                "children":
                }
                ]
                },
                {
                "id": 17,
                "name": "six-2",
                "pid": 8,
                "level": 1,
                "children":
                }
                ]

                }
                ]

                var icon = new Array('', '├ ', '└ ', '│ ');

                //Processing tree hierarchy data
                function getDeep(data) {
                var str = "";
                (function rec(data, depth) {
                var _prefix = (new Array(depth)).join(icon[3]);
                for (i in data) {
                if (data[i].name || '') {
                if (depth === 0) {
                str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
                console.log(data[i].name);
                } else {
                if (i < data.length - 1) {
                str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
                "</option>";
                console.log(_prefix + icon[1] + data[i].name);
                } else {

                str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
                "</option>";
                console.log(_prefix + icon[2] + data[i].name);
                }

                }

                }
                if (data[i].hasOwnProperty('children') && data[i].children.length) {
                rec(data[i].children, depth + 1);
                }
                }
                })(data, 0);
                $("#selectbox").append(str);
                }

                $(function() {
                getDeep(data);
                })

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <select id="selectbox"></select>








                var data = [{
                "id": 1,
                "name": "one",
                "pid": 0,
                "level": 0,
                "children": [{
                "id": 2,
                "name": "two",
                "pid": 1,
                "level": 1,
                "children": [{
                "id": 3,
                "name": "five",
                "pid": 2,
                "level": 2,
                "children":
                },
                {
                "id": 4,
                "name": "six",
                "pid": 2,
                "level": 2,
                "children":
                }
                ]
                },
                {
                "id": 5,
                "name": "three",
                "pid": 1,
                "level": 1,
                "children":
                },
                {
                "id": 6,
                "name": "four",
                "pid": 1,
                "level": 1,
                "children": [{
                "id": 7,
                "name": "seven",
                "pid": 6,
                "level": 2,
                "children":
                }]
                }
                ]
                },
                {
                "id": 8,
                "name": "one-2",
                "pid": 0,
                "level": 0,
                "children": [{
                "id": 9,
                "name": "two-2",
                "pid": 8,
                "level": 1,
                "children": [{
                "id": 10,
                "name": "five-2",
                "pid": 9,
                "level": 2,
                "children": [{
                "id": 11,
                "name": "four-2",
                "pid": 10,
                "level": 3,
                "children": [{
                "id": 12,
                "name": "four-3",
                "pid": 11,
                "level": 4,
                "children":
                }]
                }]
                }]
                },
                {
                "id": 13,
                "name": "five-3",
                "pid": 8,
                "level": 1,
                "children": [{
                "id": 14,
                "name": "four-3",
                "pid": 13,
                "level": 2,
                "children":
                },
                {
                "id": 15,
                "name": "four-4",
                "pid": 13,
                "level": 2,
                "children":
                },
                {
                "id": 16,
                "name": "four-5",
                "pid": 13,
                "level": 2,
                "children":
                }
                ]
                },
                {
                "id": 17,
                "name": "six-2",
                "pid": 8,
                "level": 1,
                "children":
                }
                ]

                }
                ]

                var icon = new Array('', '├ ', '└ ', '│ ');

                //Processing tree hierarchy data
                function getDeep(data) {
                var str = "";
                (function rec(data, depth) {
                var _prefix = (new Array(depth)).join(icon[3]);
                for (i in data) {
                if (data[i].name || '') {
                if (depth === 0) {
                str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
                console.log(data[i].name);
                } else {
                if (i < data.length - 1) {
                str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
                "</option>";
                console.log(_prefix + icon[1] + data[i].name);
                } else {

                str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
                "</option>";
                console.log(_prefix + icon[2] + data[i].name);
                }

                }

                }
                if (data[i].hasOwnProperty('children') && data[i].children.length) {
                rec(data[i].children, depth + 1);
                }
                }
                })(data, 0);
                $("#selectbox").append(str);
                }

                $(function() {
                getDeep(data);
                })

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <select id="selectbox"></select>





                var data = [{
                "id": 1,
                "name": "one",
                "pid": 0,
                "level": 0,
                "children": [{
                "id": 2,
                "name": "two",
                "pid": 1,
                "level": 1,
                "children": [{
                "id": 3,
                "name": "five",
                "pid": 2,
                "level": 2,
                "children":
                },
                {
                "id": 4,
                "name": "six",
                "pid": 2,
                "level": 2,
                "children":
                }
                ]
                },
                {
                "id": 5,
                "name": "three",
                "pid": 1,
                "level": 1,
                "children":
                },
                {
                "id": 6,
                "name": "four",
                "pid": 1,
                "level": 1,
                "children": [{
                "id": 7,
                "name": "seven",
                "pid": 6,
                "level": 2,
                "children":
                }]
                }
                ]
                },
                {
                "id": 8,
                "name": "one-2",
                "pid": 0,
                "level": 0,
                "children": [{
                "id": 9,
                "name": "two-2",
                "pid": 8,
                "level": 1,
                "children": [{
                "id": 10,
                "name": "five-2",
                "pid": 9,
                "level": 2,
                "children": [{
                "id": 11,
                "name": "four-2",
                "pid": 10,
                "level": 3,
                "children": [{
                "id": 12,
                "name": "four-3",
                "pid": 11,
                "level": 4,
                "children":
                }]
                }]
                }]
                },
                {
                "id": 13,
                "name": "five-3",
                "pid": 8,
                "level": 1,
                "children": [{
                "id": 14,
                "name": "four-3",
                "pid": 13,
                "level": 2,
                "children":
                },
                {
                "id": 15,
                "name": "four-4",
                "pid": 13,
                "level": 2,
                "children":
                },
                {
                "id": 16,
                "name": "four-5",
                "pid": 13,
                "level": 2,
                "children":
                }
                ]
                },
                {
                "id": 17,
                "name": "six-2",
                "pid": 8,
                "level": 1,
                "children":
                }
                ]

                }
                ]

                var icon = new Array('', '├ ', '└ ', '│ ');

                //Processing tree hierarchy data
                function getDeep(data) {
                var str = "";
                (function rec(data, depth) {
                var _prefix = (new Array(depth)).join(icon[3]);
                for (i in data) {
                if (data[i].name || '') {
                if (depth === 0) {
                str += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
                console.log(data[i].name);
                } else {
                if (i < data.length - 1) {
                str += "<option value='" + data[i].id + "'>" + _prefix + icon[1] + data[i].name +
                "</option>";
                console.log(_prefix + icon[1] + data[i].name);
                } else {

                str += "<option value='" + data[i].id + "'>" + _prefix + icon[2] + data[i].name +
                "</option>";
                console.log(_prefix + icon[2] + data[i].name);
                }

                }

                }
                if (data[i].hasOwnProperty('children') && data[i].children.length) {
                rec(data[i].children, depth + 1);
                }
                }
                })(data, 0);
                $("#selectbox").append(str);
                }

                $(function() {
                getDeep(data);
                })

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <select id="selectbox"></select>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 at 12:08









                claw68

                34429




                34429






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392274%2fthe-combo-box-drop-down-tree-with-javascript%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen