工作知識總結12
阿新 • • 發佈:2020-07-22
專案中查詢選單目錄表,裡面有父子關係,根據父子關係,把選單目錄表展示成樹的形式
查詢出所有的選單目錄內容
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就是有層級關係的了