element-ui-树状表格

需求:

要做出如下图所示的 树形表格,也就是数据之间有父子类关系的这种,可以点击展开、收缩在这里插入图片描述

原数据示例

[
        {
            "id": 1,
            "name": "组织架构",
            "description": "组织架构",
            "type": 1,
            "code": "department",
            "pid": 0,
            "enVisible": "1"
        },
        {
            "id": 2,
            "name": "角色管理",
            "description": "角色管理菜单",
            "type": 1,
            "code": "role",
            "pid": 0,
            "enVisible": "1"
        },
        {
            "id": 45,
            "name": "子权限",
            "description": "",
            "type": 2,
            "code": "sonPerm1",
            "pid": 1,
            "enVisible": "0"
        },
        {
            "id": 46,
            "name": "22",
            "description": "",
            "type": 2,
            "code": "22",
            "pid": 2,
            "enVisible": "0"
        }
    ]

转化为树状数据

[
    {
        "id": 1,
        "name": "组织架构",
        "description": "组织架构",
        "type": 1,
        "code": "department",
        "pid": 0,
        "enVisible": "1",
        "children": [
            {
                "id": 45,
                "name": "子权限",
                "description": "",
                "type": 2,
                "code": "sonPerm1",
                "pid": 1,
                "enVisible": "0"
            }
        ]
    },
    {
        "id": 2,
        "name": "角色管理",
        "description": "角色管理菜单",
        "type": 1,
        "code": "role",
        "pid": 0,
        "enVisible": "1",
        "children": [
            {
                "id": 46,
                "name": "22",
                "description": "",
                "type": 2,
                "code": "22",
                "pid": 2,
                "enVisible": "0"
            }
        ]
    }
]

实现

1.使用树形组件
在学习树形表格之前,肯定得先搞懂普通的树形组件是怎么搞的,然后将其套到表格中就好了,请参考ElementUI官方文档中的使用方法
在这里插入图片描述
在这里插入图片描述
2.使用树形表格

下面这段话出自博客:Element-ui 表格实现树形结构表格_elementui树形表格_在奋斗的大道的博客-CSDN博客

在el-table中,支持树类型的数据的显示。 row 中包含 children 字段时,被视为树形数据。

渲染树形数据时,必须要指定 row-key支持子节点数据异步加载。

通过指定 row 中的 hasChildren 字段来指定哪些行是包含子节点。childrenhasChildren 都可以通过 tree-props 配置。

row-key="id":tree-props="{children: 'children', hasChildren: 'hasChildren'}是必须的。
在这里插入图片描述

代码参考

<el-table
        :data="permList"
        border
        :header-cell-style="{backgroundColor:'#F5F6F8'}"
        row-key="id"
        :default-expand-all="true"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button type="text" @click="addSonPerm(row)">添加</el-button>
            <el-button type="text">编辑</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

扁平数据转化为树状数据函数

export const transListToTree = (list, rootPid) => {
  const result = []
  list.forEach(item => {
    if (item.pid === rootPid) {
      const children = transListToTree(list, item.id)
      if (children.length > 0) {
        item.children = children
      }
      result.push(item)
    }
  })
  return result
}

相关推荐

  1. 基于 element-ui 表格组件 el-table 导出表格数据

    2024-04-12 19:54:01       30 阅读
  2. element-ui 表格固定头和固定列表格错位

    2024-04-12 19:54:01       21 阅读
  3. element ui Tree树形控件

    2024-04-12 19:54:01       32 阅读
  4. VUE +element ui 表格实现数据轮播滚动效果

    2024-04-12 19:54:01       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 19:54:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 19:54:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 19:54:01       82 阅读
  4. Python语言-面向对象

    2024-04-12 19:54:01       91 阅读

热门阅读

  1. AcWing 791. 高精度加法——算法基础课题解

    2024-04-12 19:54:01       38 阅读
  2. UI设计需要学习什么?我们应该掌握什么软件?

    2024-04-12 19:54:01       36 阅读
  3. 命名实体识别模型和分词的不同

    2024-04-12 19:54:01       36 阅读
  4. 相同地域云服务器间内网通信配置

    2024-04-12 19:54:01       35 阅读
  5. 13. 重要知识点Linux中线程

    2024-04-12 19:54:01       29 阅读
  6. Linux启动配置

    2024-04-12 19:54:01       41 阅读
  7. sql 之 索引

    2024-04-12 19:54:01       43 阅读
  8. odoo在OWL组件中管理用户动作

    2024-04-12 19:54:01       38 阅读
  9. loopvar 改动不同版本的影响-并发

    2024-04-12 19:54:01       34 阅读
  10. 代码随想录算法训练营day37

    2024-04-12 19:54:01       31 阅读
  11. 【2024护网设备】椒图面试题

    2024-04-12 19:54:01       32 阅读
  12. c---内置函数模拟(memset,memcmp,memcpy,memmove)

    2024-04-12 19:54:01       31 阅读
  13. Go 源码之旅-开篇

    2024-04-12 19:54:01       38 阅读