欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

zTree异步加载全部节点

程序员文章站 2022-03-30 20:41:56
...
引用的头部文件代码

<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/component/ztree/css/zTreeStyle/zTreeStyle.css" />

<script type="text/javascript" src="<%=request.getContextPath()%>/component/ztree/js/jquery-1.4.4.min.js"></script>

<script type="text/javascript" src="<%=request.getContextPath()%>/component/ztree/js/jquery.ztree.core.js"></script>

<script type="text/javascript" src="<%=request.getContextPath()%>/component/ztree/js/jquery.ztree.excheck.js"></script>

树的载体代码

<div style="border:solid 1px #a7b5bc; float:left; width:100%; height:100%; overflow-y:auto;">  
    <ul id="tree" class="ztree"></ul>  
</div>

Javascript部分(本文重点)代码

<script type="text/javascript">  
    //初始化树  
    $(function() {  
        $zTree = $.fn.zTree.init($("#tree"), $setting);  
          
        //延迟展开根节点  
        setTimeout(function(){  
            var nodes = $zTree.getNodes();  
            if(nodes.length >0){  
                $zTree.expandNode($zTree.getNodes()[0], true, false, false);  
            }  
        },1000);  
    });  
      
    //定义ztree树所需变量  
    var $zTree;  
    var $zTreeFlag = true;  
    var $setting = {  
        async : {  
            enable : true,  
            url : "/baseplatform/auth/selectSysRoleUserTreeByPid.do",  
            autoParam : ["id"],  
            otherParam: {"srId":'${sysRole.srId}'}  
        },  
        callback : {  
            onAsyncSuccess : onAsyncSuccess  
        },  
        data : {  
            key : {name : "name"},  
            simpleData : {enable : true, idKey : "id", pIdKey : "pId", rootPId : -1}  
        },  
        check: {  
            enable: true  
        }  
    };  
      
    //每次加载节点触发的方法  
    function onAsyncSuccess(event, treeId, treeNode, msg) {  
        //递归展开子节点  
        if(treeNode != undefined){  
            expandNodes(treeNode.children, "tree");  
        }  
    }  
              
    //递归展开子节点  
    function expandNodes(nodes, treeId) {  
        if (!nodes) return;  
        var zTree = $.fn.zTree.getZTreeObj(treeId);  
        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, treeId);  
            }  
        }  
    }  
</script>