element-ui之el-table翻页后仍然记忆所选项

57 篇文章 0 订阅
本文介绍了如何使用Element UI的el-table组件,使其在分页后仍能记住用户的选中项。通过`v-loading`和`ref`属性,配合`toggleRowSelection`方法,展示了如何在`handleSelectionChange`和`showTag`方法中维护多选状态。适合前端开发者学习表格组件的交互优化。
摘要由CSDN通过智能技术生成

element-ui之el-table翻页后仍然记忆所选项

     <el-table
          :data="tableData"
          style="width: 100%"
          border
          v-loading="loading"
           ref="multipleTable"
          @select="handleSelectionChange"
          @select-all="handleSelectionChangeAll"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
    </el-table>
     getInStoreList() {
          this.tableData = res.data;
          this.showTag();
      },
        showTag() {
      let handleSelectData = this.multipleSelection || [];
      console.log(handleSelectData, "handleSelectData");
      for (let i = 0; i < this.tableData.length; i++) {
        for (let j = 0; j < handleSelectData.length; j++) {
          if (this.tableData[i].rid === handleSelectData[j].rid) {
            this.$nextTick().then(() => {
              this.$refs.multipleTable.toggleRowSelection(
                this.tableData[i],
                true
              );
            });
          }
        }
      }
    },
    // 单选
    handleSelectionChange(val, row) {
      if (val.includes(row)) {
        this.multipleSelection.push(row);
      } else {
        let index = -1;
        this.multipleSelection.some((e, i) => {
          if (e.rid === row.rid) {
            index = i;
            return true;
          }
        });
        this.multipleSelection.splice(index, 1);
      }
    },
    // 全选
    handleSelectionChangeAll(val) {
      if (val.length === 0) {
        this.tableData.forEach((t) => {
          let index = -1;
          this.multipleSelection.some((e, i) => {
            if (e.rid === t.rid) {
              index = i;
              return true;
            }
          });
          this.multipleSelection.splice(index, 1);
        });
      } else {
        val.forEach((e) => {
          let ids = this.multipleSelection.map((m) => m.rid);
          if (!ids.includes(e.rid)) {
            this.multipleSelection.push(e);
          }
        });
      }
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值