JS:
var znodes=[{id:0,pid:-1,name: "Aaaa"}, {id:1,pid:0,name: "A"}, {id:11,pid:1,name: "A1"}, {id:12,pid:1,name: "A2"}, {id:13,pid:1,name: "A3"}, {id:2,pid:0,name: "B"}, {id:21,pid:2,name: "B1"}, {id:22,pid:2,name: "B2"}, {Id:23,pid:2,nam E: "B3"}, {id:3,pid:0,name: "C"}, {id:31,pid:3,name: "C1"}, {id:32,pid:3,name: "C2"}, {id:33,pid:3,name: "C3"}, {Id:3 4,pid:31,name: "X"}, {id:35,pid:31,name: "Y"}, {id:36,pid:31,name: "Z"}, {id:37,pid:36,name: "z1123"}, {id:38,pid:37
, Name: "Z123123123"}]; function Treemenu (a) {this.tree=a| |
[];
this.groups={};
};
treemenu.prototype={init:function (PID) {this.group ();
Return This.getdom (This.groups[pid]); }, Group:function () {for (Var i=0;i<this.tree.length;i++) {if (This.groups[this.tree[i].pid]) {THIS.G
Roups[this.tree[i].pid].push (This.tree[i]);
}else{this.groups[this.tree[i].pid]=[];
This.groups[this.tree[i].pid].push (This.tree[i]);
}}, Getdom:function (a) { if (!a) {return '} var html= ' \n<ul >\n ';
for (Var i=0;i<a.length;i++) {html+= ' <li><a href= ' # ' > ' +a[i].name+ ' </a> ';
Html+=this.getdom (This.groups[a[i].id]);
html+= ' </li>\n ';
};
html+= ' </ul>\n ';
return HTML;
}
};
var html=new treemenu (znodes). Init (0); alert (HTML);
java:
Package test;
Import java.util.ArrayList;
Import Java.util.Comparator;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;
Import java.util.Collections; /** * Multi-fork Tree class */public class Multipletree {public static void main (string[] args) {//Read the hierarchical data result set list dataList = Vir
Tualdatagenerator.getvirtualresult ();
Node list (hash list, for temporary storage node objects) HashMap nodelist = new HashMap ();
Root nodes node root = null; Constructs a list of nodes (stored in a hash table) for a result set (iterator it = Datalist.iterator (); It.hasnext ();)
{Map DataRecord = (map) it.next ();
Node node = new node ();
Node.id = (String) datarecord.get ("id");
Node.text = (String) datarecord.get ("text");
Node.parentid = (String) datarecord.get ("ParentID");
Nodelist.put (node.id, node);
}//Constructs a unordered Set of EntrySet = Nodelist.entryset (); for (Iterator it = Entryset.iterator (); It.hasnext ();)
{Node node = (node) ((Map.entry) It.next ()) GetValue (); if (Node.parentid = null | | node.parEntid.equals ("")) {root = node;
else {(node) nodelist.get (Node.parentid)). AddChild (node);
The JSON string System.out.println (Root.tostring ()) of the unordered tree menu is output;
Cross-order Root.sortchildren () for multiple fork trees;
Output the ordered Tree menu JSON string System.out.println (root.tostring ()); The results of the program output are as follows (unordered tree menu) (formatted result)://{//ID: ' 100000 ',//text: ' Langfang Bank head Office ',//children: [//{//ID: ' 11 0000 ',//text: ' Langfang branch ',//children: [//{//ID: ' 113000 ',//text: ' Langfang Bank Development Zone Branch ',//leaf: True//},//{//ID: ' 111000 ',//text: ' Langfang Bank Golden Road Sub-branch ',//Leaf:true//},//{//I D: ' 112000 ',//text: ' Langfang Bank Jiefang Road Branch ',//children: [//{//ID: ' 112200 ',//text: ' Langfang Bank three street branch ',//Leaf:true//},//{//ID: ' 112100 ',//text: ' Langfang Bank Guangyang Road Branch ',//leaf:true// //]///////////////////////////////////////////////////////////////////Ext: ' Langfang Bank headquarters ',//children: [//{//ID: ' 110000 ',//text: ' Langfang branch ',///children: [//{// ID: ' 111000 ',//text: ' Langfang Bank Golden Road Sub-branch ',//Leaf:true//},//{//ID: ' 112000 ',//text: ' Langfang Bank Jiefang Road Sub-branch ',//children: [//{//ID: ' 112100 ',//text: ' Langfang Bank Guangyang Branch ',//Leaf:true/ },//{//ID: ' 112200 ',//text: ' Langfang Bank three street sub-branch ',//leaf:true////////},//
{//ID: ' 113000 ',//text: ' Langfang Bank Development Zone Sub-branch ',//leaf:true///////////////}
/** * Node class/class Node {/** * node number/public String ID;
/** * Node content */public String text;
/** * Parent Node number/public String parentid;
/** * child Node list */private Children Children = new Children ();
First-order traversal, stitching JSON string public string toString () {string result = "{" + "ID: '" + ID + "" + ", Text: '" + text + ""; if (children!= null && children.getsize ()!= 0) {result = ", children:" + children.tostring ();
else {result = ", leaf:true";
return result + "}"; }//Sibling node horizontal sort public void SortChildren () {if (Children!= null && children.getsize ()!= 0) {Children.sort
Children ();
}//Add the child node public void AddChild (node node) {This.children.addChild (node);
}/** * Children list class */class Children {Private list = new ArrayList ();
public int GetSize () {return list.size ();
public void AddChild (node node) {List.add (node);
///stitching the child node's JSON string public string toString () {string result = ' ['; for (Iterator it = List.iterator (); It.hasnext ();)
{result = = ((Node) It.next ()). ToString ();
result = = ",";
result = result.substring (0, Result.length ()-1);
result = = "]";
return result; //child node sort public void SortChildren () {//sorting the nodes of this layer//can be passed into a different comparator based on the different sorting properties, where the ID comparator collections.sort (list, new N
Odeidcomparator ()); Sorts the next level node for each node for (iterator it = List.iterator ();It.hasnext ();)
{(Node) It.next ()). SortChildren (); }}/** * node comparer/class Nodeidcomparator implements Comparator {//Compare public int compare by node number (object O1, Object
O2) {int j1 = Integer.parseint ((Node) O1). ID);
int j2 = Integer.parseint ((Node) O2). ID);
Return (J1 < J2-1: (J1 = = J2? 0:1)); }/** * Constructs the virtual hierarchical data/class Virtualdatagenerator {//Constructs a unordered list of result sets that should be queried from the database for practical applications; public static list Getvirtua
Lresult () {List dataList = new ArrayList ();
HashMap dataRecord1 = new HashMap ();
Datarecord1.put ("id", "112000");
Datarecord1.put ("text", "Langfang Bank Jiefang Road Branch");
Datarecord1.put ("ParentID", "110000");
HashMap dataRecord2 = new HashMap ();
Datarecord2.put ("id", "112200");
Datarecord2.put ("text", "Langfang Bank three Street branch");
Datarecord2.put ("ParentID", "112000");
HashMap dataRecord3 = new HashMap ();
Datarecord3.put ("id", "112100");
Datarecord3.put ("text", "Langfang Bank Guangyang Road Branch");
Datarecord3.put ("ParentID", "112000");
HashMap DataRecord4 = new HashMap (); DatarEcord4.put ("id", "113000");
Datarecord4.put ("text", "Langfang Bank Development Zone Branch");
Datarecord4.put ("ParentID", "110000");
HashMap DataRecord5 = new HashMap ();
Datarecord5.put ("id", "100000");
Datarecord5.put ("text", "Langfang Bank headquarters");
Datarecord5.put ("ParentID", "");
HashMap DataRecord6 = new HashMap ();
Datarecord6.put ("id", "110000");
Datarecord6.put ("text", "Langfang branch");
Datarecord6.put ("ParentID", "100000");
HashMap DataRecord7 = new HashMap ();
Datarecord7.put ("id", "111000");
Datarecord7.put ("text", "Langfang Bank Golden Road Branch");
Datarecord7.put ("ParentID", "110000");
Datalist.add (DATARECORD1);
Datalist.add (DATARECORD2);
Datalist.add (DATARECORD3);
Datalist.add (DATARECORD4);
Datalist.add (DATARECORD5);
Datalist.add (DATARECORD6);
Datalist.add (DATARECORD7);
return dataList; }
}
Above this Java, JS in the implementation of the infinite level of tree structure method (similar to recursion) is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.