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