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

级联树结构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.kler.cn/a/421760.html

相关文章:

  • spring boot mapper测试类优化
  • HTML 快速上手
  • 【网络安全】CSRF
  • `pnpm` 不是内部或外部命令,也不是可运行的程序或批处理文件(问题已解决,2024/12/3
  • 【Vue3】【Naive UI】<n-upload>标签
  • 【Delphi】modbus-TCP 协议库
  • 前端学习笔记-Vue篇-01
  • 前端用到的一些框架
  • python蓝桥杯刷题3
  • 游戏引擎学习第25天
  • 【XGlassTerminal.js】快速 构建 炫酷 终端 网页 以及 Linux 模拟器 在线!!
  • android视频播放器之DKVideoPlayer
  • C语言编程1.21波兰国旗问题
  • 【VPX312-0】基于3U VPX总线架构的XC7VX690T FPGA数据预处理平台
  • 机器学习概述,特征工程简述2.1——2.3
  • QT实战-qt各种菜单样式实现
  • Milvus×OPPO:如何构建更懂你的大模型助手
  • 【王道计算机组成原理·个人笔记】开头
  • leetcode 之 二分查找(java)(2)
  • 微软表示不会使用你的 Word、Excel 数据进行 AI 训练