_javascript technique of tree-shaped dropdown frame based on javascript

Source: Internet
Author: User
Tags mongodb

Usually encounter the problem of tree structure, such as the display directory structure.

In most cases, the background returns such data as follows:

[
{id:19, pid:0, Name: ' Nodejs '},
{id:20, pid:19, Name: ' Express '},
{id:21, pid:19, Name: ' MongoDB ' },
{id:60, pid:20, Name: ' Ejs '},
{id:59, pid:0, Name: ' Front-End Development '},
{id:70, pid:59, Name: ' JavaScript ' },
{id:71, pid:59, Name: ' CSS '},
{id:72, pid:59, Name: ' HTML '},
{id:73, pid:59, Name: ' Bootstrap '} ,
{id:61, pid:0, Name: ' Visual Design '},
{id:63, pid:61, Name: ' Web Design '},
]

This data structure is easy to handle in the background, but the front desk is difficult to handle, you need to first turn it into a tree-shaped JSON data, as follows:

[
{id:19, pid:0, Name: ' Nodejs ',
 children:[
   {id:20, pid:19, Name: ' Express ', children:[{id:60, Pid:20, Name: ' Ejs '}},
   {id:21, pid:19, Name: ' MongoDB '}
   ]
},
{id:59, pid:0, Name: ' Front-End development ',
 children:[
  {id:70, pid:59, Name: ' JavaScript '},
  {id:71, pid:59, Name: ' CSS '},
  {id:72, pid:59, Name: ' HTML '},
  {id:73, pid:59, Name: ' Bootstrap '}
   
    ]
},
{id:61, pid:0, Name: ' Visual design ', children:[{id:63, pid:61, Name: ' Web Design '}]},


   

This makes it easy to build a tree-shaped component recursively.

If the background can be directly returned to this structure is the best, otherwise the front need to do the conversion.

First, turn the data of list array structure into tree structure JSON

List to tree JSON
function Listtotree (list,pid) {
var ret = [];//A temporary array of results
(var i in list) {
if (list[ I].pid = = pid) {//If the parent ID of the current item is equal to the parent ID to find, make a recursive
 List[i].children = listtotree (list, list[i].id);
 Ret.push (List[i])//Save the current item to a temporary array
}
return ret;//Returns the result after recursion
}

var tree=listtotree (list,0) //Call function, passing in the list array to convert, and the PID Console.log (tree) of the top-level elements in the trees

;


Second, according to the tree structure JSON data generation Drop-down box

Add Drop-down box container
<select id= "Selectbox" name= "" ></select>
//js script, recursively generate
//Get container object
var Selectbox=document.getelementbyid ("Selectbox");

Spanning Tree drop-down menu
var j= "-"//prefix symbol for displaying parent-child relationships, where you can use other symbol
function Creatselecttree (d) {
 var option= "";
 for (Var i=0;i<d.length;i++) {if
 (d[i].children.length) {//If a subset
  option+= "<option value=" +d[i].id+ "' >" +j+d[i].name+ "</option>";
 j+= "-";//prefix notation plus a symbol
  option+=creatselecttree (d[i].children);//recursive invocation subset
 J=j.slice (0,j.length-1); Each time the recursive end returns to the superior, the prefix symbol needs to be reduced by a symbol
  }else{//there is no subset directly showing
  option+= "<option value=" "+d[i].id+" ' > ' +j+d[i].name+ ' </option> ";
  }
  }
 Return option;//returns the final HTML result
 }

//Call function and moves the structure into the Drop-down container
selectbox.innerhtml=creatselecttree (tree);

If you want to learn more, you can click the jquery dropdown box effect summary, JavaScript drop-down Box effect summary to learn.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.