element-Plus 中table复选框勾选-跨页数据保存选中状态(跨页进行分页勾选)/复选框禁点 ,实现分页切换将选中数据回显

      

          <el-dialog
         v-model="dialogOptionalVisible"
         title="医选列表"
         width="60%"
         @close="handleOptionalsClose"
         >

           <el-table
              v-loading="loading"
              :data="tablequeryCustomerItem"
              border
              height="600"
              :header-cell-style="{
                backgroundImage: 'linear-gradient(176deg,#accbee ,#e7f0fd 100%)',
                color: '#000',
              }"
              style="width: 100%"
              row-key="id"
              @selection-change="handleSelectiontoChange"
              ref="multipletoTable"
            >
              <el-table-column
                type="selection"
                :reserve-selection="true"
                width="55"
                :selectable="isSelectable"
              ></el-table-column>
              <el-table-column
                prop="bookTime"
                label="预约时间"
                align="center"
                width="110"
                show-overflow-tooltip
              >

  1. 跨页面保存选中状态 :reserve-selection="true"  

    el-table 中 绑定 row-key="id"    绑定ref

// 勾选数据
    handleSelectiontoChange(selection) {
      console.log('🚀 ~ 全选', selection);
      if (selection) {
        this.orderSelectionData = selection; // 保存选中的数据
        this.orderSelectionKeys = selection.map(item => item.optionalItemCode); // 选中的 code
      }
    },

清空数值 

   // 医选-关闭
    handleOptionalsClose() {
      this.dialogOptionalVisible = false; //医选弹框
      this.orderSelectionKeys = [];
      this.optionalsData = [];
      this.orderItemCode = '';
      this.$refs.multipletoTable.clearSelection();
    },

  //禁点
   isSelectable(row, index) {
      const item = row || {};
      if (item.bookStatus === 1 || item.refundStatus !== 0 || item.buttonStatus === 10) {
        return false;
      }
      return true;
    },
  • 需要注意:
  • 1.row-key="id"  绑定Id;
  • 2.ref="multipletoTable"  绑定ref;
  • 3.type="selection" :reserve-selection="true"  用于跨页保存选中;
  • 4.打印selection,选中的数组为this.orderSelectionData;
  • 5.选中的id,放在this.orderSelectionKeys.
  • 6.el-dialog 绑定close ,关闭弹框时,清空table的选中值

### Vue3 Element Plus 复选框分页回显解决方案 在处理 `Vue3` 和 `Element Plus` 的表格组件时,为了实现分页切换后保持已择项的状态,可以采用以下方案: #### 数据结构设计 确保数据模型能够保存每一状态。通常做法是在面级的数据对象中维护一个映射表来记录哪些行被选中。 ```javascript const selectedRowsMap = ref(new Map()); ``` 此变量用于存储每一行唯一标识符到其是否已被勾选的布尔值之间的对应关系[^1]。 #### 表格配置 当初始化表格或加载新码的数据时,应遍历当前显示的数据集并设置默认选中的行。这可以通过监听分页事件并在每次更新视图之前同步状态完成。 ```html <template> <el-table :data="tableData" @selection-change="handleSelectionChange"> <!-- 列定义 --> <el-table-column type="selection" width="55"></el-table-column> ... </el-table> <div class="pagination-container"> <el-pagination layout="prev, pager, next" :total="total" @current-change="handlePageChange"/> </div> </template> ``` 每当发生分页变化时触发 `handlePageChange` 方法,在该方法内部重新计算哪几条记录应该处于选中状态,并通过编程方式调用 `toggleRowSelection()` 来恢复这些项。 ```typescript import { ElTable } from "element-plus"; // 假设 tableRef 是指向 el-table 组件实例的一个 Ref const handlePageChange = (currentPage) => { // 获取新的数据列表... const currentSelectedKeys = Array.from(selectedRowsMap.value.keys()).filter(key => newDataList.some(item => item.id === key)); nextTick(() => { currentSelectedKeys.forEach((key) => { const row = newDataList.find(item => item.id === key); if(row){ tableRef.value.toggleRowSelection(row, true); } }); }); }; ``` 上述逻辑确保即使用户翻阅不同面再返回原位置也能看到之前的多结果得以保留[^2]。 对于渲染函数的具体实现细节以及如何自定义单元格内容可参见提供的 E-expand.js 文件说明;而对于属性配置方面,则需要注意像 `<el-tree-select>` 这样的控件需要正确指定 `props` 属性才能正常工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值