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

先看看效果

示例图片

 

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-取得类型树=================

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyxu123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值