zTree 简单地实现异步加载

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值