Easyui-Tree和Combotree使用注意事项-sunziren

  版权声明:本文为sunziren原创文章,博客园首发,转载务必注明出处以及作者名称。


  Easyui-Tree和Combotree所使用的数据结构是类似的,在我的上一篇文章《Easyui-Treegrid使用注意事项-sunziren》中,提到了两种数据格式。第一种数据间是平级的,第二种是包含关系,子数据被包在父数据的children属性中。

  Tree和Combotree使用的就是第二种数据格式。本文着重记录一下如何生成第二种数据格式。

  1. 首先需要定义一个工具类:TreeBeen。这个类相当于一条完整的数据。包括了属性:id、text、attributes、children、state这五个属性。其中attributes属性用来存储该条数据的其他属性;children为当前数据的子数据,格式为List<TreeBean>;state表示当前节点默认是否展开,你可以给个默认值“open”。

  TreeBeen的源代码如下,很简单,值得注意的是,他的children属性List的泛型正好是TreeBeen类型。

package com.basefrm.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TreeBean {
    private String id;
    private String text;
    private Map<String,String> attributes = new HashMap<String,String>();
    private List<TreeBean> children = new ArrayList<TreeBean>();
    private String state = "open";
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public Map<String, String> getAttributes() {
        return attributes;
    }
    public void setAttributes(Map<String, String> attributes) {
        this.attributes = attributes;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public TreeBean(String id, String text,
            Map<String, String> attributes, String state) {
        super();
        this.id = id;
        this.text = text;
        this.attributes = attributes;
        this.state = state;
    }
    public TreeBean() {
        super();
    }
    public List<TreeBean> getChildren() {
        return children;
    }
    public void setChildren(List<TreeBean> children) {
        this.children = children;
    }
}

  2. 我们知道,前台需要的是Json格式的字符串,我们首先需要把包含层级关系的数据从数据库查出来,3个字段是必须的。id,pid,text。其他字段查出来可以放到TreeBeen的attributes属性中。而且我们注意到,要拼这个json格式的字符串,我们肯定是需要循环的,但是我们不知道整个数据一共包括几个层级,因此我们不知道具体设置几个嵌套循环,因此这个我们需要一个递归的方法。先放代码吧,然后我在解释一下。

    //构建树的方法(递归)
    private List<TreeBean> buildTree(List<Map> all_Bm,String sjbmbm,String pid,String id,String text){
        List<TreeBean> list = new ArrayList<TreeBean>();
        for(int i=0;i<all_Bm.size();i++){
            if(all_Bm.get(i).get(pid).equals(sjbmbm)){
                TreeBean tb = new TreeBean();
                tb.setId(all_Bm.get(i).get(id).toString());
                tb.setText(all_Bm.get(i).get(text).toString());
                tb.setChildren(buildTree(all_Bm,tb.getId(),pid,id,text));
                list.add(tb);
            }
        }
        return list;
    }
    
    //获取部门Json
    public void getSzbm(){
        String sql = "select bmbm,sjbmbm,bmmc from table_bm";
        List<Map> all_Bm = (List)baseDao.loadBySql(sql, null);
        List<TreeBean> list=buildTree(all_Bm,"-1","SJBMBM","BMBM","BMMC");
        System.out.println(JSON.toJSONString(list));
        this.printToJson(JSON.toJSONString(list));
    }    

   方法buildTree是一个递归方法,他在内部调用了自己。参数List<Map> all_Bm为数据库查出来的所有数据,String sjbmbm为你要展示的树的根目录的那条数据的pid。后面三个参数分别为id,pid,text,在查出来的数据中的名称。通过这个办法,我成功了实现了tree和combotree。

猜你喜欢

转载自www.cnblogs.com/sunziren/p/10576368.html