vue element树形结构表格点击全选,子级不能选中

使用element ui 表格中的树形结构点击全选时,子级并无选中状态
在这里插入图片描述
解决示例:
1.在table中加入 @select-all="handleSelectAll"方法,当用户手动勾选全选 Checkbox 时触发的事件

<el-table
  ref="table"
  v-bind="tableProps"
  @select-all="handleSelectAll"
>
  ...
</el-table>

2.在handleSelectAll方法中,使用toggleRowSelection方法切换行的选中状态。

 handleSelectAll(selection) {
    // 获取当前表格的所有行
    const fullData = this.$refs.table.store.states.data;
    // 如果当前选中的行数为0,则取消全选
    if (selection.length === 0) {
      this.$refs.table.store.states.selection = [];
      return;
    }
    // 遍历所有的行数据,找出子节点并添加到选项中
    fullData.forEach(item => {
      const children = item.children || [];
      children.forEach(child => {
        if (selection.includes(item)) {
          this.$refs.table.toggleRowSelection(child, true);
        } else {
          this.$refs.table.toggleRowSelection(child, false);
        }
      });
    });
  }

在修改后的handleSelectAll方法中,添加了一个判断语句。如果selection数组中包含了父节点,就将其所有子节点选中;如果不包含父节点,就将其所有子节点取消选中。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在arco,可以通过使用vue数据渲染树形表格子级。 具体步骤如下: 1. 定义树形数据结构,包括id、name、children等字段。 2. 在vue,使用递归组件方式渲染树形表格。在组件,使用v-for指令遍历所有节点,为每个节点生成行数据,并判断是否有节点。若有,则调用递归组件并传入节点数据作为参数,以此实现树形结构的渲染。 3. 可以根据需要,使用slot插槽自定义表格列的渲染方式。 具体代码实现可以参考以下示例: ``` <template> <div> <div class="table-header">树形表格</div> <div class="table-container"> <a-table :columns="columns" :data-source="dataSource" row-key="id"> <template #default="{ row, index }"> <tr> <td>{{row.id}}</td> <td>{{row.name}}</td> </tr> <tr :class="'treeTable-subRow-' + row.id" v-if="row.children && row.children.length > 0"> <td colspan="2"> <RecursiveTable :dataSource="row.children"></RecursiveTable> </td> </tr> </template> </a-table> </div> </div> </template> <script> import { Table, Column } from 'arco-design-vue'; // 递归组件 let RecursiveTable = { name: 'RecursiveTable', props: ['dataSource'], components: { 'RecursiveTable': this }, render() { return ( <a-table :columns="columns" :data-source="dataSource" row-key="id" style="margin-left: 12px;"> <template #default="{row}"> <tr> <td>{{row.id}}</td> <td>{{row.name}}</td> </tr> <tr :class="'treeTable-subRow-' + row.id" v-if="row.children && row.children.length > 0"> <td colspan="2"> <RecursiveTable :dataSource="row.children"></RecursiveTable> </td> </tr> </template> </a-table> ) }, data() { return { columns: [ { title: 'ID', dataIndex: 'id', width: '20%' }, { title: '名称', dataIndex: 'name', width: '80%', } ], } }, } export default { components: { RecursiveTable, [Table.name]: Table, [Column.name]: Column, }, data() { return { columns: [ { title: 'ID', dataIndex: 'id', width: '20%' }, { title: '名称', dataIndex: 'name', width: '80%', } ], dataSource: [ { id: '1', name: '节点1', children: [ { id: '1-1', name: '节点1-1', children: [ { id: '1-1-1', name: '节点1-1-1', }, { id: '1-1-2', name: '节点1-1-2', } ] }, { id: '1-2', name: '节点1-2', } ] }, { id: '2', name: '节点2', } ], } }, } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玫光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值