JS中:
var znodes = [{id:0,pid:-1,name:“ aaaa”},{id:1,pid:0,name:a“ a”},{id:11,pid:1,name:a1“ a1”},{ {id:2,pId:0,name:"B"}, {id:21,pId:2,name:"B1"}, {id:22,pId:2,name:"B2"}, {id:23,pId:2,name:"B3"}, {id:3,pId:0,name:"C"}, {id:31,pId:3,name:"C1"}, {id:32,pID:3,名称:“ C2”},{id:33,pid:3,name:“ c3”},{id:34,pid:31,name:“ x”},{id:35,pid:35,pid:31,name:“ name:y” y“ y”},ID:36,{:36,PID:31,PID:31,名称:31,名称: {id:37,pid:36,名称:“ z1123”},{id:38,pid:37,name:“ z123123123”}]; function treemenu(a){this.tree = a | a || []; this.groups = {};}; treemenu.prototype = {init:function(pid){this.group();返回this.getdom(this.groups [pid]); },group:function(){for(var i = 0; i <this.tree.length; i ++){if(this.groups [this.tree [i] .pid])){this.groups [this.tree [tree [i] .pid] .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 =“ href =”#“>”>'+a [i] .name+'</a>'; html+= this.getDom(this.groups [a [i] .id]); html+='</li>/n'; }; html+='</ul>/n';返回html; }}; var html = new treemenu(znodes).init(0); arter(html);爪哇:
软件包测试;导入java.util.arraylist; import java.util.comparator; import java.util.hashmap; import java.util.iterator; import java.util.util.list; import java.util.map; import java.util.map; import Java.util.set; import Java.util.util.collection java.util.collection; main(string [] args){//读取层次数据结果集列表列表datalist = virtualdatagenerator.getVirtualResult(); // 节点列表(散列表,用于临时存储节点对象)hashmap nodelist = new hashmap(); //根节点节点root = null; // 根据结果集构造节点列表(存入散列表)for(iterator it = datalist.iterator(); it.hasnext();){map dataRecord =(map)it.next();节点节点= new node(); node.id =(string)datarecord.get(“ id”); node.text =(string)datarecord.get(“ text”); node.parentid =(string)dataRecord.get(“ parendId”); nodelist.put(node.id,node); } //构造无序的多叉树设置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); }} //输出无序的树形菜单的json字符串system.out.println(root.toString()); //对多叉树进行横向排序root.sortchildren(); //输出有序的树形菜单的json字符串system.out.println(root.toString()); ((// 程序输出结果如下(无序的树形菜单)(格式化后的结果) '廊坊银行金光道支行',// laf:true //},// {// {// id:'112000',//文本:'廊坊银行解放道支行',//子女:[// {// {// {// {// {// 112200',// text:'廊坊银行三大街支行',// laf',// laf:true //} //} //] //} //(():// {// {// ID:'100000',// text:'廊坊银行总行',//儿童:[// {// {// ID:'110000',// text:'110000',// text:'廊坊分行',// // id:'112000',//文本:'廊坊银行解放道支行',//儿童:[// {// {// ID:'112100',//文字:'廊坊银行广阳道支行',// laf:true //},// {// {// {// {// {112200 '廊坊银行开发区支行',// leaf:true //} //] //} //] //}}}}}}/***节点类*/class node {/***节点编号*节点编号*/public String ID; / ** *节点内容 */公共字符串文本; / ** *父节点编号 */ public String parentId; / ** *孩子节点列表 */私人儿童=新孩子(); //先序遍历,拼接json字符串public String toString(){字符串结果=“ {” +“ ID:'” + ID +“' +” +“,text:'” + text +“''''''; if(children!= null && childris.getSize()!= 0){结果 + =“,儿童:” + childris.tostring(); } else {结果 +=“,叶:true”; }返回结果 +“}”; } //兄弟节点横向排序public void sortchildren(){if(childris!= null && childris.getSize()!= 0){children.sortchildren(); }} //添加孩子节点public void addChild(node node){this.children.addchild(node); }}/***孩子列表类*/类儿童{私人列表list = new arraylist(); public int getsize(){return list.size(); } public void addChild(node node){list.add(node); } //拼接孩子节点的json字符串public string tostring(){字符串结果=“ [”; for(iterator it = list.iterator(); it.hasnext();){结果 +=((node)it.next())。toString();结果 +=“,”; }结果= result.substring(0,result.length() - 1);结果 +=“]”;返回结果; } //孩子节点排序public void sortChildren(){//对本层节点进行排序//可根据不同的排序属性,传入不同的比较器//对每个节点的下一层节点进行排序for(iterator it = list.iterator(); it.hasnext();){((node)it.next())。sortChildren(); }}}/** *节点比较器 */类NodeIdComparator实现了比较器{//按照节点编号比较public int comparare(对象O1,Object O2){int j1 = integer.parseint.parseint((((node)o1).id).id); int j2 = integer.parseint((((node)o2).id);返回(j1 <j2?-1 :( j1 == j2?0:1)); }}/** *构造虚拟的层次数据 */class virtualDatagenerator {//构造无序的结果集列表,实际应用中,该数据应该从数据库中查询获得;,该数据应该从数据库中查询获得;公共静态列表getVirtualResult(){list datalist = new arraylist(); hashmap dataRecord1 = new hashmap(); datarecord1.put(“ id”,“ 112000”); dataRecord1.put(“ text”,“”,“廊坊银行解放道支行”); dataRecord1.put(“ parentId”,“ 110000”); hashmap dataRecord2 = new hashmap(); datarecord2.put(“ id”,“ 112200”); datarecord2.put(“ text”,“”,“廊坊银行三大街支行”); dataRecord2.put(“ parendId”,“ 112000”); hashmap dataRecord3 = new hashmap(); datarecord3.put(“ id”,“ 112100”); dataRecord3.put(“ text”,“”,“廊坊银行广阳道支行”); dataRecord3.put(“ parentId”,“ 112000”); hashmap dataRecord4 = new hashmap(); datarecord4.put(“ id”,“ 113000”); datarecord4.put(“ text”,“”,“廊坊银行开发区支行”); dataRecord4.put(“ parendId”,“ 110000”); hashmap dataRecord5 = new hashmap(); datarecord5.put(“ id”,“ 100000”); datarecord5.put(“ text”,“”,“廊坊银行总行”); dataRecord5.put(“ parendId”,“”); hashmap dataRecord6 = new hashmap(); datarecord6.put(“ id”,“ 110000”); dataRecord6.put(“ text”,“”,“廊坊分行”); dataRecord6.put(“ parendId”,“ 100000”); hashmap dataRecord7 = new hashmap(); datarecord7.put(“ id”,“ 111000”); dataRecord7.put(“ text”,“”,“廊坊银行金光道支行”); dataRecord7.put(“ parendId”,“ 110000”); datalist.add(dataRecord1); datalist.add(dataRecord2); datalist.add(dataRecord3); datalist.add(dataRecord4); datalist.add(dataRecord5); datalist.add(dataRecord6); datalist.add(dataRecord7);返回数据师; }}}以上这篇java js中实现无限层级的树形结构方法(类似递归)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。,也希望大家多多支持武林网。