ztree选父不选子,选子不选父

发布时间:2019-05-23 22:00:15发布者:Mr.Zhang阅读(241)

var setting = {
            data: {
                simpleData: {
                    enable: true
                }
            }
            ,check: {
                enable: true
                , chkStyle: 'checkbox'
                //,chkboxType:{ "Y": "", "N": "" }
            }
            ,callback:{
                onCheck:function(e,treeId, treeNode){
                    if(treeId == "treeDemo") {
                        var zTreeObj = $.fn.zTree.getZTreeObj(treeId);
                        optParOrSon(treeId, treeNode,zTreeObj);
                    }
                }
            }
        };
//获取所有子节点
	    var getChildsByTreeNode = function(treeNode,childsArr){
	    	var childsArr = childsArr || [];
	    	if (treeNode.isParent) {//是父节点则获取所有子节点
	    		var childs = treeNode.children;
	    		for(var i in childs){
		        	childsArr.push(childs[i]);
		        	getChildsByTreeNode(childs[i],childsArr);
	        	}
	    	}
	    	return childsArr;
	    };
	    
	    //获取所有父节点
	    var getParsByTreeNode = function(treeNode,parsArr){
	    	var parsArr = parsArr || [];
	    	var parNode = treeNode.getParentNode();
	    	if(parNode){
	    		parsArr.push(parNode);
	    		getParsByTreeNode(parNode,parsArr);
	    	}
	    	return parsArr;
	    }

  

/**
         * 选父不选子,选子不选父
         * */
        var optParOrSon = function(treeId,treeNode,zTreeObj){
            if(treeNode.checked){
                //取消全部后代节点的选中
                var childNodes = getChildsByTreeNode(treeNode);
                for(var i in childNodes){
                    zTreeObj.checkNode(childNodes[i], false, false);
                }
                //同时全部祖先节点也不能被选中
                if(treeNode.getParentNode()){
                    var parNodes = getParsByTreeNode(treeNode);
                    for(var i in parNodes){
                        zTreeObj.checkNode(parNodes[i], false, false);
                    }
                }
            }
        };

 

var setting = {data: {simpleData: {enable: true}},check: {            enable: true            , chkStyle: 'checkbox'            //,chkboxType:{ "Y": "", "N": "" }        },callback:{onCheck:function(e,treeId, treeNode){if(treeId == "treeDemo") {var zTreeObj = $.fn.zTree.getZTreeObj(treeId);                    optParOrSon(treeId, treeNode,zTreeObj);                }}}};





本文转自博客园,原文地址:https://www.cnblogs.com/littleboyck/p/10912407.html