el-table 多选回显,分页回显

实现el-table多选分页回显功能,左侧是分页的数据源,右侧是选择后的人员数据,切换下一页,选中的数据会在左侧表格回显。

实现:

javascript 复制代码
<template>
  <el-dialog :title="title" :visible.sync="show" :before-close="cancel" center
             width="1050px" custom-class="bind-dialog"
  >
    <div  class="content">
      <div class="left-user" >
        <el-table ref="table" :data="tableData" border :row-key="getRowKeys" style="width: 100%;height: 100%"
                  class="cust-table" size="mini"   @select="handleSelectionChange" @select-all="selectAll">
          <el-table-column :reserve-selection="true" type="selection" width="50" align="center" center></el-table-column>
          <el-table-column v-for="(item, index) in tableTitle" :label="item.label" :prop="item.prop" :key="index"
                           align="center" center :width="item.width"
          >
            <template slot-scope="scope">
              <div v-if="scope.row[item.prop]==null || scope.row[item.prop] ===''">-</div>
              <div v-else>
                {{ scope.row[item.prop] }}
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="right-user">
        <el-tag
          v-for="tag in selectedUsers"
          :key="tag.userId"
          class="mr8 mb8"
          @close="delUser(tag)"
          closable
        >
          {{ tag.nickName }}
        </el-tag>
      </div>
    </div>
    <pagination
      class="mt12"
      v-show="total>0"
      :total="total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="queryList"
    />
    <span slot="footer" class="dialog-footer">
       <el-button @click="cancel">取消</el-button>
       <el-button type="primary" @click="confirm">确定</el-button>
     </span>
  </el-dialog>
</template>

el-table的ref、row-key、select、select-all、type="selection"、:reserve-selection="true"都是需要设置的,并且表格绑定的data初始值不能为null,可以设置[]

设置row-key

javascript 复制代码
  getRowKeys(row) {
      return row.userId
    },

表格选择数据,@select="handleSelectionChange" @select-all="selectAll" 选择单个和全选都要写

javascript 复制代码
 handleSelectionChange(arr, row) {
     
      const bool = this.selectedUsers.some(user => user.userId === row.userId) // 存在返回true 否则返回false
      if (bool) {
        // 存在删除
        this.selectedUsers = this.selectedUsers.filter(user => user.userId !== row.userId)
      } else {
        // 添加
        this.selectedUsers.push(row)
      }
    },
    selectAll(arr){
      if (arr.length !== 0) {
        // 全选
        arr.forEach(item => {
          const bool = this.selectedUsers.some(user => user.userId === item.userId) // 存在返回true 否则返回false
          if (!bool) {
            // 不存在添加
            this.selectedUsers.push(item)
          }
        })
      } else {
        // 取消全选
        this.tableData.forEach(item => {
          this.selectedUsers = this.selectedUsers.filter(user => user.userId !== item.userId)
        })
      }
    },

删除右侧选中数据的时候,不仅要对右侧选中数组处理,还要把左侧数组的选中状态设为未选中;

javascript 复制代码
 delUser(node) {
      this.selectedUsers = this.selectedUsers.filter(user => user.userId !== node.userId)
      this.tableData.forEach(item => {
        if (node.userId === item.userId) {
          // 存在添加
          this.$refs.table.toggleRowSelection(item, false)
        }
      })
    },

在数据编辑的时候,回显设置。注意切换table的page的时候要清除table的选中状态,重新设置选中状态,因为当右侧删除选中的数据不是当前页,分页切换的时候要刷新table的选中状态。

javascript 复制代码
    queryList() {
      this.loading = true
      let queryParams = {
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize,
      }
      queryUserData(queryParams).then((res) => {
          if (res.code === 200) {
            this.tableData = res.rows
            this.total = res.total
            // 切换分页的时候要清楚table的选中状态,在根据selectedUsers的值设置table选中状态
            this.$refs.table.clearSelection()
            if (this.selectedUsers.length > 0) {
              this.$nextTick(()=>{
                for (let i = 0; i < this.tableData.length; i++) {
                  this.selectedUsers.forEach(item=>{
                    if (item.userId == this.tableData[i].userId){
                      this.$refs.table.toggleRowSelection(this.tableData[i], true);
                    }
                  })

                }
              })
            }
          }
        })
        .finally(() => {
          this.loading = false
        })
    },

css样式设置

javascript 复制代码
<style lang="scss" scoped>
.bind-dialog {
  .content {
    display: flex;
    height: 406px;

    .left-user {
      flex: 1;
      margin-right: 12px;
    }
 

    .right-user {
      width: 310px;
      padding: 12px;
      height: 100%;
      box-sizing: border-box;
      border: 1px solid #dfe6ec;
      border-radius: 6px;
      overflow: hidden auto;
    }
  }
  .mt12{
    margin-bottom: 12px;
  }
  .mr8{
    margin-right: 8px;
  }
  .mb8{
    margin-bottom: 8px;
  }
}
</style>
相关推荐
一 乐1 小时前
学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
昨天;明天。今天。1 小时前
案例-表白墙简单实现
前端·javascript·css
数云界1 小时前
如何在 DAX 中计算多个周期的移动平均线
java·服务器·前端
风清扬_jd1 小时前
Chromium 如何定义一个chrome.settingsPrivate接口给前端调用c++
前端·c++·chrome
安冬的码畜日常1 小时前
【玩转 JS 函数式编程_006】2.2 小试牛刀:用函数式编程(FP)实现事件只触发一次
开发语言·前端·javascript·函数式编程·tdd·fp·jasmine
ChinaDragonDreamer1 小时前
Vite:为什么选 Vite
前端
小御姐@stella1 小时前
Vue 之组件插槽Slot用法(组件间通信一种方式)
前端·javascript·vue.js
GISer_Jing1 小时前
【React】增量传输与渲染
前端·javascript·面试
GISer_Jing1 小时前
WebGL在低配置电脑的应用
javascript
eHackyd1 小时前
前端知识汇总(持续更新)
前端