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); } } } };
//展開選中節點
var expandCheckedNode = function(){
//獲取所有選中狀態的節點
var checkedNodes = zTreeObj.getCheckedNodes(true);
for(var i in checkedNodes){
var checkedParAll = getParsAll(checkedNodes[i]);
for(var j in checkedParAll){
zTreeObj.expandNode(checkedParAll[j],true,false);
}
}
};