miniui树 动态加载(懒加载) 实例

前台jsp:

<div class="mini-fit">
        
    id="resourceTree" class="mini-tree" style="width:100%;height:95%;" onbeforeload="onBeforeTreeLoad" showTreeIcon="true" textField="text" idField="id" resultAsTree="false" expandOnLoad="true" onnodeclick="onclicknode" url="/dabot/dsm/dataSourceManager/sourceList">
div>

其中,动态加载主要依靠的是onBeforeTreeLoad方法
js中方法为

function onBeforeTreeLoad(e){//动态加载树

    var tree = e.sender;    //树控件
    var node = e.node;      //当前节点
    var params = e.params;  //参数对象
    var levelnode =mini.get("#resourceTree").getAncestors(e.node);//获取该节点所有父节点
    var nodepath="";
    if(levelnode.length==0){nodepath=e.node.text}
    else{//对路径进行拼接 ,用来为后台判断节点位置
        for(i=0;i"/";
        }
        nodepath=nodepath+e.node.text;
    }
    console.log(nodepath);
    //可以传递自定义的属性
    var id=null;
    if(levelnode.length>0){id=levelnode[0].id;}
    else{id="0";}
    params.id = id; //后台获取数据源id
    params.nodepath=nodepath;//后台获取nodepath
}

后台java代码

public String sourceList() {
        HttpServletRequest request = ActionContext.getActionContext().getHttpServletRequest();
        String nodepath=request.getParameter("nodepath");//
        String id=request.getParameter("id");//数据源id
        System.out.println(nodepath);
        if(nodepath==null){
            ProcessResult<JSON> sourceList=this.getSourceListTree();
            return sourceList.getData().toString();
        }else if (nodepath.contains("query")) {
            ProcessResult<JSON> sourceList=this.getQueryList(id);
            return sourceList.getData().toString();
        } 
        else {
            ProcessResult<JSON> sourceList=treeNodeLoader.tree(null, nodepath);
            return sourceList.getData().toString();
        }

    }

其中 pathnode为null时,调用的方法如下

public ProcessResult getSourceListTree() {
        String sql="select * from dsm02"; 
        List> list=this.jdbcTemplate.queryForList(sql);
        JSONArray jsons = new JSONArray();
        for(int i=0;inew JSONObject();
            json.element("type", "sql");//数据库类型
            json.element("id", list.get(i).get("id"));
            json.element("text", list.get(i).get("linkname"));
            //json.element("iconCls", "/dabot/dsm/images/database.png");
            json.element("isLeaf", false);
            json.element("expanded", false);//LK添加
            jsons.add(json);
        }
        ProcessResult pr = new ProcessResult(true);
        pr.setData(jsons);
        return pr;
    }

注意,写返回json值的时候,这里isLeaf和expanded一定要写false,否则是子节点,无法展开。
最终实现效果图
miniui树 动态加载(懒加载) 实例_第1张图片

你可能感兴趣的:(工具使用,miniui)