dwr 返回的数据格式
var treeData =[ {"id":"1200000100","parentId":"0","text":"苏州报表中心","staffId":"1200001016","level":"1","state":"closed"}, {"id":"1200000345","parentId":"1200000100","text":"周期报表","staffId":"1200001016","level":"2","state":"closed"}, {"id":"1200001245","parentId":"1200000100","text":"报表模板","staffId":"1200001016","level":"2","state":"closed"}, {"id":"1200001487","parentId":"1200000345","text":"创电中心","staffId":"1200001016","level":"3","state":"closed"}, {"id":"1200001540","parentId":"1200001245","text":"报表测试","staffId":"1200001016","level":"3","state":"open","iconCls":"icon_P"}, {"id":"1200001266","parentId":"1200001245","text":"报表导入测试","staffId":"1200001016","level":"3","state":"open","iconCls":"icon_A"}, {"id":"1200002653","parentId":"1200001245","text":"编辑列表测试","staffId":"1200001016","level":"3","state":"open","iconCls":"icon_E"}, {"id":"1200001567","parentId":"1200001245","text":"冻结列表测试","staffId":"1200001016","level":"3","state":"open","iconCls":"icon_F"}, {"id":"1200001377","parentId":"1200000345","text":"业管中心","staffId":"1200001016","level":"3","state":"closed"}, {"id":"1200001370","parentId":"1200000345","text":"政企客户部","staffId":"1200001016","level":"3","state":"closed"}, {"id":"1200000845","parentId":"1200001245","text":"曲线图表测试","staffId":"1200001016","level":"3","state":"open","iconCls":"icon_C"}, {"id":"1200000965","parentId":"1200001245","text":"柱状图表测试","staffId":"1200001016","level":"3","state":"open","iconCls":"icon_H"}, {"id":"1200001005","parentId":"1200001245","text":"下拉树参数测试","staffId":"1200001016","level":"3","state":"open","iconCls":"icon_P"}, {"id":"1200000985","parentId":"1200001245","text":"异步树表测试","staffId":"1200001016","level":"3","state":"open","iconCls":"icon_S"}, {"id":"1200002996","parentId":"1200001487","text":"王二狗","staffId":"1200001016","level":"4","state":"closed"}, {"id":"1200002253","parentId":"1200001370","text":"政企渠道中心","staffId":"1200001016","level":"4","state":"closed"}, {"id":"1200000765","parentId":"1200001377","text":"钱红燕","staffId":"1200001016","level":"4","state":"closed"}, {"id":"1200002997","parentId":"1200002996","text":"王二狗-短信包统计","staffId":"1200001016","level":"5","state":"open","iconCls":"icon_P"}, {"id":"1200004180","parentId":"1200000765","text":"王xx-号簿欠费调整清单","staffId":"1200001016","level":"5","state":"open","iconCls":"icon_L"}, {"id":"1200001383","parentId":"1200002253","text":"王xx","staffId":"1200001016","level":"5","state":"closed"}, {"id":"1200001384","parentId":"1200001383","text":"王xx_招财宝每月统计数据","staffId":"1200001016","level":"6","state":"open","iconCls":"icon_L"} ] //查询树节点 function doSearch(){ var treeTxt = $("#treeTxt").val(); //dwr 调用后台 conductRpt.getConductRptSch(treeTxt,staffId,function(treeData){ //获取根节点 var node = $('#myTree').tree('getRoot'); //获取根节点的子节点 var childData = $("#myTree").tree('getChildren',node.target); //删除所有子节点 for(var i=0;i<childData.length;i++){ $("#myTree").tree('remove',childData[i].target); } //转换后台返回的结果集【List】 treeData 数组 var nodes = convert(treeData) //追加子节点 $("#myTree").tree('append',{parent:(node ? node.target : null),data : nodes}); }); } function convert(treeData){ var nodes = []; // 得到顶层节点 for(var i=0; i<treeData.length;i++){ var row = treeData[i]; if (!exists(treeData, row.parentId)){ nodes.push({ id : row.id, text : row.text, iconCls : row.iconCls }); } } var nodesArr = []; for(var i=0; i<nodes.length;i++){ nodesArr.push(nodes[i]); } while(nodesArr.length){ var node = nodesArr.shift(); // 父节点 shift()方法用于把数组的第一个元素从其中删除,并返回第一个元素的值 // 得到子节点 for(var i=0; i<treeData.length;i++){ var row = treeData[i]; if(row.parentId==node.id){ var child ={ id: row.id, text: row.text, iconCls: row.iconCls }; if(node.children){ node.children.push(child); } else { node.children = [child]; } nodesArr.push(child); } } } return nodes; } function exists(treeData, parentId){ for(var i=0; i<treeData.length;i++){ if (treeData[i].id == parentId){ return true; } } return false; }
原文:https://blog.csdn.net/snowvhaha/article/details/56675664?utm_source=copy