vue里面 一维数组转为树形结构及树形结构转为一维数组

24 篇文章 1 订阅

后台管理系统中经常会用到树形结构式的表格,或者下拉菜单,如下图,我的办公下面有几个子级,而其中的一个子级电子邮件下面还有子级
在这里插入图片描述
此时可以和后端商量下返回的数据格式,便于父级和子级进行关联,返回的字段除了id,最好有个pid子级的pid等于父级的id),数据格式如下

data:[
   {id:1,pid:0,name:'oa办公',},
   {id:2,pid:0,name:'财务管理',},
   {id:4,pid:1,name:'盖章申请',},
   {id:5,pid:1,name:'我的审批',},
   {id:6,pid:2,name:'社保账单',},
   {id:7,pid:2,name:'薪资账单',},
   {id:8,pid:4,name:'用章外借',},
],

此时盖章申请我的审批的pid等于1,oa办公的id是1,他俩就是oa办公的子级,而用章外借盖章申请的子级,同理,财务管理薪资账单社保账单的父级

一、一维数组转为树形结构

方法如下:

/**
   pid形式数据转children形式
   data 需要转换的数组
   idKey id字段名
   pidKey pid字段名
   childKey 生成的children字段名
   pid 顶级的pid
   returns {[]}
*/
toTreeData(data, idKey, pidKey, childKey, pid) {
   if (!childKey) childKey = 'children';
   if (pid === undefined) {
       pid = [];
       data.forEach(d => {
           let flag = true;
           for (let i = 0; i < data.length; i++) {
               if (d[pidKey] == data[i][idKey]) {
                   flag = false;
                   break;
               }
           }
           if (flag) pid.push(d[pidKey]);
       });
   }
   let result = [];
   data.forEach(d => {
       //if (d[idKey] == d[pidKey]) return console.error('data error: ', d);
       if (Array.isArray(pid) ? (pid.indexOf(d[pidKey]) !== -1) : (d[pidKey] == pid)) {
           let children = this.toTreeData(data, idKey, pidKey, childKey, d[idKey]);
           if (children.length > 0) d[childKey] = children;
           result.push(d);
       }
   });
   return result;
},

示例如下:

在这里插入图片描述
代码如下,可以直接在浏览器console打印看看

function toTreeData(data, idKey, pidKey, childKey, pid){
   if (!childKey) childKey = 'children';
   if (pid === undefined) {
       pid = [];
       data.forEach(d => {
           let flag = true;
           for (let i = 0; i < data.length; i++) {
               if (d[pidKey] == data[i][idKey]) {
                   flag = false;
                   break;
               }
           }
           if (flag) pid.push(d[pidKey]);
       });
   }
   let result = [];
   data.forEach(d => {
       //if (d[idKey] == d[pidKey]) return console.error('data error: ', d);
       if (Array.isArray(pid) ? (pid.indexOf(d[pidKey]) !== -1) : (d[pidKey] == pid)) {
           let children = this.toTreeData(data, idKey, pidKey, childKey, d[idKey]);
           if (children.length > 0) d[childKey] = children;
           result.push(d);
       }
   });
   return result;
};
let data=[
   {id:1,pid:0,name:'oa办公',},
   {id:2,pid:0,name:'财务管理',},
   {id:4,pid:1,name:'盖章申请',},
   {id:5,pid:1,name:'我的审批',},
   {id:6,pid:2,name:'社保账单',},
   {id:7,pid:2,name:'薪资账单',},
   {id:8,pid:4,name:'用章外借',},
];
let mydata=toTreeData(data,'id','pid');
console.log(mydata);

页面中el-table使用大致如下

 <el-table ref="table" :data="data" v-loading="loading" row-key="id" default-expand-all border
            height="calc(100vh - 215px)" highlight-current-row  lazy
            :load="load"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
    <el-table-column label="编号" type="index" width="60" align="center" fixed="left"/>
 <el-table>

二、树形结构转为一维数组

额,一般来说,后端接口返回的数据都是一维数组的,然后前端兄弟根据自己的需求对数据进行转换,所以大多都是怎么把一维数组转为树形结构,很少用到把树形结构转为一维数组的,但是人生无常,大肠包小肠,或许用不到,知道还是没啥坏处的,嘿嘿,方法如下

convertTreeData(data) {
   for (let i = 0; i < data.length; i++) {
       if (data[i].children != undefined) {
           const temp = data[i].children
           delete data[i].children
           data = data.concat(temp)
       }
   }
   return data
}

示例如下

在这里插入图片描述
代码如下:

function convertTreeData(data) {
   for (let i = 0; i < data.length; i++) {
       if (data[i].children != undefined) {
           const temp = data[i].children
           delete data[i].children
           data = data.concat(temp)
       }
   }
   return data
};
let data=[
    {id:1,pid:0,name:'oa办公',children:[{id:4,pid:1,name:'盖章申请',children:[{id:8,pid:4,name:'薪资账单',children:[]},]},{id:5,pid:1,name:'我的审批',children:[]},]},
    {id:2,pid:0,name:'财务管理',children:[{id:6,pid:2,name:'社保账单',children:[]},{id:7,pid:2,name:'薪资账单',children:[]},]},
];
let mydata=convertTreeData(data);
console.log(mydata);

ps:chrome浏览器的console里面写代码怎么换行?shift+enter

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coderdwy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值