el-table树形表格的多选框

文章描述了一个在Vue.js应用中处理树形表格的问题,当点击父节点时,子节点未自动选中。作者通过监听表格的点击和全选事件,结合数据判断来手动控制子节点的选中状态,实现了父子节点的联动。代码示例展示了如何使用`toggleSelection`方法来切换行的选中状态。
摘要由CSDN通过智能技术生成

树形表格加复选框后,选中父节点后子节点不自动选中的问题。于是在table上监听点击和全选,根据数据有子节点来手动切换选中与否。

template如下
<template><divclass="divBox"><el-tableref="table":data="tableData"style="width: 100%;margin-bottom: 20px;"row-key="id"border:indent="50":select-on-indeterminate="false"@select="select"@select-all="selectAll"@selection-change="selectionChange"default-expand-all:tree-props="{children: 'children'}"><el-table-columntype="selection"width="55"></el-table-column><el-table-columnprop="date"label="日期"sortablewidth="180"></el-table-column><el-table-columnprop="name"label="姓名"sortablewidth="180"></el-table-column><el-table-columnprop="address"label="地址"></el-table-column></el-table>
    {{ selectArr.map(el => el.id) }}
    <divstyle="margin-top: 20px"><el-button@click="cancelAll()">取消选择</el-button></div></div></template>
data如下:
       tableData: [{
          id: 1,
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          id: 2,
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        }, {
          id: 3,
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄',
          children: [{
              id: 31,
              date: '2016-05-31',
              name: '王小虎',
              address: '上海市普陀区金沙江路 1519 弄'
            }, {
              id: 32,
              date: '2016-05-32',
              name: '王小虎',
              address: '上海市普陀区金沙江路 1519 弄'
          }]
        }, {
          id: 4,
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          children: [{
              id: 41,
              date: '2016-05-31',
              name: '王小虎',
              address: '上海市普陀区金沙江路 1519 弄'
            }, {
              id: 42,
              date: '2016-05-32',
              name: '王小虎',
              address: '上海市普陀区金沙江路 1519 弄'
          }]
        }],
      selectArr: []
    }
methods方法如下:
select(selection, row){if(selection.some(el=>{return row.id === el.id })){if(row.children){
          row.children.map(j=>{this.toggleSelection(j,true)})}}else{if(row.children){
          row.children.map(j=>{this.toggleSelection(j,false)})}}},selectAll(selection){// tabledata第一层只要有在selection里面就是全选const isSelect = selection.some(el=>{const tableDataIds =this.tableData.map(j=> j.id)return tableDataIds.includes(el.id)})// tableDate第一层只要有不在selection里面就是全不选const isCancel =!this.tableData.every(el=>{const selectIds = selection.map(j=> j.id)return selectIds.includes(el.id)})if(isSelect){
        selection.map(el=>{if(el.children){
            el.children.map(j=>{this.toggleSelection(j,true)})}})}if(isCancel){this.tableData.map(el=>{if(el.children){
            el.children.map(j=>{this.toggleSelection(j,false)})}})}},selectionChange(selection){this.selectArr = selection
      console.log(this.selectArr,'selectArr')},toggleSelection(row, select){if(row){this.$nextTick(()=>{this.$refs.table &&this.$refs.table.toggleRowSelection(row, select)})}},cancelAll(){this.$refs.table.clearSelection()}}
### el-table 树形数据勾实现方式 对于 `el-table` 的树形数据显示与勾功能,Element Plus 提供了一套完整的解决方案来处理复杂的数据结构。为了使表格能够展示并操作树状层次关系的数据,需设置特定属性。 #### 属性配置 - **row-key**: 设置每一行唯一标识符字段名,用于识别节点间的父子关系[^1]。 - **tree-props**: 定义子级 children 字段名称,默认为 "children"。 - **default-expand-all**: 是否默认展开所有行,适用于初次加载时全部显示细节信息。 - **show-checkbox**: 显示多选框列,允许用户个条目。 当涉及到树形数据的择逻辑时,还需要考虑父节点与其子节点之间的联动效果: - 如果希望父节点被中则自动全其下所有的子孙节点,则可以监听 `selection-change` 事件,在回调函数内遍历当前中的项,并手动调用 `toggleRowSelection(row, selected)` 来控制关联节点的状态变化。 下面是一个简单的例子展示了如何创建带有复框的可扩展树表单: ```html <template> <div> <el-table :data="tableData" style="width: 100%" row-key="id" default-expand-all show-checkbox @selection-change="handleSelectionChange"> <!-- 列定义 --> <el-table-column type="selection" width="55"></el-table-column> <el-table-column prop="name" label="Name"></el-table-column> ... </el-table> </div> </template> <script setup lang="ts"> import { ref } from 'vue'; const tableData = [ { id: 1, name: 'Parent Node', children: [{ id: 2, name: 'Child Node' }] } ]; function handleSelectionChange(selection) { console.log('Selected:', selection); } </script> ``` 此代码片段实现了基本的功能需求,即通过指定 `row-key`, `tree-props` 及其他必要参数让 `el-table` 支持树型结构的同时提供复项让用户可以择任意数量的记录;并通过自定义的方法响应用户的交互行为,如点击某一行前面的小方块触发整个分支或者仅该叶子结点的取状态改变。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值