zTree 简单地实现异步加载

来源:互联网 发布:英雄无敌6加点 知乎 编辑:程序博客网 时间:2024/05/16 11:35
var tree, setting = {
            view:{selectedMulti:false},
            check:{enable:"${checked}",nocheckInherit:true},
            data:{
                simpleData:{
                    enable:true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: 0
                }
            },
            view:{
                fontCss:function(treeId, treeNode) {
                    return (!!treeNode.highlight) ? {color:"#A60000","font-weight":"bold"} : {color:"#333","font-weight":"normal"};
                }
            },
            async: {
                enable: true,
                url:url,

                autoParam:["id","pId","name"],                        //此  id 会传到后台

                dataFilter: filter //异步返回后经过Filter
            },
            callback:{beforeClick:function(id, node){
                if("${checked}" == "true"){
                    tree.checkNode(node, !node.checked, true, true);
                    return false;
                }
            },
                asyncSuccess: zTreeOnAsyncSuccess,//异步加载成功的fun
            onDblClick:function(){
                top.$.jBox.getBox().find("button[value='ok']").trigger("click");    //通过弹出框,加载树,然后选择
            }}};
$(document).ready(function(){
            // 初始化树结构
            tree = $.fn.zTree.init($("#tree"), setting,nodeList);
});
        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('','');
            }
            return childNodes;
        }
        function zTreeOnAsyncSuccess(event, treeId, treeNode, msg){
            alert("加载成功");

        }


<div id="tree" class="ztree" style="padding:15px 20px;"></div>


上面url到获取数据  

OfficeController.java

if(StringUtils.isEmpty(id)){
            id="0";                                      //初始化时根节点 id为0
        }
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<Office> list = officeService.findAllByID(id);
        for (int i=0; i<list.size(); i++){
            Office e = list.get(i);
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", e.getId());
            map.put("pId", id);
            map.put("name", e.getName());
            map.put("isParent",true);                     //由于自己建实体中没有这个字段,所以这里全部设为true,不然无法加载子节点
            mapList.add(map);
        }
        return mapList;


OfficeService类

public List<Office> findAllBySubID(String id){
        List<Office> officeList  = Lists.newArrayList();
        DetachedCriteria dc = officeDao.createDetachedCriteria();
        dc.add(Restrictions.eq("parent.id", id));
        officeList = officeDao.find(dc);
        return officeList;
    }

0 0