首页 > 代码库 > java递归实现easyui树节点

java递归实现easyui树节点

1. [代码]easyui的Tree节点JSON格式     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[{   
    "id":1,   
    "text":"Folder1",   
    "iconCls":"icon-save",   
    "children":[{   
        "text":"File1",   
        "checked":true  
    },{   
        "text":"Books",   
        "state":"open",   
        "attributes":{   
            "url":"/demo/book/abc",   
            "price":100   
        },   
        "children":[{   
            "text":"PhotoShop",   
            "checked":true  
        },{   
            "id": 8,   
            "text":"Sub Bookds",   
            "state":"closed"  
        }]   
    }]   
},{   
    "text":"Languages",   
    "state":"closed",   
    "children":[{   
        "text":"Java"  
    },{   
        "text":"C#"  
    }]   
}] 

2. [代码][Java]代码     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * 树 json model 数据
 * @author glq
 *
 */
public class JsonTreeData {
 
    public String id;       //json id
    public String pid;      //
    public String text;     //json 显示文本
    public String state;    //json ‘open‘,‘closed‘
    public List<JsonTreeData> children;       //
     
    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 String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public List<JsonTreeData> getChildren() {
        return children;
    }
    public void setChildren(List<JsonTreeData> children) {
        this.children = children;
    }
    public String getPid() {
        return pid;
    }
    public void setPid(String pid) {
        this.pid = pid;
    }
}

3. [代码][Java]代码     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @ClassName: TreeNodeUtil
* @Description 描述: 获取树节点集合(这里用一句话描述这个类的作用)
* @author: 青柠loft www.rmworking.com/blog
* @date 最后修改时间: 2015年6月9日 下午6:39:28
*
*/
public class TreeNodeUtil {
 
    /**
    * @Title: getfatherNode
    * @Description 方法描述: 父节点
    * @param 设定文件: @param treeDataList
    * @param 设定文件: @return   
    * @return 返回类型:List<JsonTreeData>   
    * @throws
    * @date 最后修改时间:2015年6月9日 下午6:39:26
    */
    public final static List<JsonTreeData> getfatherNode(List<JsonTreeData> treeDataList) {
        List<JsonTreeData> newTreeDataList = new ArrayList<JsonTreeData>();
        for (JsonTreeData jsonTreeData : treeDataList) {
            if(jsonTreeData.getPid() == null) {
                //获取父节点下的子节点
                jsonTreeData.setChildren(getChildrenNode(jsonTreeData.getId(),treeDataList));
                jsonTreeData.setState("open");
                newTreeDataList.add(jsonTreeData);
            }
        }
        return newTreeDataList;
    }
     
    /**
    * @Title: getChildrenNode
    * @Description 方法描述: 子节点
    * @param 设定文件: @param pid
    * @param 设定文件: @param treeDataList
    * @param 设定文件: @return   
    * @return 返回类型:List<JsonTreeData>   
    * @throws
    * @date 最后修改时间:2015年6月9日 下午6:39:50
    */
    private final static List<JsonTreeData> getChildrenNode(String pid , List<JsonTreeData> treeDataList) {
        List<JsonTreeData> newTreeDataList = new ArrayList<JsonTreeData>();
        for (JsonTreeData jsonTreeData : treeDataList) {
            if(jsonTreeData.getPid() == nullcontinue;
            //这是一个子节点
            if(jsonTreeData.getPid().equals(pid)){
                //递归获取子节点下的子节点
                jsonTreeData.setChildren(getChildrenNode(jsonTreeData.getId() , treeDataList));
                newTreeDataList.add(jsonTreeData);
            }
        }
        return newTreeDataList;
    }
}

4. [代码][Java]代码     跳至 [1] [2] [3] [4] [全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
*serviceImpl 业务实现
*/
public List<JsonTreeData> getGoodsSpec(HtSpecifications spec) {
        List<HtSpecifications> resultList = htSpecificationsDaoImpl.findAll("getSpecList", spec);
        List<JsonTreeData> treeDataList = new ArrayList<JsonTreeData>();
         /*为了整理成公用的方法,所以将查询结果进行二次转换。
          * 其中specid为主键ID,varchar类型UUID生成
          * parentid为父ID
          * specname为节点名称
          * */
        for (HtSpecifications htSpecifications : resultList) {
            JsonTreeData treeData = http://www.mamicode.com/new JsonTreeData();
            treeData.setId(htSpecifications.getSpecid());
            treeData.setPid(htSpecifications.getParentid());
            treeData.setText(htSpecifications.getSpecname());
            treeDataList.add(treeData);
        }
        //最后得到结果集,经过FirstJSON转换后就可得所需的json格式
        List<JsonTreeData> newTreeDataList = TreeNodeUtil.getfatherNode(treeDataList);
        return newTreeDataList;
    }

java递归实现easyui树节点