【vue】element el-table怎么实现跨页勾选

【vue】element el-table怎么实现跨页勾选

需求

由后端提供分页接口,每一次翻页el-table的数据都会被刷新一次,这种情况下怎么实现跨页的勾选

代码

<el-table
      ref="multipleTable"
      v-loading="loading"
      :data="operationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
</el-table>
handleSelectionChange(e) {
      if(this.loading) return 
      for(let listItem of this.operationList){
        let eIndex = e.findIndex(item=>item.ic===listItem.ic)
        if(eIndex>=0){
          //该数据再当页被选中了
          let index = this.batchRedeployIds.findIndex(item=>item.id===listItem.id)
          if(index<0){
            //如果batchRedeployIds中没有该数据则添加上
            this.batchRedeployIds.push(listItem)
          }
        }else{
          //该数据在当页没被选中
          let index = this.batchRedeployIds.findIndex(item=>item.id===listItem.id)
          if(index>=0){
            //如果batchRedeployIds中有该数据则需要删除
            this.batchRedeployIds.splice(index,1)
          }
        }
      }
    },

翻页方法

getList() {
      请求方法(paramsObj)
        .then((res) => {
          // console.log(res.rows);
          if (res.code == '200') {
            this.operationList = res.data.records;
            this.total = res.data.total;
            // this.loading = false;
            this.$nextTick(()=>{
              for(let listItem of this.operationList){
                let index = this.batchRedeployIds.findIndex(item=>item.workOrderId===listItem.workOrderId)
                if(index>=0){
                  this.$nextTick(()=>{
                    this.$refs.multipleTable.toggleRowSelection(listItem,true)
                  })
                }else{
                  // this.$refs.multipleTable.toggleRowSelection(listItem,false)
                }
              }
              this.loading=false
            })
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

分析

1.定义了一个batchRedeployIds变量,储存所有被选中的行
2.每次勾选变化时,往batchRedeployIds里添加或删除元素
3.每次分页请求后,回填batchRedeployIds里的选中元素

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这个需求可以通过以下步骤实现: 1. 在页面上添加一个全选按钮和一个div,div里面包含一个el-table。 2. 在el-table中添加一个列用于显示复选框,并绑定每一行数据的选中状态。 3. 在el-table上方添加一个带checkbox的作业按钮,点击时,遍历el-table中的数据,将它们的选中状态都设置为true。 4. 监听全选按钮的点击事件,点击时,遍历el-table中的数据,将它们的选中状态都设置为全选按钮的选中状态。 以下是一个简单的实现示例: ``` <template> <div> <el-checkbox v-model="isAllSelected" @change="selectAll">全选</el-checkbox> <div> <el-table :data="tableData" style="width: 100%"> <el-table-column type="selection"></el-table-column> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column prop="status" label="状态"></el-table-column> <el-table-column label="操作"> <template slot-scope="{ row }"> <el-checkbox @change="selectRow(row)">作业</el-checkbox> </template> </el-table-column> </el-table> </div> </div> </template> <script> export default { data() { return { isAllSelected: false, tableData: [ { name: '项目1', status: '进行中', isSelected: false }, { name: '项目2', status: '已完成', isSelected: false }, { name: '项目3', status: '已取消', isSelected: false } ] } }, methods: { selectAll(val) { this.tableData.forEach(item => { item.isSelected = val; }) }, selectRow(row) { row.isSelected = !row.isSelected; } }, watch: { tableData: { handler(val) { this.isAllSelected = val.every(item => item.isSelected); }, deep: true } } } </script> ``` 在上面的示例中,我们使用了一个isAllSelected变量来保存全选按钮的选中状态,并且监听了tableData数组的变化来更新全选按钮的状态。同时,我们也在每一行数据中添加了一个isSelected变量来保存复选框的选中状态,并使用selectRow方法来处理作业按钮的点击事件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值