ElementUi表格行的点击选中和取消

本小白在刚开始使用elementui表格,发现表格的行只能选中,不能直接在表格中取消选中行,所以在经过请教之后写出了一个方法,可以选中单元格,又可以取消选中。

 

<template>
    <el-table ref="multipleTable" v-on:row-click="rowClick" :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期" width="180"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
    </el-table>
</template>

 在这个表格中,我们给表格定义了 ref 和表格的点击事件rowclick,接下来就用js去实现功能:

rowStyle: function ({ row, rowIndex }) {
      Object.defineProperty(row, 'rowIndex', { value: rowIndex, writable: true, enumerable: false });
},
rowClick: function (row, column, event) {
      let me = this;
      if (!utils.isEmpty(this.selectData) && this.selectData.rowIndex == row.rowIndex) {
           this.$refs.multipleTable.setCurrentRow();
           this.selectData = null;
           this.$refs.multipleTable.toggleRowSelection(row, false);
      } else {
           this.selectData = row;
           this.$refs.multipleTable.toggleRowSelection(row, true);
      }
},

在<script></script>的methods中写入上述方法,rowStyle方法是给每一行添加不可枚举属性rowIndex来标识当前行,再通过rowClick方法对当前行进行选中取消,则可实现表格的选中和取消选中功能。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值