如果父节点数量很大,请注意利用延时进行控制,避免异步进程过多。因此需要用延迟加载的方式控制。
var setting = {
async: {
enable: true,
url:"../ajaxGetNodes.php",
autoParam:["id", "name=n", "level=lv"],
otherParam:{"otherParam":"zTreeAsyncTest"},
dataFilter: filter,
type: "get"
},
callback: {
beforeAsync: beforeAsync,
onAsyncSuccess: onAsyncSuccess
}
};
function filter(treeId, parentNode, childNodes) {
if (!childNodes) return null;
for (var i=0, l=childNodes.length; i
childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
}
return childNodes;
}
function beforeAsync() {
curAsyncCount++;
}
function onAsyncSuccess(event, treeId, treeNode, msg) {
curAsyncCount--;
if (curStatus == "expand") {
expandNodes(treeNode.children);
} else if (curStatus == "async") {
asyncNodes(treeNode.children);
}
if (curAsyncCount <= 0) {
curStatus = "";
}
}
var curStatus = "init", curAsyncCount = 0, goAsync = false;
function expandAll() {
if (!check()) {
return;
}
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
expandNodes(zTree.getNodes());
if (!goAsync) {
curStatus = "";
}
}
function expandNodes(nodes) {
if (!nodes) return;
curStatus = "expand";
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
for (var i=0, l=nodes.length; i
zTree.expandNode(nodes[i], true, false, false);//展开节点就会调用后台查询子节点
if (nodes[i].isParent && nodes[i].zAsync) {
expandNodes(nodes[i].children);//递归
} else {
goAsync = true;
}
}
}
function check() {
if (curAsyncCount > 0) {
return false;
}
return true;
}
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"), setting);
setTimeout(function(){
expandAll("treeDemo");
},1000);//延迟加载
});