zTree异步加载并初始化树时全部展开

2018-07-12 网页设计
  • 文章介绍
如果父节点数量很大,请注意利用延时进行控制,避免异步进程过多。因此需要用延迟加载的方式控制。

<link rel="stylesheet" href="css/demo.css" type="text/css">
<link rel="stylesheet" href="css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.ztree.core-3.5.js"></script>
<SCRIPT type="text/javascript">
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<l; 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<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;
}
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"), setting);
setTimeout(function(){
expandAll("treeDemo");
},1000);//延迟加载
});
</SCRIPT>

上一篇:仿造w3school的试一试功能,实现左侧编辑框,右侧效果页面

下一篇:js弹出窗口简单实现代码

跟版网模板,累计帮助5000+客户企业成功建站,为草根创业提供助力!

立刻开启你的建站之旅