zoukankan      html  css  js  c++  java
  • 生成树形菜单

    题记------学习别人的精髓,并加以总结,消化吸收,这就是提高!!!

           动态生成树形菜单,前台用easyui实现,非常简单不赘述,主要给出后台java的逻辑代码

     1 package com.gongli.util.entity.treeMenu;
     2 
     3 
     4 
     5 /*
     6  * 用于封装树形菜单,无具体表与之对应,根节点为-1,节点0,然后父节点0,节点001,父节点001,节点001001,001002,001...依次类推
     7  */
     8 public class TreeMenu {
     9     private String parentId;//父节点id   
    10     private String nodeId;//后代节点id 
    11     private String perName;//权限名
    12     private String icon;//显示节点的图标
    13     private String url;//点击树形菜单的叶子节点时,请求的url路径
    14     public String getParentId() {
    15         return parentId;
    16     }
    17     public void setParentId(String parentId) {
    18         this.parentId = parentId;
    19     }
    20     public String getNodeId() {
    21         return nodeId;
    22     }
    23     public void setNodeId(String nodeId) {
    24         this.nodeId = nodeId;
    25     }
    26     public String getPerName() {
    27         return perName;
    28     }
    29     public void setPerName(String perName) {
    30         this.perName = perName;
    31     }        
    32     public String getIcon() {
    33         return icon;
    34     }
    35     public void setIcon(String icon) {
    36         this.icon = icon;
    37     }
    38     public String getUrl() {
    39         return url;
    40     }
    41     public void setUrl(String url) {
    42         this.url = url;
    43     }
    44     
    45     
    46 
    47     
    48     
    49     
    50     
    51 }
     1 package com.gongli.util.treeMenu;
     2 
     3 import java.util.List;
     4 import java.util.Map;
     5 
     6 import com.gongli.util.entity.treeMenu.TreeMenu;
     7 /**
     8  *树形菜单工具类接口
     9  *使用前缀编码,每级增加三个数字,如:第一级 001,第二级001001,第三级001001001,目录根节点为0,目录根节点的父节点为-1
    10  */
    11 public interface TreeMenuInteferce {
    12     /** 
    13      * 将树形菜单封装成树
    14      * @param list 为树形菜单 
    15      * @param fid 父id 
    16      */  
    17     public List<Map<String,Object>> createTree(List<TreeMenu> list, String fid);
    18     /** 
    19      * 递归设置TreeMenu树 
    20      * @param list 
    21      * @param fid 
    22      * @return 
    23      */  
    24     public List<Map<String, Object>> createChildren(List<TreeMenu> list, String fid);
    25     
    26 }
     1 package com.gongli.util.impl.treeMenu;
     2 
     3 import java.util.ArrayList;
     4 import java.util.HashMap;
     5 import java.util.List;
     6 import java.util.Map;
     7 
     8 import com.gongli.util.entity.treeMenu.TreeMenu;
     9 import com.gongli.util.treeMenu.TreeMenuInteferce;
    10 
    11 public class TreeMenuInteferceImpl implements TreeMenuInteferce{
    12     
    13     /** 
    14      * 将树形菜单封装成树
    15      * @param list 为树形菜单 
    16      * @param fid 父id 
    17      */ 
    18     @Override
    19     public List<Map<String,Object>> createTree(List<TreeMenu> list, String fid) {  
    20         //存放转换后数据的集合  
    21         List<Map<String,Object>> comboTreeList  = new ArrayList<Map<String,Object>>();
    22         for (int i = 0; i < list.size(); i++) {  
    23             Map<String, Object> map = null;  
    24             TreeMenu treeMenu = (TreeMenu) list.get(i);  
    25             if (treeMenu.getParentId().equals(fid)) {  
    26                 map = new HashMap<String, Object>();  
    27                 //这里必须要将对象角色的id、name转换成Tree在页面的显示形式id、text  
    28                 map.put("id", list.get(i).getNodeId());         //id  
    29                 map.put("text",list.get(i).getPerName());      //权限名  
    30                 map.put("url", list.get(i).getUrl()); 
    31                 map.put("iconCls", list.get(i).getIcon()); 
    32                 map.put("children", createChildren(list, treeMenu.getNodeId()));  
    33             }  
    34             if (map != null)  
    35                 comboTreeList.add(map);  
    36         }  
    37         return comboTreeList;
    38     }  
    39     
    40     /** 
    41      * 递归设置TreeMenu树 
    42      * @param list 
    43      * @param fid 
    44      * @return 
    45      */  
    46     @Override
    47     public List<Map<String, Object>> createChildren(List<TreeMenu> list, String fid) {  
    48         List<Map<String, Object>> childList = new ArrayList<Map<String, Object>>();  
    49         for (int j = 0; j < list.size(); j++) {  
    50             Map<String, Object> map = null;  
    51             TreeMenu treeChild = (TreeMenu) list.get(j);  
    52             if (treeChild.getParentId().equals(fid)) {  
    53                 map = new HashMap<String, Object>();  
    54                 map.put("id", list.get(j).getNodeId());  
    55                 map.put("text", list.get(j).getPerName());
    56                 map.put("url", list.get(j).getUrl()); 
    57                 map.put("iconCls", list.get(j).getIcon()); 
    58                 List<Map<String, Object>> list2 = createChildren(list, treeChild.getNodeId());
    59                 //此处一定要注意,easyui中当节点为叶子节点时并且state为closed,该叶子节点会加载整个tree,形成死循环,所以要排除
    60                 //给叶子节点赋值state:closed,但给非叶子节点赋值state:closed,则默认关闭除了第一个根节点以外的所有节点
    61                 if(list2.size()>0){
    62                     map.put("state", "closed"); 
    63                 }
    64                 map.put("children", createChildren(list, treeChild.getNodeId()));  
    65             }                
    66             if (map != null)  
    67                 childList.add(map);  
    68             }  
    69         return childList;  
    70     }  
    71     
    72 
    73 
    74 }
    希望各位大神,对文中的不足不吝赐教,共同学习,共同进步!!!
  • 相关阅读:
    Django(25)WSGIRequest对象
    Django(24)永久重定向和临时重定向
    Django(23)Django限制请求装饰器
    Django(22)Django执行SQL语句
    Django(21)migrate报错的解决方案
    Django(20)ORM模型迁移命令
    Django(19)QuerySet API
    Django(18)聚合函数
    Django(17)orm查询操作
    数据结构-用队列实现栈
  • 原文地址:https://www.cnblogs.com/gongli123/p/7265978.html
Copyright © 2011-2022 走看看