前端html代码
复制代码
前端js代码
//增add: function () { $('#SysResourceEdit').dialog({ href: SysResource.URL.inputUI(), onLoad: function () { $('#parentResource').combotree({ onSelect: function (record) { }, onChange: function (a,b) { }, url: SysResource.URL.tree(), method: 'get', value:'0', panelHeight: 'auto' }); } }) .dialog("open");}复制代码
后台Java核心代码
@RequestMapping("/tree")public List tree(){ List menuList = sysMenuService.queryList(); List trees = ResourceUtil.prepareMenuToTree(menuList); Tree tree = new Tree(); tree.setText("一级目录"); tree.setId(0L); tree.setPid(-1L); tree.setIsLeaf(0); tree.setChildren(new ArrayList<>()); trees.add(0,tree); return trees;} public static List prepareMenuToTree(List resourceList) { if(resourceList == null || resourceList.size() == 0){ return new ArrayList<>(); } //目录集合 List catalogMenuList = new ArrayList<>(); //菜单集合 List menuList = new ArrayList<>(); //按钮集合 List buttonList = new ArrayList<>(); resourceList.forEach(sysMenuEntity->{ Tree menuTree = menuToTree(sysMenuEntity); //类型 Integer type = menuTree.getIsLeaf(); //目录 if(type == Constant.MenuType.CATALOG.getValue() ){ catalogMenuList.add(menuTree); } //菜单 if(type == Constant.MenuType.MENU.getValue()){ menuList.add(menuTree); } //按钮 if(type == Constant.MenuType.BUTTON.getValue() ){ buttonList.add(menuTree); } }); catalogMenuList.forEach(catalogMenu->{ long catalogId = catalogMenu.getId(); //目录的孩子 List catalogChildren = catalogMenu.getChildren(); for(Iterator second = menuList.iterator(); second.hasNext();){ Tree menu = second.next(); long menuId = menu.getId(); //菜单的孩子 List menuChildren = menu.getChildren(); if(catalogId == menu.getPid()){ catalogChildren.add(menu); for(Iterator buttonTree = buttonList.iterator(); buttonTree.hasNext();){ Tree button = buttonTree.next(); long pid = button.getPid(); if(menuId == pid){ menuChildren.add(button); buttonTree.remove(); } } second.remove(); } if(menuChildren!=null && menuChildren.size() != 0){ menu.setState("closed"); } } if(catalogChildren!=null && catalogChildren.size() != 0){ catalogMenu.setState("closed"); } }); menuList.clear(); buttonList.clear(); return catalogMenuList;} private static Tree menuToTree(SysMenuEntity resource) { Tree tree = new Tree(); tree.setId(resource.getMenuId()); tree.setText(resource.getName()); tree.setIconCls(resource.getIcon()); tree.setIsLeaf(resource.getType()); tree.setPid(resource.getParentId()); /*tree.setAttributes(resource);*/ return tree;}public class Tree implements Serializable { /*节点的 id*/ private Long id; /*要显示的节点文本*/ private String text; private int seq; /*节点状态,'open' 或 'closed',默认是 'open'*/ private String state = "open";// open,closed /*指示节点是否被选中*/ private boolean checked = false; /*定义了一些子节点的节点数组*/ private List children = new ArrayList<>(); /*用来显示图标的 css class*/ private String iconCls; private Long pid; /*把指定的节点定义成叶节点*/ private Integer isLeaf;}复制代码
最终效果: