ztree选父不选子选子不选父
程序员文章站
2022-05-18 23:01:10
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); } } } };
下一篇: html+php上传图片文件到服务器