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
}
treegrid插件 当前选中的行: var config = { id: "tg1", width: "800", renderTo: "div1", headerAlign: "left", headerHeight: "30", dataAlign: "left", indentation: "20", folderOpenIcon: "images/folderOpen.gif", folderCloseIcon: "images/folderClose.gif", defaultLeafIcon: "images/defaultLeaf.gif", hoverRowBackground: "false", folderColumnIndex: "1", itemClick: "itemClickEvent", columns:[ {headerText: "", headerAlign: "center", dataAlign: "center", width: "20", handler: "customCheckBox"}, {headerText: "名称", dataField: "name", headerAlign: "center", handler: "customOrgName"}, {headerText: "拼音码", dataField: "code", headerAlign: "center", dataAlign: "center", width: "100"}, {headerText: "负责人", dataField: "assignee", headerAlign: "center", dataAlign: "center", width: "100"}, {headerText: "查看", headerAlign: "center", dataAlign: "center", width: "50", handler: "customLook"} ], data:[ {name: "城区分公司", code: "CQ", assignee: "", children:[ {name: "城区卡品分销中心"}, {name: "先锋服务厅", children:[ {name: "chlid1"}, {name: "chlid2"}, {name: "chlid3", children: [ {name: "chlid3-1"}, {name: "chlid3-2"}, {name: "chlid3-3"}, {name: "chlid3-4"} ]} ]}, {name: "半环服务厅"} ]}, {name: "清新分公司", code: "QX", assignee: "", children:[]}, {name: "英德分公司", code: "YD", assignee: "", children:[]}, {name: "佛冈分公司", code: "FG", assignee: "", children:[]} ] }; /* 单击数据行后触发该事件 id:行的id index:行的索引。 data:json格式的行数据对象。 */ function itemClickEvent(id, index, data){ window.location.href="ads"; } /* 通过指定的方法来自定义栏数据 */ function customCheckBox(row, col){ return ""; } function customOrgName(row, col){ var name = row[col.dataField] || ""; return name; } function customLook(row, col){ return "查看"; } //创建一个组件对象 var treeGrid = new TreeGrid(config); treeGrid.show(); /* 展开、关闭所有节点。 isOpen=Y表示展开,isOpen=N表示关闭 */ function expandAll(isOpen){ treeGrid.expandAll(isOpen); } /* 取得当前选中的行,方法返回TreeGridItem对象 */ function selectedItem(){ var treeGridItem = treeGrid.getSelectedItem(); if(treeGridItem!=null){ //获取数据行属性值 //alert(treeGridItem.id + ", " + treeGridItem.index + ", " + treeGridItem.data.name); //获取父数据行 var parent = treeGridItem.getParent(); if(parent!=null){ //jQuery("#currentRow").val(parent.data.name); } //获取子数据行集 var children = treeGridItem.getChildren(); if(children!=null && children.length>0){ jQuery("#currentRow").val(children[0].data.name); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值