对象数组按照指定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>
相关推荐
Avan_菜菜35 分钟前
AI 能写代码了,为什么我反而开始要求它先写文档?
前端·github·ai编程
JieE2124 小时前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2124 小时前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
爱勇宝5 小时前
鸿蒙生态的下半场:开发者不只要能开发,还要能赚钱
android·前端·程序员
IT_陈寒8 小时前
SpringBoot这个自动配置坑我跳了三次
前端·人工智能·后端
kyriewen8 小时前
我用 AI 一周写完了整个项目,上线第一天就崩了——这是我踩过最贵的 5 个坑
前端·javascript·ai编程
Larcher8 小时前
AI Loop:让AI像人一样自主完成任务的核心机制
javascript·人工智能·设计模式
默_笙9 小时前
🃏 JS 只有 8 种数据类型,但我花了 2 天才搞懂 null 和 undefined 的区别
javascript
牧艺9 小时前
从零到协同:构建类飞书在线文档系统的五个技术重难点
前端·人工智能
jump_jump9 小时前
流式 HTML:从 htmx 片段装配到浏览器原生增量渲染
javascript·性能优化·前端工程化