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);
}
}
};