@RequestMapping("/loadTreeDepartment") public void loadTreeDepartment(HttpServletResponse response) { Print print = new Print(); List<TBaseDepartment> allDepartments = baseService.loadAllDepartment();查询所有菜单
流化后过滤出根菜单 List<TBaseDepartment> departments = allDepartments.stream().filter(x -> x.getFdepcode() == null || x.getFdepcode().equals("")).collect(Collectors.toList()); List<Map<String, Object>> rightDataTree = getChildren(allDepartments,departments);根据根菜单找出叶子菜单 print.WriteJson(response, rightDataTree); } private List<Map<String, Object>> getChildren(List<TBaseDepartment> listAll, List<TBaseDepartment> pList) { List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); for (TBaseDepartment department : pList) { Map<String, Object> child = new HashMap<String, Object>(); child.put("id", department.getDepcode()); child.put("text", department.getDepname()); child.put("fid", department.getFdepcode()); child.put("note", department.getNote());
找出每个根菜单的叶子节点 List<TBaseDepartment> childList = listAll.stream().filter(x -> x.getFdepcode() != null && StringUtils.equals(x.getFdepcode(), department.getDepcode())).collect(Collectors.toList()); 直到没有叶子节点if (childList != null) {
遍历查询叶子节点的叶子,直到没有叶子 child.put("children", getChildren(listAll, childList)); child.put("leaf", false); } else { child.put("leaf", true); } list.add(child); } return list; }