当前位置: 首页 > news >正文

级联树结构TreeSelect和上级反查

接口返回结构

前端展示格式

前端组件

<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());}

http://www.lryc.cn/news/497828.html

相关文章:

  • gradle下载慢解决方案2024 /12 /1android studio (Windows环境)
  • Python+OpenCV系列:GRAY BGR HSV
  • 丢垃圾视频时间检测 -- 基于状态机的实现
  • 【QT】一个简单的串口通信小工具(QSerialPort实现)
  • 24/12/5 算法笔记<强化学习> doubleDQN,duelingDQN
  • 道可云人工智能元宇宙每日资讯|全国工商联人工智能委员会成立会议在南京举办
  • MySQL数据库(2)-检查安装与密码重置
  • C# 13 中的新增功能
  • 视频自学笔记
  • easyexcel 导出日期格式化
  • 02-开发环境搭建
  • DBeaver导入csv到数据库
  • React第十一节 组件之间通讯之发布订阅模式(自定义发布订阅器)
  • tcpreplay/tcpdump-重放网络流量/捕获、过滤和分析数据包
  • ASPICE评估体系概览:对象、范围与参考标准解析
  • 力扣92.反转链表Ⅱ
  • Java设计模式之适配器模式:深入JDK源码探秘Set类
  • java八股-流量封控系统
  • 【WebRTC】Android SDK使用教学
  • 基于单片机的智能晾衣控制系统的设计与实现
  • 多人聊天室 NIO模型实现
  • 三、使用 Maven:命令行环境
  • Blender导入下载好的fbx模型像的骨骼像针戳/像刺猬
  • 如何高效搭建智能BI数据分析系统
  • 第 6 章 Java 并发包中锁原理剖析Part one
  • 使用 Canvas 绘制一个镂空的圆形区域
  • 【Notepad++】---设置背景为护眼色(豆沙绿)最新最详细
  • 2024 数学建模国一经验分享
  • 安全见闻2
  • Web游戏开发指南:在 Phaser.js 中读取和管理游戏手柄输入