树形控件el-tree数据转换

估计到这来的都挺急的把,分享两种算法(推荐使用第二种,有时间再研究第一种),直接上代码,拿去直接粘走:

第一种

export function transListToTreeData(list, rootValue) {
  // 思路:先找到所有的根节点,然后再再去添加children,第二个参数是根节点的pid值
  var arr = [];
  list.forEach((item) => {
    if (item.pid === rootValue) {
      const children = transListToTreeData(list, item.id);
      if (children.length) {
        // 如果children的长度大于0就说明找到了子节点
        item.children = children;
      }
      arr.push(item);
    }
  });
  return arr;
}

解析:如果不会用的话,我来说一下怎么用:
当后端传给我们的数据没有按照树形控件的规则排好的时候,可以使用这个方法组装数组:
如:后端给我们的数据是:
在这里插入图片描述

在这里插入图片描述
然后我们调用这个方法之后(第二个参数是根节点的pid,传一个空值就可以了,因为根节点没有pid),结果就会变成:
在这里插入图片描述

在这里插入图片描述

第二种

这一种是

万能的 万能的 万能的

  function getListData(data, config) {
    var id = config.id || "id"
    var pid = config.pid || "pid"
    var children = config.children || "children"
    var idMap = {}
    var jsonTree = []
    data.forEach(function (v) {
      idMap[v[id]] = v
    })
    data.forEach(function (v) {
      var parent = idMap[v[pid]]
      if (parent) {
        !parent[children] && (parent[children] = [])
        parent[children].push(v)
      } else {
        jsonTree.push(v)
      }
    })
    return jsonTree
  }

使用方法:
第一个参数是传递的数据,第二个参数是一个对象,传递的id的值是一个字符串,是后端返回给我们的数据中的每一条数据的id的key,pid是每一条数据的pid的key,children的值我吗可以随便指定。
在这里插入图片描述
最终数据的结果如下:
在这里插入图片描述

附录

测试用的数据

  var aaa = [
    {
      id: "1175310929766055936",
      pid: "",
      companyId: "1",
      name: "总裁办",
      code: "ZCB",
      managerId: null,
      manager: "孙财",
      introduce: "总裁办公室",
      createTime: null,
    },
    {
      id: "1175311213774962688",
      pid: "",
      companyId: "1",
      name: "行政部",
      code: "XZB",
      managerId: null,
      manager: "罗晓晓",
      introduce: "1111",
      createTime: null,
    },
    {
      id: "1175311267684352000",
      pid: "",
      companyId: "1",
      name: "人事部",
      code: "RSB",
      managerId: "1071632760222810112",
      manager: "孙财",
      introduce: "规划法国恢复规划",
      createTime: null,
    },
    {
      id: "1175311325720936448",
      pid: "",
      companyId: "1",
      name: "财务部",
      code: "",
      managerId: null,
      manager: null,
      introduce: null,
      createTime: null,
    },
    {
      id: "1175311373083017216",
      pid: "",
      companyId: "1",
      name: "技术部",
      code: "JSB",
      managerId: null,
      manager: null,
      introduce: null,
      createTime: null,
    },
    {
      id: "1175311418004013056",
      pid: "",
      companyId: "1",
      name: "运营部",
      code: "YYB",
      managerId: null,
      manager: null,
      introduce: null,
      createTime: null,
    },
    {
      id: "1175311466846683136",
      pid: "",
      companyId: "1",
      name: "市场部",
      code: "SCB",
      managerId: "1063705989926227968",
      manager: "武高丽",
      introduce: null,
      createTime: null,
    },
    {
      id: "1235395178363559936",
      pid: "1175311325720936448",
      companyId: "1",
      name: "财务核算部",
      code: null,
      managerId: null,
      manager: null,
      introduce: null,
      createTime: null,
    },
    {
      id: "1235398264104624128",
      pid: "1175311325720936448",
      companyId: "1",
      name: "税务管理部",
      code: null,
      managerId: null,
      manager: null,
      introduce: null,
      createTime: null,
    },
    {
      id: "1235398536969265152",
      pid: "1175311325720936448",
      companyId: "1",
      name: "薪资管理部",
      code: null,
      managerId: null,
      manager: null,
      introduce: null,
      createTime: null,
    },
    {
      id: "1235398608847052800",
      pid: "1175311373083017216",
      companyId: "1",
      name: "Java研发部",
      code: "tryutyty",
      managerId: null,
      manager: "孙悟空",
      introduce: "1213245",
      createTime: null,
    },
    {
      id: "1235398661355544576",
      pid: "1175311373083017216",
      companyId: "1",
      name: "Python研发部",
      code: null,
      managerId: null,
      manager: null,
      introduce: null,
      createTime: null,
    },
    {
      id: "1235398708763762688",
      pid: "1175311373083017216",
      companyId: "1",
      name: "Php研发部",
      code: null,
      managerId: null,
      manager: null,
      introduce: null,
      createTime: null,
    },
    {
      id: "1235398917619130368",
      pid: "1175311466846683136",
      companyId: "1",
      name: "北京事业部",
      code: null,
      managerId: null,
      manager: null,
      introduce: null,
      createTime: null,
    },
    {
      id: "1235399011458293760",
      pid: "1175311466846683136",
      companyId: "1",
      name: "上海事业部",
      code: null,
      managerId: null,
      manager: null,
      introduce: null,
      createTime: null,
    },
    {
      id: "1542388304374833152",
      pid: "1175311373083017216",
      companyId: "1",
      name: "前端技术部",
      code: "QD",
      managerId: null,
      manager: "高小山",
      introduce: "前端的小可爱们",
      createTime: null,
    },
  ]
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

白嫖leader

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

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

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

打赏作者

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

抵扣说明:

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

余额充值