前端纯数组转树形结构

问题描述

前端需要处理后端返回的数据,展示如下。

解决方式

因为使用ProTable组件,那么数据只要携带children字段,就可以如上图展示。

方式一:后端返回数据的时候,直接封装好,如下:

rust 复制代码
const testMenuList = [
  {
    "id": 100100,
    "parentId": 0,
    "icon": "setting",
    "name": "系统管理",
    "path": "",
    "uri": "",
    "sort": 0,
    "hidden": 1,
    "type": 0,
    "children": [
		{
		    "id": 100101,
		    "parentId": 100100,
		    "icon": "",
		    "name": "app版本管理",
		    "path": "",
		    "uri": "",
		    "sort": 0,
		    "hidden": 1,
		    "type": 1,
		},
		{
			"id": 100102,
		    "parentId": 100100,
		    "icon": "",
		    "name": "部门管理",
		    "path": "",
		    "uri": "",
		    "sort": 1,
		    "hidden": 1,
		    "type": 1,
		},
		{
		    "id": 100103,
		    "parentId": 100100,
		    "icon": "",
		    "name": "角色管理",
		    "path": "",
		    "uri": "",
		    "sort": 2,
		    "hidden": 1,
		    "type": 1,
		},
		{
		    "id": 100104,
		    "parentId": 100100,
		    "icon": "",
		    "name": "菜单管理",
		    "path": "",
		    "uri": "",
		    "sort": 3,
		    "hidden": 1,
		    "type": 1,
		},
	]
  },
  {
    "id": 100200,
    "parentId": 0,
    "icon": "message",
    "name": "新闻管理",
    "path": "",
    "uri": "",
    "sort": 1,
    "hidden": 1,
    "type": 0,
    "children":[
		{
		    "id": 100201,
		    "parentId": 100200,
		    "icon": "",
		    "name": "新闻首页维护",
		    "path": "",
		    "uri": "",
		    "sort": 0,
		    "hidden": 1,
		    "type": 1,
		},
		{
		    "id": 100202,
		    "parentId": 100200,
		    "icon": "",
		    "name": "置顶新闻",
		    "path": "",
		    "uri": "",
		    "sort": 1,
		    "hidden": 1,
		    "type": 1,
		},
		{
		    "id": 100203,
		    "parentId": 100200,
		    "icon": "",
		    "name": "专题新闻",
		    "path": "",
		    "uri": "",
		    "sort": 2,
		    "hidden": 1,
		    "type": 1,
		},
		{
		    "id": 100204,
		    "parentId": 100200,
		    "icon": "",
		    "name": "地方性新闻",
		    "path": "",
		    "uri": "",
		    "sort": 3,
		    "hidden": 1,
		    "type": 1,
		},
	]
  },
]

方式二:

后端不携带children字段,返回所有数据由前端进行处理,如下格式:

haskell 复制代码
const testMenuList = [
  {
    "id": 100100,
    "parentId": 0,
    "icon": "setting",
    "name": "系统管理",
    "path": "",
    "uri": "",
    "sort": 0,
    "hidden": 1,
    "type": 0,
  },
  {
    "id": 100101,
    "parentId": 100100,
    "icon": "",
    "name": "app版本管理",
    "path": "project/cms/system/app/index",
    "uri": "project/cms/system/app",
    "sort": 0,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100102,
    "parentId": 100100,
    "icon": "",
    "name": "部门管理",
    "path": "project/cms/system/department/index",
    "uri": "project/cms/system/department",
    "sort": 1,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100103,
    "parentId": 100100,
    "icon": "",
    "name": "角色管理",
    "path": "project/cms/system/role/index",
    "uri": "project/cms/system/role",
    "sort": 2,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100104,
    "parentId": 100100,
    "icon": "",
    "name": "菜单管理",
    "path": "project/cms/system/menu/index",
    "uri": "project/cms/system/menu",
    "sort": 3,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100200,
    "parentId": 0,
    "icon": "message",
    "name": "新闻管理",
    "path": "",
    "uri": "",
    "sort": 1,
    "hidden": 1,
    "type": 0,
  },
  {
    "id": 100201,
    "parentId": 100200,
    "icon": "",
    "name": "新闻首页维护",
    "path": "project/cms/news/frontpage/index",
    "uri": "project/cms/news/frontpage",
    "sort": 0,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100202,
    "parentId": 100200,
    "icon": "",
    "name": "置顶新闻",
    "path": "project/cms/news/top/index",
    "uri": "project/cms/news/top",
    "sort": 1,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100203,
    "parentId": 100200,
    "icon": "",
    "name": "专题新闻",
    "path": "project/cms/news/special/index",
    "uri": "project/cms/news/special",
    "sort": 2,
    "hidden": 1,
    "type": 1,
  },
  {
    "id": 100204,
    "parentId": 100200,
    "icon": "",
    "name": "地方性新闻",
    "path": "project/cms/news/original/index",
    "uri": "project/cms/news/original",
    "sort": 3,
    "hidden": 1,
    "type": 1,
  },
]

处理函数如下:

javascript 复制代码
/**
将数组转为树形
*/
export function ofList(
    data,
    rootIndex,
    itemHandler = item => item,
    current = 'id',
    parentId = 'parentId',
    children = 'children',
  ) {
    const treeData = [];
  
    let index = 0;
  
    while (index < data.length) {
      const item = itemHandler(data[index]);
  
      if (item[parentId] === rootIndex) {
        data.splice(index, 1);
        const childrens = ofList(data, item[current], itemHandler, current, parentId, children);
        item[children] = childrens.length > 0 ? childrens : undefined;
        treeData.push(item);
        index = 0;
      } else {
        index += 1;
      }
    }
  
    return treeData;
}

处理后的数据就是方式一的数据格式。因为最近在写前端,所以就在前端处理一下数据,后期赋上方式一的处理。

相关推荐
高山我梦口香糖4 小时前
[react 3种方法] 获取ant组件ref用ts如何定义?
typescript·react
一叶茶3 天前
前端生成docx文档、excel表格、图片、pdf文件
前端·javascript·react
走,板砖去5 天前
大文件传输与断点续传实现(极简Demo: React+Node.js)
node.js·react
SRestia5 天前
Django+React---从0搭建一个听音乐+聊天室的网站
websocket·django·react
远洋录5 天前
前端部署实战:从人工发布到全自动化流程
前端·人工智能·react
Rudon滨海渔村7 天前
React-antd组件库 - 让Menu子菜单项水平排列 - 下拉菜单排序 - 自定义子菜单展示方式
前端·react.js·前端框架·react
远洋录9 天前
前端单元测试实战:从零开始构建可靠的测试体系
前端·人工智能·react
Krorainas9 天前
将PDF流使用 canvas 绘制然后转为图片展示在页面上(二)
前端·javascript·pdf·react
远洋录10 天前
前端性能优化实战:从加载到渲染的全链路提升
前端·人工智能·react
Krorainas11 天前
将PDF流使用 canvas 绘制展示在页面上(一)
前端·javascript·pdf·react