就是一个简单的查询
List deptList = mapper.getDeptList();
Map nodeMap = new HashMap();
List rootIds = new ArrayList();
for (Dept dept : deptList) {
Long deptId = dept.getDeptId();
String name = dept.getDeptName();
Long parentId = dept.getParentId();
OrgNode node = nodeMap.computeIfAbsent(deptId, OrgNode::new);
node.setId(deptId);
node.setLabel(name);
node.setParentId(parentId);
if (parentId == 0) {
rootIds.add(deptId);
} else {
OrgNode parent = nodeMap.computeIfAbsent(parentId, OrgNode::new);
parent.getChildren().add(node);
}
}
// 3. 输出组织机构
List orgList = new ArrayList();
for (long rootId : rootIds) {
orgList.add(nodeMap.get(rootId));
}
List
业务需要
/**
* 只查询两级
* @param nodes
* @return
*/
private static List
效果
查询所有级别
递归查询
/**
* 查询所有组织树
* @param nodes
* @return
*/
private static List> dfs(List nodes) {
List> result = new ArrayList();
for (OrgNode node : nodes) {
Map map = new HashMap();
map.put("id", node.getId());
map.put("label", node.getLabel());
map.put("parentId", node.getParentId());
List children = node.getChildren();
if (children != null && !children.isEmpty()) {
map.put("children", dfs(children));
}
result.add(map);
}
return result;
}
树形控件
通过 node 获取
参与评论
手机查看
返回顶部