使用Ztree
时,treeNode.children
只能获取到子节点,该如何拿到节点的叶子节点呢?
创建初始化设置
var setting = { data: { simpleData: { enable: true } }, callback: { onCheck:onCheck, } };
|
创建点击响应事件
function onCheck(event, treeId, treeNode, clickFlag) { var treeObj = $(ELT).fn.zTree.getZTreeObj("datarecdirs"); var str = ""; str = getAllChildNodes(treeNode,str); var ids = str.substring(1, str.length); var idsArray = ids.split(','); var filterArr = idsArray.filter(function(n){return n}); var leafNodesLen = filterArr.length; if(filterArr){ var nodeChecked = treeNode.checked; if(nodeChecked){ for (var i=0; i < leafNodesLen; i++) { var idVal = filterArr[i]; var node = treeObj.getNodeByParam("id", idVal, null); treeObj.setChkDisabled(node,true,false,true); } }else{ for (var j=0; j < leafNodesLen; j++) { idVal = filterArr[j]; node = treeObj.getNodeByParam("id", idVal, null); treeObj.setChkDisabled(node,false,false,true); } var nodes = treeObj.getCheckedNodes(); console.log(nodes); if (nodes.length>0) { for(var c=0;c<nodes.length;c++){ treeObj.checkNode(nodes[c],false,true); } } } } }
function getAllChildNodes(treeNode,result){ if (treeNode.isParent) { var childrenNodes = treeNode.children; if (childrenNodes) { for (var i = 0; i < childrenNodes.length; i++) { result += ',' + childrenNodes[i].id; result = getAllChildNodes(childrenNodes[i], result); } } } return result; }
|
注意
zTree 里严格区分了选中和勾选这两个概念,选中是指节点被选择背景颜色有变化,因此 cancelSelectedNode()只是把你选择的节点,变成不选择状态,也就是节点的背景色发生变化。
而勾选是指节点的勾选框被选中,你要将节点的勾选状态由勾选变为不勾选,就不能使用 cancelSelectedNode()方法,只能使用 checknode()方法!
参考来源