ztree 更新配置后重新渲染树_zTree异步加载并初始化树时全部展开

如果父节点数量很大,请注意利用延时进行控制,避免异步进程过多。因此需要用延迟加载的方式控制。

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);//延迟加载

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值