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, nome: "b"}, {id: 21, pid: 2, nome: "b1"}, {id: 22, pid: 2, name: "b2"}, {id: 23, pid: 2, name: "b3"}, {id: 3, pid: 2, name: 2, 2, name: "b3"}, {id: 3, 3, pid: 2, name: 2, 2, name: "b3"}, {id: 3, 3, pid: 2, pid: 2, name: "b3"}, {id: 3, 3, pid: 0, pid: 2, "c" c "c" c "c" c "c" c "c" c "c. {id: 32, pid: 3, nome: "c2"}, {id: 33, pid: 3, nome: "c3"}, {id: 34, pid: 31, nome: "x"}, {id: 35, pid: 31, nome: "y"}, {{id: 36, pid: 31 {id: 37, pid: 36, nome: "z1123"}, {id: 38, pid: 37, nome: "z123123123"}]; função teemenu (a) {this.tree = a || []; this.groups = {};}; Treemenu.prototype = {init: function (pid) {this.group (); return this.getdom (this.Groups [PID]); }, grupo: function () {for (var i = 0; i <this.tree.length; i ++) {if (this.groups [this.tree [i] .pid]) {this.groups [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'; retornar html; }}; var html = new Treemenu (znodes) .init (0); alerta (html);Java:
teste de pacote; importar java.util.ArrayList; importar java.util.comparator; importar java.util.hashmap; importar java.util.iterator; importar java.util.list; importraereTereTrats {PublicReTra) {PublicReTra) {PublicSetrats). main (string [] args) {// 读取层次数据结果集列表 Datalist da lista = virtualDataGenerator.getvirtualResult (); // 节点列表 (散列表 , 用于临时存储节点对象) hashmap nodelist = new hashmap (); // 根节点 nó root = null; // 根据结果集构造节点列表 (存入散列表) para (iterator it = datalist.iterator (); it.hasNext ();) {map dataarecord = (map) it.next (); Nó nó = new Node (); node.id = (string) dataarecord.get ("id"); node.Text = (String) DataRecord.get ("text"); node.parentID = (String) DataRecord.get ("parentid"); nodelist.put (node.id, nó); } // 构造无序的多叉树 Set EntrySet = nodelist.entrySet (); para (iterator it = entradas. if (node.parentid == null || node.parentid.equals ("" ")) {root = node; } else {((node) nodelist.get (node.parentId)). addChild (nó); }} // 输出无序的树形菜单的 json 字符串 System.out.println (root.toString ()); // 对多叉树进行横向排序 root.sortChildren (); // 输出有序的树形菜单的 json 字符串 System.out.println (root.toString ()); : : 程序输出结果如下 程序输出结果如下 (无序的树形菜单) (格式化后的结果) : : // {// id: '100000', // text: '' ', // filhos: [// {// id:' 110000 ', // text:' '', // crianças: [// «// id: '113000',// text: '廊坊银行开发区支行', '廊坊银行开发区支行', ', // crianças: [//« // id:' 113000 ',// text:' 廊坊银行开发区支行 ', '111000', // Texto: '' ', // folha: true //}, // {// id:' 112000 ', // text:' '', // crianças: [// {// id: '112200', // text: '廊坊银行三大街支行', // folha: tuer //}, // {// {////10: true //} //] //} //] //} //] //} // 程序输出结果如下 (有序的树形菜单)) (格式化后的结果)) : // {// id: '100000', // text: '' ', // crianças: [// {// id:' 1100 ', // text:' 廊坊分行 ', '' ', // folha: true //}, // {// id:' 112000 ', // text:' '', // filhos: [// {// id: '112100', // text: '' ', // folha: true //}, // {// id:' 11200 ', }, // {// id: '113000', // text: '' ', // folha: true //} //] //} //] //}}}/*** 节点类*/classe node {/***节点编号*/public string id; / ** * 节点内容 */ public string text; / ** * 父节点编号 */ public string parentid; / ** * 孩子节点列表 */ crianças particulares Crianças = novos filhos (); // 先序遍历 , 拼接 json 字符串 public string tostring () {string resultado = "{" + "id: '" + id + "'" + ", text: '" + text + "'"; if (crianças! = null && Children.getSize ()! = 0) {resultado + = ", filhos:" + children.toString (); } else {resultado += ", folha: true"; } return resultado + "}"; } // 兄弟节点横向排序 public void Sortchildren () {if (filhos! }} // 添加孩子节点 public void addchild (nó nó) {this.children.addchild (nó); }}/*** 孩子列表类*/classe crianças {lista privada list = new ArrayList (); public int getSize () {return list.size (); } public void addchild (nó nó) {list.add (nó); } // 拼接孩子节点的 json 字符串 public string tostring () {string result = "["; for (iterator it = list.iterator (); it.hasnext ();) {resultado += ((nó) it.next ()). tostring (); resultado += ","; } resultado = resultado.substring (0, resultado.Length () - 1); resultado += "]"; resultado de retorno; } // 孩子节点排序 public void STELCHILDREN () {// 对本层节点进行排序 // 可根据不同的排序属性 , 传入不同的比较器 , 这里传入 ID 比较器 COLEÇÕES. // 对每个节点的下一层节点进行排序 para (iterator it = list.iterator (); it.hasnext ();) {((nó) it.next ()). Sortchildren (); }}}/** * 节点比较器 */classe nodeidComparator implementa o comparador {// 按照节点编号比较 public int compare (objeto o1, objeto o2) {int j1 = integer.parseint (((nó) o1) .id); int j2 = integer.parseint ((((nó) o2) .id); return (j1 <j2? -1: (j1 == j2? 0: 1)); }}/** * 构造虚拟的层次数据 */class VirtualDataGenerator {// 构造无序的结果集列表 , 实际应用中 , 该数据应该从数据库中查询获得; Lista estática pública getVirtualResult () {list Datalist = new ArrayList (); Hashmap dataarecord1 = new hashmap (); DataCord1.put ("ID", "112000"); DataCord1.put ("Texto", "廊坊银行解放道支行"); DataCord1.put ("ParentId", "110000"); Hashmap dataarecord2 = new hashmap (); DataCord2.put ("ID", "112200"); DataCord2.put ("Texto", "廊坊银行三大街支行"); DataRecord2.put ("ParentId", "112000"); Hashmap dataarecord3 = new hashmap (); DataCord3.put ("ID", "112100"); DataCord3.put ("Texto", "廊坊银行广阳道支行"); DataCord3.put ("ParentId", "112000"); Hashmap dataarecord4 = new hashmap (); DataCord4.put ("ID", "113000"); DataCord4.put ("Texto", "廊坊银行开发区支行"); DataRecord4.put ("ParentId", "110000"); Hashmap dataarecord5 = new hashmap (); DataCord5.put ("ID", "100000"); DataCord5.put ("Texto", "廊坊银行总行"); DataRecord5.put ("ParentId", ""); Hashmap dataarecord6 = new hashmap (); DataCord6.put ("ID", "110000"); DataCord6.put ("Texto", "廊坊分行"); DataRecord6.put ("ParentId", "100000"); Hashmap dataarecord7 = new hashmap (); DataRecord7.put ("ID", "111000"); DataCord7.put ("Texto", "廊坊银行金光道支行"); DataRecord7.put ("ParentId", "110000"); datalist.add (DataCord1); datalist.add (DataCord2); datalist.add (DataCord3); datalist.add (DataCord4); datalist.add (DataCord5); datalist.add (DataCord6); datalist.add (DataCord7); Datalista de retorno; }}以上这篇 Java 、 JS 中实现无限层级的树形结构方法 (类似递归) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。