组织树查询-Jvava实现(递归)

十念  金牌会员 | 2023-4-19 13:11:23 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 887|帖子 887|积分 2661

1.首先查询出组织机构

就是一个简单的查询
  1. List<Dept> deptList = mapper.getDeptList();
  2.         Map<Long, OrgNode> nodeMap = new HashMap<>();
  3.         List<Long> rootIds = new ArrayList<>();
  4.         for (Dept dept : deptList) {
  5.             Long deptId = dept.getDeptId();
  6.             String name = dept.getDeptName();
  7.             Long parentId = dept.getParentId();
  8.             OrgNode node = nodeMap.computeIfAbsent(deptId, OrgNode::new);
  9.             node.setId(deptId);
  10.             node.setLabel(name);
  11.             node.setParentId(parentId);
  12.             if (parentId == 0) {
  13.                 rootIds.add(deptId);
  14.             } else {
  15.                 OrgNode parent = nodeMap.computeIfAbsent(parentId, OrgNode::new);
  16.                 parent.getChildren().add(node);
  17.             }
  18.         }
  19.         // 3. 输出组织机构
  20.         List<OrgNode> orgList = new ArrayList<>();
  21.         for (long rootId : rootIds) {
  22.             orgList.add(nodeMap.get(rootId));
  23.         }
  24.         List<Map<String, Object>> result = dfs2(orgList);
复制代码
2.def2方法,只查询两级

业务需要
  1. /**
  2.      * 只查询两级
  3.      * @param nodes
  4.      * @return
  5.      */
  6.     private static List<Map<String, Object>> dfs2(List<OrgNode> nodes) {
  7.         List<Map<String, Object>> result = new ArrayList<>();
  8.         for (OrgNode node : nodes) {
  9.             Map<String, Object> map = new HashMap<>();
  10.             map.put("id", node.getId());
  11.             map.put("label", node.getLabel());
  12.             map.put("parentId", node.getParentId());
  13.             List<OrgNode> children = node.getChildren();
  14.             if (children != null && !children.isEmpty()){
  15.                 List<OrgNode> collect = children.stream().peek(s -> s.setChildren(null)).collect(Collectors.toList());
  16.                 map.put("children", collect);
  17.             }
  18.             result.add(map);
  19.         }
  20.         return result;
  21.     }
复制代码
效果

查询所有级别递归查询
  1. /**
  2.      * 查询所有组织树
  3.      * @param nodes
  4.      * @return
  5.      */
  6.     private static List<Map<String, Object>> dfs(List<OrgNode> nodes) {
  7.         List<Map<String, Object>> result = new ArrayList<>();
  8.         for (OrgNode node : nodes) {
  9.             Map<String, Object> map = new HashMap<>();
  10.             map.put("id", node.getId());
  11.             map.put("label", node.getLabel());
  12.             map.put("parentId", node.getParentId());
  13.             List<OrgNode> children = node.getChildren();
  14.             if (children != null && !children.isEmpty()) {
  15.                 map.put("children", dfs(children));
  16.             }
  17.             result.add(map);
  18.         }
  19.         return result;
  20.     }
复制代码
3.前端代码
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>树形控件</title>
  6.    
  7.    
  8.    
  9.     <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
  10. </head>
  11. <body>
  12.     <el-tree
  13.             :data="data"
  14.             show-checkbox
  15.             default-expand-all
  16.             node-key="id"
  17.             ref="tree"
  18.             highlight-current
  19.             :props="defaultProps"
  20.             @check-change="handleCheckChange"
  21.             :indent="20"
  22.     >
  23.     </el-tree>
  24.     <el-button @click="getCheckedNodes">通过 node 获取</el-button>
  25. </body>
  26. </html>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

十念

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表