对象数组按照指定rule对数据进行切割分层形成树形结构并支持搜索功能

根据提供的对象数组,按照rule: '5-3-3'的规则对数据进行分层,形成树形结构并支持搜索功能

数据:

dataList: [

{ id: '10000', name: 'a' },

{ id: '10000001', name: 'a1' },

{ id: '10000001001', name: 'a1c' },

{ id: '20000', name: 'b' },

{ id: '20000002', name: 'b1' }

]

效果:

复制代码
<template>
  <div id="app">
    <!-- 搜索框 -->
    <el-input
      placeholder="请输入搜索内容"
      v-model="filterText"
      @input="onFilterInput"
      clearable
    >
      <template #append>
        <el-button icon="el-icon-search"></el-button>
      </template>
    </el-input>

    <!-- 树形结构 -->
    <el-tree
      :data="treeData"
      :props="defaultProps"
      :filter-node-method="filterNode"
      ref="tree"
      @node-click="handleNodeClick"
    ></el-tree>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      // 动态规则,例如 "5-3-3"
      rule: '5-3-3',
      // 动态数据列表,现在是对象数组
      dataList: [
        { id: '10000', name: 'a' },
        { id: '10000001', name: 'a1' },
        { id: '10000001001', name: 'a1c' },
        { id: '20000', name: 'b' },
        { id: '20000002', name: 'b1' }
      ],
      treeData: [], // 将会填充的树形结构数据
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      filterText: '' // 搜索关键字
    };
  },
  created() {
    this.generateTree();
  },
  methods: {
    generateTree() {
      const segments = this.rule.split('-').map(Number);
      const root = {};
      const idsToNames = new Map(this.dataList.map(item => [item.id, item.name]));

      this.dataList.forEach(({ id }) => {
        let node = root;
        let start = 0;
        for (let i = 0; i < segments.length; i++) {
          const end = start + segments[i];
          const segment = id.slice(start, end);
          if (!node[segment]) {
            node[segment] = { label: idsToNames.get(id), children: {} };
          }
          if (end === id.length) break; // 如果已经到达ID的末尾,则停止
          node = node[segment].children;
          start = end;
        }
      });

      function toTreeArray(node) {
        return Object.keys(node).map(key => ({
          ...node[key],
          children: toTreeArray(node[key].children)
        })).filter(item => item.label !== undefined);
      }

      this.treeData = toTreeArray(root);
    },
    handleNodeClick(data) {
      console.log('Node clicked:', data);
    },
    onFilterInput() {
      this.$refs.tree.filter(this.filterText);
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    }
  },
  watch: {
    // 监听规则或数据的变化以重新生成树
    rule: 'generateTree',
    dataList: {
      handler: 'generateTree',
      deep: true
    }
  }
};
</script>

<style>
/* 添加样式 */
</style>
相关推荐
badhope1 天前
Mobile-Skills:移动端技能可视化的创新实践
开发语言·人工智能·git·智能手机·github
码云数智-园园1 天前
微服务架构下的分布式事务:在一致性与可用性之间寻找平衡
开发语言
hpoenixf1 天前
2026 年前端面试问什么
前端·面试
还是大剑师兰特1 天前
Vue3 中的 defineExpose 完全指南
前端·javascript·vue.js
C++ 老炮儿的技术栈1 天前
volatile使用场景
linux·服务器·c语言·开发语言·c++
hz_zhangrl1 天前
CCF-GESP 等级考试 2026年3月认证C++一级真题解析
开发语言·c++·gesp·gesp2026年3月·gespc++一级
泯泷1 天前
阶段一:从 0 看懂 JSVMP 架构,先在脑子里搭出一台最小 JSVM
前端·javascript·架构
Liu628881 天前
C++中的工厂模式高级应用
开发语言·c++·算法
IT猿手1 天前
基于控制障碍函数的多无人机编队动态避障控制方法研究,MATLAB代码
开发语言·matlab·无人机·openclaw·多无人机动态避障路径规划·无人机编队
AI科技星1 天前
全尺度角速度统一:基于 v ≡ c 的纯推导与验证
c语言·开发语言·人工智能·opencv·算法·机器学习·数据挖掘