<el-table>树tree节点全选

描述:当写了一棵树的树,想把全部的子节点都选中如:

爷爷:A 指(广东省)

爸爸:B 指(市)

儿子:C 指(区/街道)

优化点:当独个选中爸爸的时候,儿子也会自动跟着选中。

<template>
<el-table ref="tables" 
v-loading="loading" 
:height="height" 
border stripe 
@select-all="selectAll"
@select="select"
:data="dataList" 
style="width: 100%" row-key="id" 
:tree-props="{children: 'children'}" >

  <el-table-column type="selection" width="60" align="center" />
  <el-table-column type="index" label="序号" width="80" align="center" sortable />
</template>

<script>
data() {
 return {
     // 数据列表
    dataList: [],
}
methods: {
//选中框的全部树节点
       selectAll( ) {
            let rows = [];
            this.getChildren(rows, this.dataList);
            delete rows[0];
            rows.forEach(row => {
                this.$refs.tables.toggleRowSelection(row);
            });
        },
        select(selection, row) {
            let rows = [];
            this.getChildren(rows, row.children);
            rows.forEach(row => {
                this.$refs.tables.toggleRowSelection(row);
            });
        },
//写了个递归
        getChildren(rows, children) {
            if (children) {
                children.forEach(child => {
                    rows.push(child);
                    this.getChildren(rows, child.children);
                });
            }
        },
}

</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值