ElementUI中eable表格的回显问题,单选,全选

elementUI的翻页,全选只能选中当前页,不能翻页选中,同时,回显也不能翻页回显

采用如下方式实现翻页回显、单选、全选:
vue代码

		<el-table
            :data="tableData"
            class="demo-table-thead thead theme-table-thead"
            size="small"
            style=" line-height: 24px;"
            ref="refTable"
            @select="handleSelect"
            @select-all="handleSelectAll"
            :selection="selectarr"
            :row-key="(row)=>row.groupId"
            v-loading="loading">
            <el-table-column
              type="selection"
              width="55"
              align="center"
              :reserve-selection="true">
            </el-table-column>
        </el-table>

列表查询——回显

 //查询,表格回显
    search (page) {
      const params = {
        page: this.pagination.current,
        pageSize: this.pagination.size,
      }
      if (page) {
        params.page = page
      }
      this.loading = true
     
      getist(params)
        .then((response) => {
          this.loading = false
          this.tableData = response.data.records ? response.data.records : []
          //获取表格分页数据之和,初始为[],用于在翻页选中的时候判断
          this.allData = this.allData.concat(this.tableData)
          this.pagination.total = Number(response.data.total)
          // table回显
          this.$nextTick(() => {
            this.tableData.forEach((row, index) => {
              if (this.selectarr.findIndex((v) => v.groupId === row.groupId) >= 0) {
                this.$refs.refTable.toggleRowSelection(this.$refs.refTable.data[index], true)
              }
            })
          })
        })
        .catch(() => {
          this.loading = false
        })
    },

如果toggleRowSelection不生效,可以在外边包裹setTimeout即可

		setTimeout(() => {
            this.$nextTick(() => {
              this.selectComs.map(row => {
                this.$refs.poolTableRef.toggleRowSelection(this.poolTable.find(item => row.id === item.id), true)
              })
            })
          }, 10)

js代码:单选,全选方法。(selectarr为选中的数组,或者需要回显是外部传入的数组)

// 单选方法
    handleSelect (selection, newV) {
      if (selection.find(item => item.groupId === newV.groupId)) { //选中
        this.selectarr.push(newV)
      } else { //取消选中
        this.selectarr = this.selectarr.filter(item => item.groupId !== newV.groupId)
      }
      this.$emit('selected', this.selectarr)
    },
     // 全选方法
    handleSelectAll (rows) {
      if (rows && rows.length > 0) { //全选或(当前页取消全选+前一页有选中)
        rows.forEach(row => {
          if (!this.selectarr.find(item => item.groupId === row.groupId)) { //不在当前选中里
            this.selectarr.push(row)
          }
        })
        //上一页有选中,当前页取消全选
        this.selectarr = this.selectarr.filter(item => rows.find(val => val.groupId === item.groupId))
      } else { //取消全选
        this.selectarr = this.selectarr.filter(item => !this.allData.find(val => item.groupId === val.groupId))
      }
      this.$emit('selected', this.selectarr)
    },

   
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值