Unlimited level tree implemented by Treeview (background) and JavaScript (foreground)

Source: Internet
Author: User

There is such a table in the database (common region selection will also be similar structure), mainly including ID, name, parentid,

In order to achieve the effect of an infinite Layer tree, how can we present such a tree? The preceding and background methods are used for implementation.

1. Bind the backend to the Treeview to implement an infinite number of trees.

  

View code

1 protected void page_load (Object sender, eventargs E) 2 {3 cinemagroupbll BLL = new cinemagroupbll (); 4 5 bindtreeview (BLL. getallgroup (), treeview1.nodes, "0"); 6} 7 8 9 // bind list 10 private void bindtreeview (datatable, treenodecollection treenodes, string parentid) 11 {12 dataview = new dataview (datatable); 13 treenode node; 14 dataview. rowfilter = "parentid =" + "'" + parentid + "'"; 15 dataview. sort = "sortnum DESC"; 16 foreach (datarowview in dataview) 17 {18 node = new treenode (); 19 node. TEXT = datarowview ["groupname"]. tostring (); 20 21 treenodes. add (node); 22 bindtreeview (datatable, node. childnodes, datarowview ["groupid"]. tostring (); 23} 24}

 

2. Obtain JSON data in the specified format through the foreground, and splice the data to form a tree layer.

View code

<SCRIPT type = "text/JavaScript"> var temp = [{'id': 1, 'name': 'hq ', 'parentid': 0 }, {'id': 2, 'name': 'wuhan branch ', 'parentid': 1}, {'id': 3, 'name': 'Beijing branch ', 'parentid': 1}, {'id': 4, 'name': 'wuhan cinema 1', 'parentid': 2}, {'id': 5, 'name': 'group', 'parentid': 4}, {'id': 6, 'name': 'mcdonald ', 'parentid': 5 }, {'id': 6, 'name': 'Beijing XX cinema ', 'parentid': 3}]; $ (function () {var arr = ['']; createtree (temp, arr, 0); var STR = arr. join (''); // remove null <ul> </ul> STR = Str. replace (/<ul> \ s * <\/ul>/g, ''); values ('{treediv'{.html (STR) ;}); // recursive tree creation function createtree (JSON, arrhtml, parnetid) {If (JSON = NULL | JSON. length <1) {return;} arrhtml. push ('<ul>'); For (VAR I = 0, Len = JSON. length; I <Len; I ++) {If (parnetid = JSON [I] ['parentid']) {arrhtml. push ('<li>' + JSON [I] ['name'] + '</LI>'); arrhtml. push ('<ul>'); For (var j = 0, Len = JSON. length; j <Len; j ++) {If (JSON [I] ['id'] = JSON [J] ['parentid']) {arrhtml. push ('<li>' + JSON [J] ['name'] + '</LI>'); createtree (JSON, arrhtml, JSON [J] ['id']);} arrhtml. push ('</ul>');} arrhtml. push ('</ul>');} </SCRIPT>

JS implementation:

 

 

 

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.