后端返回parentId,前端处理成children嵌套数据

rouyi 的 vuetree函数结合elementui el-table组件使用

把有parentId和id结构的数据处理成children嵌套数据,字段名称不一致,可以设置。

vuetree函数

html 复制代码
/**

 * 构造树型结构数据

 * @param {*} data 数据源

 * @param {*} id id字段 默认 'id'

 * @param {*} parentId 父节点字段 默认 'parentId'

 * @param {*} children 孩子节点字段 默认 'children'

 */

export function handleTree(data, id, parentId, children) {

  let config = {

    id: id || 'id',

    parentId: parentId || 'parentId',

    childrenList: children || 'children'

  };



  var childrenListMap = {};

  var nodeIds = {};

  var tree = [];



  for (let d of data) {

    let parentId = d[config.parentId];

    if (childrenListMap[parentId] == null) {

      childrenListMap[parentId] = [];

    }

    nodeIds[d[config.id]] = d;

    childrenListMap[parentId].push(d);

  }



  for (let d of data) {

    let parentId = d[config.parentId];

    if (nodeIds[parentId] == null) {

      tree.push(d);

    }

  }



  for (let t of tree) {

    adaptToChildrenList(t);

  }



  function adaptToChildrenList(o) {

    if (childrenListMap[o[config.id]] !== null) {

      o[config.childrenList] = childrenListMap[o[config.id]];

    }

    if (o[config.childrenList]) {

      for (let c of o[config.childrenList]) {

        adaptToChildrenList(c);

      }

    }

  }

  return tree;

}
html 复制代码
<template>

    <el-table

      v-if="refreshTable"

      v-loading="loading"

      :data="typeList"

      row-key="typeId"

      :default-expand-all="isExpandAll"

      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"

     >

      <el-table-column label="主键" align="center" prop="typeId" />

      <el-table-column label="类型编码" align="center" prop="typeCode" />

     ......

    </el-table>
</template>

export default{

data(){
    return{
        //一维数组:
        testdata:[
            {"id": 30035, "name": "分类1"},
            {"id": 30036, "name": "分类2"},
            {"id": 30037, "name": "分类3"},
            {"id": 30040, "name": "分类1-1", "parentId": 30035},
            {"id": 30041, "name": "分类2-1", "parentId": 30036},
            {"id": 30042, "name": "分类1-1-1", "parentId": 30040},
            {"id": 30043, "name": "分类1-1-2", "parentId": 30040},
            {"id": 30044, "name": "分类1-1-2-1", "parentId": 30043}
          ]
    }
},
methods:{
  getList() {

      this.loading = true;

      listType(this.queryParams).then(response => {

        this.typeList = this.handleTree(response.rows, 'typeCode', 'parentCode')

        this.total = response.total;

        this.loading = false;

      });

    },
}
}
相关推荐
yuanyxh3 小时前
macOS 应用 - 纯对话生成
前端·macos·ai编程
大家的林语冰3 小时前
ES5 凉凉,Babel 8 正式发布,默认不再编译为 ES5 和 CJS......
前端·javascript·前端工程化
光影少年4 小时前
react批量更新、同步/异步更新场景
前端·react.js·掘金·金石计划
假如让我当三天老蒯4 小时前
模块化:ES Module 与 CommonJS 的区别
前端·面试
用户40950115773174 小时前
Private Forge v2.0 发布:12大前端业务场景技能系统
前端
weedsfly5 小时前
异步编程全景与事件循环——彻底搞懂 JS 执行机制
前端·javascript
用户059540174465 小时前
AI Agent记忆测试踩坑实录:Mock骗了我一周,Mem0+pytest一招破局
前端·css
用户1733598075375 小时前
纯前端 PDF 数字签名实战:Vue 3 + pdf-lib 在浏览器里完成签名嵌入
前端·javascript
IT_陈寒6 小时前
SpringBoot自动配置的坑,我爬了三天才出来
前端·人工智能·后端
Avan_菜菜13 小时前
AI 能写代码了,为什么我反而开始要求它先写文档?
前端·github·ai编程