var zTree = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
var setting = {
check: {
enable: true,
chkStyle: "checkbox",
chkboxType: {
"Y": "ps",
"N": "s"
}
},
data: {
simpleData: {
enable: true
}
},
view: {
showIcon: false
},
callback: {
onClick: function zTreeOnClick(event, treeId, treeNode) {
var checkedFlag = treeNode.checked;
if (checkedFlag) {
// 如果已经选中,则取消选中状态
setChildrenCheckedState(treeNode, false);
} else {
// 如果没有选中,则设置为选中状态
setChildrenCheckedState(treeNode, true);
}
}
}
};
//设置选中的节点treeNode下所有的子节点的选中状态
function setChildrenCheckedState(treeNode, flag) {
// 设置被点击的节点选中状态
treeNode.checked = flag;
// 需要更新才能正确显示更新后的状态
zTree.updateNode(treeNode);
if (treeNode.isParent) {
for (var obj in treeNode.children) {
treeNode.children[obj].checked = flag;
zTree.updateNode(treeNode.children[obj]);
}
}
}
原文地址:http://blog.51cto.com/1197822/2157672