接口返回结构
前端展示格式
前端组件
<template>
<div >
<el-scrollbar height="70vh">
<el-tree :data="deptOptions" :props="{ label: 'label', children: 'children' }" :expand-on-click-node="false"
:filter-node-method="filterNode" ref="deptTreeRef" node-key="id" highlight-current default-expand-all
@node-click="handleNodeClick" />
</el-scrollbar>
</div>
</template>
<script setup name="regulation">
import { treeListRegulationCategory } from "@/api/system/regulation/regulationCategory";
const deptOptions = ref(undefined);
/** 查询机构下拉树结构 */
function getDeptTree() {
treeListRegulationCategory().then((response) => {
deptOptions.value = response.data;
});
}
/** 通过条件过滤节点 */
const filterNode = (value, data) => {
if (!value) return true;
return data.label.indexOf(value) !== -1;
};
getDeptTree();
</script>
后端递归
publicList<TreeSelect> selectTreeList() {
// 查询全部列表
List<RegulationCategory> list = this.list();
// 构建级联树的父子结构
List<RegulationCategory> categorys = buildTree(list);
// 对级联树展示内容构建
List<TreeSelect> trees categorys.stream().map(TreeSelect::new).collect(Collectors.toList());
return trees;
}
/**
* 组装节点树
*/
private List<RegulationCategory> buildTree(Collection<RegulationCategory> categoryList) {
// 顶级父节点
List<RegulationCategory> parents = ListUtil.createList();
Map<Long, List<RegulationCategory>> categoryMap = new HashMap<>();
// 组装父子关系
for (RegulationCategory category : categoryList) {
Long parentId = category.getParentId();
if (parentId == 0L) {
parents.add(category);
continue;
}
// 子节点
List<RegulationCategory> categories = categoryMap.get(parentId);
if (ObjectUtil.isNotEmpty(categories)) {
categories.add(category);
} else {
categories = ListUtil.createList();
categories.add(category);
categoryMap.put(parentId, categories);
}
}
recursionFn(categoryMap, parents);
return parents;
}
/**
* 递归列表
*/
private void recursionFn(Map<Long, List<RegulationCategory>> categoryMap, List<RegulationCategory> parents) {
for (RegulationCategory parent : parents) {
List<RegulationCategory> childs = categoryMap.get(parent.getRegulationCategoryId());
if (ObjectUtil.isEmpty(childs)) {
continue;
}
parent.setChildren(childs);
recursionFn(categoryMap, childs);
}
}
树结构实体类
package com.ydlh.system.domain.vo;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.ydlh.common.utils.ObjectUtil;
import com.ydlh.system.domain.SysDept;
import com.ydlh.system.domain.SysMenu;
import com.ydlh.system.domain.SysMenuBusiness;
import com.ydlh.system.domain.regulation.RegulationCategory;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;
/**
* Treeselect树结构实体类
*/
@Data
public class TreeSelect implements Serializable
{
private static final long serialVersionUID = 1L;
/** 节点ID */
private Long id;
/** 节点名称 */
private String label;
/** 节点类型 */
private String type;
/** 子节点 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<TreeSelect> children;
public TreeSelect(){}
public TreeSelect(RegulationCategory category)
{
this.id = category.getRegulationCategoryId();
this.label = category.getCategoryName();
this.type = category.getCategoryType();
List<RegulationCategory> childs = category.getChildren();
if(ObjectUtil.isEmpty(childs)){
this.children = null;
}else{
this.children = category.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
}
}
}
上级反查
假设有一个层级树,其中每个id都有一个上级parentId,顶级的parentid == 0。现在需要从节点反查他的所有父级目录,这样的场景适用于点击菜单展示对应的树列表。
入参是‘节点’
public List<TreeSelect> selectRegulationTreeList(String rout) {
// 获取节点列表
RegulationCategoryRequestVo reqVo = new RegulationCategoryRequestVo();
reqVo.setBelongRegulation(rout);
List<RegulationCategory> nodeList = selectRegulationCategoryList(reqVo);
// 获取全部目录节点列表
List<RegulationCategory> categoryList = list();
Map<Long, RegulationCategory> categoryMap = categoryList.stream()
.collect(Collectors.toMap(RegulationCategory::getRegulationCategoryId, Function.identity()));
// 获取节点对应的父级目录列表
Set<RegulationCategory> returnList = new HashSet<>();
for (RegulationCategory node : nodeList) {
Long parentId = node.getParentId();
while (parentId != 0L) {
RegulationCategory parent = categoryMap.get(parentId);
if (parent == null) {
break;
}
returnList.add(parent);
parentId = parent.getParentId();
}
returnList.add(node);
}
if (ObjectUtil.isEmpty(returnList)){
return ListUtil.createList();
}
// 树结构
Collection<RegulationCategory> categorys = buildTree(returnList);
return categorys.stream().map(TreeSelect::new).collect(Collectors.toList());
}