vue3+element-plus组件下拉列表,数组数据转成树形数据

引入组件

可以直接在项目中引入element-plus表格组件,如果需要变成下拉列表样式需要添加以下属性:

row-key 必填 最好给数字或唯一属性 , 给每个节点设置id 不填的话 没有办法实现展开效果

load 这个是动态添加数据的 前提(开启lazy ,表格数组里设置了hasChildren:true)

treeprops 是配置树状收缩数据的

treeprops :{hasChildren} 是否可收缩

treeprops :{children} 展开的子项

代码示例:

复制代码
 <el-table
        :data="
         (所需要的渲染数据)
        "
        row-key="id"
        style="width: 100%; border: 0.1px solid #ebeef5"
        v-loading="loading"
        lazy
        :load="load"
        :tree-props="{ hasChildren: 'hasChildren', children: 'children' }"//
      >

普通数组转换成树形数据

复制代码
 const transListDataToTreeData = (list, root) => {
      console.log(list);
      const arr = [];
      // 1.遍历
      list.forEach(item => {
        // 2.首次传入空字符串  判断list的pid是否为0 如果为空就是一级节点
        if (item.pid === root) {
          // 找到之后就要去找item下面有没有子节点  以 item.id 作为 父 id, 接着往下找
          const children = transListDataToTreeData(list, item.id);
          if (children.length > 0) {
            // 如果children的长度大于0,说明找到了子节点
            item.children = children;
          }
          // 将item项, 追加到arr数组中
          arr.push(item);
          console.log(arr);
          console.log(arr);
        }
      });
      return arr;
    };
  transListDataToTreeData(初始数组, "");
相关推荐
面向星辰1 天前
html各种常用标签
前端·javascript·html
森之鸟1 天前
Mac电脑上如何打印出字体图标
前端·javascript·macos
mCell1 天前
GSAP 入门指南
前端·javascript·动效
gnip1 天前
组件循环引用依赖问题处理
前端·javascript
Aotman_1 天前
el-input textarea 禁止输入中文字符,@input特殊字符实时替换,光标位置保持不变
前端·javascript·vue.js·前端框架·es6
EveryPossible1 天前
选择数据展示
javascript
百思可瑞教育1 天前
在Vue项目中Axios发起请求时的小知识
前端·javascript·vue.js·北京百思教育
患得患失9491 天前
【个人项目】【前端实用工具】OpenAPI to TypeScript 转换器
前端·javascript·typescript
大前端helloworld1 天前
前端梳理体系从常问问题去完善-基础篇(html,css,js,ts)
前端·javascript·面试
良木林1 天前
浅谈原型。
开发语言·javascript·原型模式