bootstrap-treeview的完善(父子节点的全选与反选)以及使用的方法

// 选中父节点时,选中所有子节点
    function getChildNodeIdArr(node) {   
        var ts = [];  
        if (node.nodes) {    
            for (x in node.nodes) {     
                ts.push(node.nodes[x].nodeId);     
                if (node.nodes[x].nodes) {      
                    var getNodeDieDai = getChildNodeIdArr(node.nodes[x]);      
                    for (j in getNodeDieDai) {       
                            ts.push(getNodeDieDai[j]);     
                    }     
                }    
            }   
        } else {    
            ts.push(node.nodeId);   
        }   
        return ts;  
    }

// 选中父节点时,选中所有子节点
    function getChildNodeIdArr(node) {   
        var ts = [];  
        if (node.nodes) {    
            for (x in node.nodes) {     
                ts.push(node.nodes[x].nodeId);     
                if (node.nodes[x].nodes) {      
                    var getNodeDieDai = getChildNodeIdArr(node.nodes[x]);      
                    for (j in getNodeDieDai) {       
                            ts.push(getNodeDieDai[j]);     
                    }     
                }    
            }   
        } else {    
            ts.push(node.nodeId);   
        }   
        return ts;  
    }

 

// 取消父节点时 取消所有子节点
    function setChildNodeUncheck(node) {
        if (node.nodes) {   
            var ts = [];    //当前节点子集中未被选中的集合
            for (x in node.nodes) {
                if (!node.nodes[x].state.checked) {  
                    ts.push(node.nodes[x].nodeId);  
                }
                if (node.nodes[x].nodes) {      
                    var getNodeDieDai = node.nodes[x];
                    console.log(getNodeDieDai);      
                    for (j in getNodeDieDai) {
                        if (!getNodeDieDai.nodes[x].state.checked) {        
                            ts.push(getNodeDieDai[j]);
                        }    
                    }     
                }    
            }   
        }
        return ts;  
    }

onNodeChecked: function(event, node) { //选中节点
                var selectNodes = getChildNodeIdArr(node); //获取所有子节点      
                if (selectNodes) { //子节点不为空,则选中所有子节点       
                    $('#treeview-checkable').treeview('checkNode', [selectNodes, { silent: true }]);     
                }     
                var parentNode = $("#treeview-checkable").treeview("getNode", node.parentId);      
                setParentNodeCheck(node);   
          },    
          onNodeUnchecked: function(event, node) { //取消选中节点  
                // 取消父节点 子节点取消
                var selectNodes = setChildNodeUncheck(node); //获取未被选中的子节点
                var childNodes = getChildNodeIdArr(node);    //获取所有子节点
                if (selectNodes && selectNodes.length==0) { //有子节点且未被选中的子节点数目为0,则取消选中所有子节点   
                  console.log("反选");   
                  $('#treeview-checkable').treeview('uncheckNode', [childNodes, { silent: true }]);      
                }  
                // 取消节点 父节点取消
                var parentNode = $("#treeview-checkable").treeview("getNode", node.parentId);  //获取父节点
                var selectNodes = getChildNodeIdArr(node);     
                setParentNodeCheck(node);
          }   
        });

bootstrap-treeview的完善(父子节点的全选与反选)以及使用的方法_第1张图片

你可能感兴趣的:(bootstrap-treeview的完善(父子节点的全选与反选)以及使用的方法)