Java中使用List递归生成下拉列表树

来源:互联网 发布:网络广播电台在线收听 编辑:程序博客网 时间:2024/05/22 08:45

先看看效果

示例图片

 

bean类:

 1 package com.lemsun.jtreport.beans.workflow;
 2 
 3 /**
 4  * SysAuditotypecategory entity.
 5  *
 6  * @author MyEclipse Persistence Tools
 7  */
 8 
 9 public class SysAuditotypecategory implements java.io.Serializable {
10 
11     // Fields
12 
13     private Long id;
14     private String categoryname;
15     private SysAuditotypecategory parent;
16     // Constructors
17 
18     /** default constructor */
19     public SysAuditotypecategory() {
20     }
21 
22     /** full constructor */
23     public SysAuditotypecategory(String categoryname) {
24         this.categoryname = categoryname;
25         this.parent = new SysAuditotypecategory();
26     }
27 
28     // Property accessors
29 
30     public Long getId() {
31         return this.id;
32     }
33 
34     public void setId(Long id) {
35         this.id = id;
36     }
37 
38     public String getCategoryname() {
39         return this.categoryname;
40     }
41 
42     public void setCategoryname(String categoryname) {
43         this.categoryname = categoryname;
44     }
45 
46     public SysAuditotypecategory getParent() {
47         return this.parent;
48     }
49 
50     public void setParent(SysAuditotypecategory parent) {
51         this.parent = parent;
52     }
53 
54 }

action中生成下拉树的代码

 1     //=================begin-取得类型树=================
 2     //传入所有的列表节点
 3     private List<SysAuditotypecategory> cTreeList(List<SysAuditotypecategory> aList){
 4         List<SysAuditotypecategory> returnList = new ArrayList<SysAuditotypecategory>();
 5         List<SysAuditotypecategory> topList = new ArrayList<SysAuditotypecategory>();
 6         List<SysAuditotypecategory> tempList = new ArrayList<SysAuditotypecategory>();
 7 
 8         //筛选出顶级节点
 9         for (SysAuditotypecategory sacty : aList) {
10             if(sacty.getParent()==null){
11                 topList.add(sacty);//是顶级节点
12             }else {
13                 tempList.add(sacty);//不是顶级节点
14             }
15         }
16         
17         //获取子级节点列表
18         for (int i=0;i<topList.size();i++) {
19                 SysAuditotypecategory sacty = topList.get(i);
20                 returnList.add(cTreeNode(sacty, "", (i+1<topList.size())));//添加自身
21 
22                 //添加所有子级,自身如果不是顶级节点列表的最后一个前面用“│”打头,如果是则用“…”打头。
23                 returnList.addAll(cTreeNodeList(tempList,sacty.getId(),(i+1<topList.size())?"":""));
24             }
25         return returnList;
26     }
27 
28     /**
29     * 按照父节点取得所有子节点
30     * @inList 所有不是顶级节点的列表
31     * @parentid 顶级节点的id
32     * @headString 前缀字符
33     */
34     private List<SysAuditotypecategory> cTreeNodeList(List<SysAuditotypecategory> inList,Long parentid,String headString){
35         List<SysAuditotypecategory> tempList1 = new ArrayList<SysAuditotypecategory>();
36         List<SysAuditotypecategory> tempList2 = new ArrayList<SysAuditotypecategory>();
37         List<SysAuditotypecategory> returnList = new ArrayList<SysAuditotypecategory>();
38 
39         //取得兄弟节点和不是兄弟节点
40         for(int i=0;i<inList.size();i++){
41             SysAuditotypecategory sacty = inList.get(i);
42             if(sacty.getParent().getId() == parentid){
43                 tempList1.add(sacty);//兄弟节点
44             }else {
45                 tempList2.add(sacty);//子节点
46             }
47         }
48 
49         if(tempList1.size()>0){
50             for(int i=0;i<tempList1.size();i++){
51                 SysAuditotypecategory sacty1 = tempList1.get(i);
52                 //返回列表添加自身
53                 returnList.add(cTreeNode(sacty1, headString, (i+1<tempList1.size())));
54                 
55                 //子节点个数不为0这递归这个方法。
56                 if(tempList2.size()>0){
57                     returnList.addAll(cTreeNodeList(tempList2,sacty1.getId(),headString+((i+1<tempList1.size())?"":"")));
58                 }
59             }
60         }
61 
62         return returnList;
63     }
64 
65     private SysAuditotypecategory cTreeNode(SysAuditotypecategory satcy,String headString,boolean havenext) {
66         satcy.setCategoryname(headString.concat((havenext?"":""))+satcy.getCategoryname());//添加本身,不是最末节点在前面添加"├",只最末节点这添加"└"
67         return satcy;
68     }
69     //=================end-取得类型树=================

原创粉丝点击