后端返回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;

      });

    },
}
}
相关推荐
奇迹_h3 小时前
打造你的HTML5打地鼠游戏:零基础入门实践
前端
SuperEugene3 小时前
Vue生态精选篇:Element Plus 的“企业后台常用组件”用法扫盲
前端·vue.js·面试
Neptune13 小时前
JavaScript回归基本功之---类型判断--typeof篇
前端·javascript·面试
贾铭3 小时前
如何实现一个网页版的剪映(三)使用fabric.js绘制时间轴
前端·后端
子兮曰5 小时前
后端字段又改了?我撸了一个 BFF 数据适配器,从此再也不怕接口“屎山”!
前端·javascript·架构
万少7 小时前
使用Trae轻松安装openclaw的教程-附带免费token
前端·openai·ai编程
浪浪山_大橙子7 小时前
OpenClaw 十分钟快速,安装与接入完全指南 - 推荐使用trae 官方 skills 安装
前端·人工智能
忆江南7 小时前
iOS 可视化埋点与无痕埋点详解
前端
离开地球表面_997 小时前
金三银四程序员跳槽指南:从简历到面试再到 Offer 的全流程准备
前端·后端·面试
_柳青杨7 小时前
跨域获取 iframe 选中文本?自己写个代理中间层,再也不求后端!
前端