ztree模糊查詢,只保留查詢到的節點
阿新 • • 發佈:2019-01-02
var ztreeNodes = [];var toshownodeslist = new Object(); var toshownodesArray = []; var checknodes =[]; var checknodeids =[];
var treeObj; function makeStationTree(node_type){ //樹的銷燬 treeObj = $.fn.zTree.getZTreeObj("tree_station"); treeObj.destroy(); //隱藏所有樹節點 treeObj = null; $.fn.zTree.init($("#tree_station"), setting_station, ztreeNodes); treeObj = $.fn.zTree.getZTreeObj("tree_station"); var allNode = treeObj.transformToArray(treeObj.getNodes()); for(var i = 0;i < allNode.length;i++){ allNode[i].nocheck = true; treeObj.updateNode(node); } treeObj.hideNodes(allNode); //顯示當前型別及父親節點 var nodelist = treeObj.getNodesByParamFuzzy("node_type",node_type,null); for(var n = 0;n<nodelist.length;n++){ toshownodeslist[[Number(nodelist[n].id)]] = nodelist[n]; checknodes.push(nodelist[n]); findParent(treeObj,nodelist[n]); } $.each(toshownodeslist,function (key,val) { toshownodesArray.push(toshownodeslist[key]); }) treeObj.showNodes(toshownodesArray); //toshownodeslist = []; toshownodeslist = new Object(); toshownodesArray = []; checknodeids =[]; //顯示的節點再過濾哪些有checkbox var treeObj = $.fn.zTree.getZTreeObj("tree_station"); for(var i=0;i<checknodes.length;i++){ var node = checknodes[i]; node.nocheck = false; treeObj.updateNode(node); checknodeids[i] = node.id; } checknodes = []; } function findParent(treeObj,node){ //treeObj.expandNode(node,true,false,false); var pNode =node.getParentNode(); if(pNode!=null){ //toshownodeslist.push(pNode); toshownodeslist[[Number(pNode.id)]] = pNode; findParent(treeObj,pNode); } }
涉及到幾個點:
ztree隱藏函式hide()
ztree節點nocheck屬性
ztree查詢父親節點,避免出現同一父親幾點多次放入陣列,使用
var toshownodeslist = new Object();
toshownodeslist[[Number(pNode.id)]] = pNode;可以保證唯一性,同一id多次賦值,後面覆蓋前面的
涉及Object轉正陣列[]
$.each(toshownodeslist,function (key,val) { toshownodesArray.push(toshownodeslist[key]); })