项目中查询菜单目录表,里面有父子关系,根据父子关系,把菜单目录表展示成树的形式
查询出所有的菜单目录内容
SysUserMVO sessionUser = SecurityManager.getSessionUser();
List<MenuCatalogMVO> catalogs = sessionUser.getMenuCatalog();
将数据根据父子关系展示成json格式
Map<String,Object> leafMap = getLevelMenuCatalog(catalogs,leafNodeIds,MENU_LV); public Map<String, Object> getLevelMenuCatalog(List<MenuCatalogMVO> catalogs, List<String> parentCatalogIds, String... catalogCodes) { List<Object> treeList = new LinkedList<Object>(); List<String> leafNodeId = new LinkedList<String>(); Map<String, Object> rsMap = new HashMap<String, Object>(); for (MenuCatalogMVO menuCatalog : catalogs) { if ((parentCatalogIds != null && parentCatalogIds.size() > 0) ? parentCatalogIds.contains(menuCatalog.getParentCatalogId()) : StringUtils.isBlank(menuCatalog.getParentCatalogId())) { if (catalogCodes.length > 0 && Arrays.asList(catalogCodes).contains(menuCatalog.getCatalogCode())) { Map<String, Object> treeMap = new HashMap<String, Object>(); treeMap.put("id", menuCatalog.getMenuCatalogId()); treeMap.put("text", menuCatalog.getCatalogName()); treeMap.put("code", menuCatalog.getCatalogCode()); treeMap.put("parentId", menuCatalog.getParentCatalogId()); childMenuCatalog(treeMap, catalogs, catalogCodes, leafNodeId); treeList.add(treeMap); } } } rsMap.put("LEAFNODEIDS", leafNodeId); JSONArray jo = JSONArray.fromObject(treeList); String rs = jo.toString(); rsMap.put("RS", rs); return rsMap; } public void childMenuCatalog(Map<String, Object> parentMap, List<MenuCatalogMVO> catalogs, String[] catalogCodes, List<String> leafNodeId) { List<Object> treeList = new LinkedList<Object>(); for (MenuCatalogMVO menuCatalog : catalogs) { Map<String, Object> treeMap = new HashMap<String, Object>(); if (catalogCodes.length > 0 && Arrays.asList(catalogCodes).contains(menuCatalog.getCatalogCode())) { if (parentMap.get("id").equals(menuCatalog.getParentCatalogId())) { treeMap.put("id", menuCatalog.getMenuCatalogId()); treeMap.put("text", menuCatalog.getCatalogName()); treeMap.put("code", menuCatalog.getCatalogCode()); treeMap.put("parentId", menuCatalog.getParentCatalogId()); childMenuCatalog(treeMap, catalogs, catalogCodes, leafNodeId); treeList.add(treeMap); } } } if (treeList.size() > 0) { parentMap.put("children", treeList); } else { leafNodeId.add((String) parentMap.get("id")); } }
catalog = (String) leafMap.get("RS");
这样打印出的catalog就是有层级关系的了