如果是同步加载的模式使用一句函数:
treeObj.expandAll(true);
如果为异步模式,此方式不可用。
以业务代码为例:
$.typeTree = $.fn.zTree.init($("#typeTrees"), {
async : {
enable : true,
url : "/backend/content/getlayout?columnId="+$('#rootColumn').val(),
autoParam : ["id", "type"]
},
data : {
simpleData : {
enable : true,
idKey : "id",
pIdKey : "pId",
rootPId : 0
}
},
callback : {
onExpand : function(event, treeId, treeNode){
if(treeNode.type == "layout"){
$.layoutid = treeNode.id;
$.pageid = treeNode.getParentNode().id;
$("#searchBtn").click();
}
else{
$.layoutid = -1;
$.pageid = -1;
}
},
onClick : function(event, treeId, treeNode, clickFlag) {
if(treeNode.type == "layout"){
$.layoutid = treeNode.id;
$.pageid = treeNode.getParentNode().id;
$("#searchBtn").click();
}
else{
$.layoutid = -1;
$.pageid = -1;
}
},
beforeAsync: beforeAsync, // 重点在这
onAsyncSuccess: onAsyncSuccess // 重点在这
}
});
// 延时加载
setTimeout(function(){
expandAll("typeTrees");
},100);
需要使用beforeAsync属性和onAsyncSuccess属性
具体函数以及参数定义:
var curStatus = "init", curAsyncCount = 0, goAsync = false;
function beforeAsync() {
curAsyncCount++;
}
function onAsyncSuccess(event, treeId, treeNode, msg) {
curAsyncCount--;
if (curStatus == "expand") {
expandNodes(treeNode.children);
}
if (curAsyncCount <= 0) {
curStatus = "";
}
}
function expandAll() {
if (!check()) {
return;
}
var zTree = $.fn.zTree.getZTreeObj("typeTrees");
expandNodes(zTree.getNodes());
if (!goAsync) {
curStatus = "";
}
}
function expandNodes(nodes) {
if (!nodes) return;
curStatus = "expand";
var zTree = $.fn.zTree.getZTreeObj("typeTrees");
for (var i=0, l=nodes.length; i<l; 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;
}