欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ztree选父不选子选子不选父

程序员文章站 2023-04-06 16:34:05
ztree ......
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);
                    }
                }
            }
        };