美文网首页
vue el-table 批量选择功能

vue el-table 批量选择功能

作者: wxw_威 | 来源:发表于2023-06-06 15:31 被阅读0次

赶时间,直接上代码。
逻辑就是,多选的数组,通过分页,保存到一个对象里,类似这样

selectRowObj: {
  1: [],
  2: []
}
<template>
  <el-table
      ref="table"
      :data="list"
      :height="tableHeight"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
      @selection-change="handleTableSelect"
    >
  .....
  .....
  </el-table>
</template>

method: {
 handleTableSelect(val) {
      // 加载列表的时候,拦截
      if (this.isRefreshTableData) {
        this.isRefreshTableData = false
        return
      }
      // 根据分页,保存选中数据
      this.$set(this.selectRowObj, this.pagger.current, val)
    },
    // 表格回显数据
    callBackTableData() {
      const selectionRows = []
      // 缓存的对象,通过分页下表取出选中的列表
      const cachRows = this.selectRowObj[this.pagger.current]
      // 判断选中的列表是否为空
      if (cachRows && cachRows.length > 0) {
        // 遍历数组,将原始数据list,符合要求的数据添加到空数组里
        // toggleRowSelection 只能用原始数据
        cachRows.forEach((cachItem) => {
          this.list.forEach(item => {
            if (cachItem.secSource === item.secSource) {
              selectionRows.push(item)
            }
          })
        })
        // 遍历勾选
        selectionRows.forEach(item => {
          this.$refs.table.toggleRowSelection(item)
        })
      } else {
        // 清空选中
        this.$refs.table.clearSelection()
      }
    },
    // 获取列表数据
    async getList() {
      const res = await xxxx()
      .....
      // 渲染成功后,再回显数据,防止refs获取不到table
      this.$nextTick(() => {
        this.callBackTableData()
      })
    },
    // 分页方法
    changePagination() {
      this.isRefreshTableData = true
      this.getList()
    }
}

相关文章

网友评论

      本文标题:vue el-table 批量选择功能

      本文链接:https://www.haomeiwen.com/subject/vgegedtx.html