element ui表格多选翻页记忆

在一般的element ui 表格中数据选中后,翻译就没有了。今天来解决这个问题。

需求:左右都有一个表格。左边选中时,右边出现选中的的数据,翻页时,右边数据保留,再翻回来选中的数据勾起,删除右边表格的数据,左边相应数据取消勾选
大概效果:在这里插入图片描述
左边表格代码

<el-table
            :data="userTable"
            ref="selectedUser"
            :row-key="
              (row) => {
                return row.id;
              }
            "
            @select="userSel"
            @select-all="userAll"
            style="width: 100%"
            max-height="250"
          >
            <el-table-column type="selection" align="center"> </el-table-column>
            <el-table-column prop="name" label="待选">
              <template scope="scope">
                {{ scope.row.name ? scope.row.name : scope.row.phone }}
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            style="text-align: right; margin-top: 20px"
            layout="prev, pager, next"
            :total="usertotal"
            @current-change="yema"
            v-show="usertotal > 0"
            :page-size="6"
          >
          </el-pagination>

row-key用不用好像都可以,不过多选框事件一定要@select和@select-all,select是单个复选框的事件,selectAll是全选框的事件。不能用@selection-change会重复保存。

select事件

// 左边用户多选改变事件
    userSel(select, row) {
      // console.log(select,row)
      var selctIds = [];
      if (select.length > 0) {
        if (this.selected.length > 0) {
          this.selected.forEach((item, index) => {
            selctIds.push(item.id);
            selctIds = [...new Set(selctIds)];
            if (row.id == item.id) {
              this.selected.splice(index, 1);
              // console.log("删除");
            }
          });
          if (selctIds.indexOf(row.id) == -1) {
            this.selected.push(row);
            // console.log("加入");
          }
        } else {
          this.selected.push(row);
          selctIds.push(row.id);
        }
      } else {
        this.selected = [];
      }
    },

select-all事件

userAll(selection){
      let selectAll=[]
      if(selection.length==0){
        selectAll=[]
        // console.log(this.userTable)
        // 取消全选时获取当前页的所有数据,在selected中删除
        this.userTable.forEach((item,index)=>{
          this.selected.forEach((ite,inde)=>{
            if(item.id==ite.id){
              this.selected.splice(inde,1)
            }
          })
        })
      }else{
        selectAll=[...selection]
      }
      this.selected.forEach((item,index)=>{
        selectAll.forEach((ite,inde)=>{
          if(item.id==ite.id){
            selectAll.splice(inde,1)
          }
        })
      })
      selectAll.forEach(item=>{
        this.selected.push(item)
      })
      console.log(selectAll)
    },

右边取消选中

// 右边取消选中
    delUser(row) {
      console.log(this.userTable);
      this.userTable.forEach(item=>{
        if(row.id==item.id){
          this.$refs.selectedUser.toggleRowSelection(item, false);
        }
      })
      
      this.selected.forEach((item,index)=>{
        if(row.id==item.id){
          this.selected.splice(index,1)
        }
      })
    },

最后还有一个功能,翻页时已经选中的数据会勾选起,这个一定要在获取数据的地方判断,不能再翻页事件那边判断.

 // 获取用户
    getUser() {
      selUser(this.userParmas).then((response) => {
        this.userTable = response.rows;
        this.usertotal = Number(response.total);
        // console.log(this.userTable);

        if (this.selected.length > 0) {
          setTimeout(() => {
            this.userTable.forEach((item) => {
              this.selected.forEach((ite) => {
                if (item.id == ite.id) {
                  this.$refs.selectedUser.toggleRowSelection(item, true);
                }
              });
            });
          }, 200);

          // console.log(item)
        }
      });
    },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值