Vue element ui table,点击一行时选中这一行对应的复选框

这篇博客详细介绍了如何在Vue Element UI组件库中,通过监听表格行点击事件和复选框选择变化,实现点击表格行时选中对应的复选框。提供了`clickRow`、`rowClick`和`handleSelectionChange`三个关键方法的实现,用于单击选中、多选以及复选框单选操作,确保了选中状态的正确同步。
摘要由CSDN通过智能技术生成
12、Vue element ui table,点击一行时选中这一行对应的复选框
      <el-table
              @row-click="clickRow"
              ref="dataList"
              :data="tableData"
              :height="tableHeight"
              border
              stripe
              tooltip-effect="dark"
              @selection-change="onTableSelectChange"
              style="width: 100%"
      >
          

js对应的方法:

clearSelection : 清除选中状态

toggleRowSelection:选中复选框

1、单击

 clickRow(row, column, event) {
        // console.log(row)
        //   选已选中数据中判断当前点击的是否被选中
        if (this.listTableSelection[0] == row) {  // 选中的是已有的 取消选中
          this.listTableSelection = [];
          this.$refs['dataList'].clearSelection();
        } else {
          this.listTableSelection = [row];
          this.$refs['dataList'].clearSelection();
          this.$refs['dataList'].toggleRowSelection(row, true);
        }
      },

2、单击多选

rowClick(row, column, event) {  // 点击行多选
        // console.log(row)
        // 从已选中数据中 判断当前点击的是否被选中
        const selected = this.listTableSelection.some(item => item.id === row.id)  // 是取消选择还是选中
        if (!selected) { // 不包含   代表选择
          this.listTableSelection.push(row)
          this.$refs['multipleTable'].toggleRowSelection(row, true);
        } else { // 取消选择
          var finalArr = this.listTableSelection.filter((item) => {
            return item.id !== row.id
          })
          this.listTableSelection = finalArr  // 取消后剩余选中的

          this.$refs['dataList'].toggleRowSelection(row, false);
        }
        console.log('更改选择后', this.multipleSelection)
    },

3、table复选框单选 点击复选框单选效果 记录选中数据

handleSelectionChange(selection, row) {
      if (this.listTableSelection[0] == row) {
        this.listTableSelection = [];
        this.$refs['dataList'].clearSelection();
      } else {
        this.multipleSelection = [row];
        this.$refs['dataList'].clearSelection();
        this.$refs['dataList'].toggleRowSelection(row, true);
      }
    },
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值