目录
- 取消选中和取消勾选所有节点
- 全部选中所有的节点
- 修改节点
- 删除节点
- 得到当前节点的父节点
- 自动点击某节点
- 找到当前节点的根节点
- 递归查询当前节点下面的全部子节点
- 得到树的所有叶子结点id列表
- 设置不允许勾选父节点
- checkbox设置父子联动
- 取消选中某个节点
取消选中和取消勾选所有节点
var treeObj = $.fn.zTree.getZTreeObj("layerGroupTree");
treeObj.cancelSelectedNode();
treeObj.checkAllNodes(false);
全部选中所有的节点
var treeObj = $.fn.zTree.getZTreeObj("layerGroupTree");
var node = treeObj.getNodes();
var nodes = treeObj.transformToArray(node);
var checkNode = treeObj.getCheckedNodes().length;
if (checkNode < nodes.length) {
treeObj.checkAllNodes(true);
} else{
treeObj.checkAllNodes(false);
}
修改节点
var treeObj = $.fn.zTree.getZTreeObj("layerGroupTree");
var treeNode = treeObj.getNodeByParam("id", node.id);
treeNode.name = node.name;
treeNode.type = node.type;
treeObj.updateNode(treeNode);
删除节点
var treeObj = $.fn.zTree.getZTreeObj("layerGroupTree");
var treeNode = treeObj.getNodeByParam("id", node.id);
treeObj.removeNode(treeNode);
得到当前节点的父节点
var treeObj = $.fn.zTree.getZTreeObj("layerGroupTree");
var treeNode = treeObj.getNodeByParam("id", node.id);
var parentNode = treeNode.getParentNode();
自动点击某节点
var treeObj = $.fn.zTree.getZTreeObj("layerGroupTree");
var treeNode = treeObj.getNodeByParam("id", node.id);
$("#" + treeNode.tId + "_a").click();
找到当前节点的根节点
function findRoot(treeNode) {
var parentNodes = treeNode.getPath();
for(var key in parentNodes){
if(parentNodes[key].level == 0){
console.log("当前节点的跟节点:" + JSON.stringify(parentNodes[key]));
return parentNodes[key];
}
}
}
递归查询当前节点下面的全部子节点
function getZTreeChildrenNode(treeNode, result) {
if (treeNode.children != null) {
var childrenNodes = treeNode.children;
if (childrenNodes) {
for (var i = 0; i < childrenNodes.length; i++) {
result.push(childrenNodes[i]);
result = getzTreeChildrenNode(childrenNodes[i], result);
}
}
}
return result;
}
得到树的所有叶子结点id列表
function getAllLeafNodeId(treeObj) {
var leftIdList = new Array();
$.each(treeObj.transformToArray(treeObj.getNodes()), function (key, node) {
if (node.children == null || node.children.length == 0) {
leftIdList.push(node.id);
}
});
return leftIdList;
}
设置不允许勾选父节点
var setting = {
callback: {
beforeCheck: zTreeBeforeCheck
}
}
function zTreeBeforeCheck(treeId, treeNode) {
return !treeNode.isParent;
}
checkbox设置父子联动
var setting = {
check: {
enable: true,
chkStyle: "checkbox",
chkboxType: {"Y": "ps", "N": "ps"}
}
}
取消选中某个节点
var treeObj = $.fn.zTree.getZTreeObj("layerGroupTree");
var treeNode = treeObj.getNodeByParam("id", node.id);
treeObj.checkNode(treeNode, false, false, false);