vue element-table分页回显选中与再次更改保留状态,前端手动过滤多条件查询

本文介绍了一种基于Vue的table组件实现多选、回显数据、全选取消全选功能的方法,同时支持后台分页及前端手动过滤,确保用户在进行多条件查询时能够保持选择状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:
1.table表格多选,并且切换分页后记住上一页选项
2.回显数据,切换分页后依然回显
3.全选,取消全选数据正常变化
4.后台分页
5.前端手动过滤,多条件查询
代码:

  props: {
  //回显的数据,我这里是上一页传过来的数据
    questionListChoose: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
data() {
    return {
    	roleList:[],//总数据
    	echoList: [], //选中的id
        echoListObj: [], //选中的对象
        echoListAllObj: [], //选中的对象--备份全部
        filter: {},//筛选条件
    }
}
  mounted() {
    this.getList();
    this.echoList = JSON.parse(
      JSON.stringify(
        this.questionListChoose.map((i) => {
          return i.question_id;
        })
      )
    );
    this.echoListObj = JSON.parse(JSON.stringify(this.questionListChoose));//数据对象
    this.echoListAllObj = JSON.parse(JSON.stringify(this.questionListChoose));//备份
    console.log("echoList==", this.echoList);
  },
 <el-table
          ref="allTable"
          v-loading="loading"
          :data="roleList"
          @select="select2"
          @select-all="selectAll2"
          height="500"
        >
          <el-table-column width="55" type="selection" />
 </el-table>
  /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.question_bank_id = this.question_bank_id;
      questionList(this.queryParams).then((response) => {
        this.roleList = response.data;
        this.total = response.total;
        //在当前分页列表中查询与回显数据是否有一致的,一致的则回显勾选
        this.$nextTick(() => {
          this.roleList.forEach((item) => {
            if (this.echoList.includes(item.question_id)) {
              this.$refs.allTable.toggleRowSelection(item, true);
            }
          });
        });
        this.loading = false;
      });
    },
    // 单选
    select2(selecteds, row) {
      if (!this.echoList.includes(row.question_id)) {
        // 回显数据⾥没有本条,把这条加进来(选中)
        this.echoList.push(row.question_id);
        this.echoListObj.push(row);
        this.echoListAllObj.push(row);
      } else {
        // 回显数据⾥有本条,把这条删除(取消选中)
        this.echoList.forEach((question_id, index) => {
          if (question_id === row.question_id) {
            this.echoList.splice(index, 1);
            this.echoListObj.splice(index, 1);
            this.echoListAllObj.splice(index, 1);
          }
        });
      }
    },
//全选,取消全选
    selectAll2(selecteds) {
      if (selecteds.length > 0) {
        //全选
        selecteds.forEach((item) => {
          if (!this.echoList.includes(item.question_id)) {
            this.echoList.push(item.question_id);
            this.echoListObj.push(item);
            this.echoListAllObj.push(item);
          }
        });
      } else {
        this.roleList.forEach((item) => {
          this.echoList.forEach((question_id, index) => {
            if (question_id === item.question_id) {
              this.echoList.splice(index, 1);
              this.echoListObj.splice(index, 1);
              this.echoListAllObj.splice(index, 1);
            }
          });
        });
      }
    },

前端手动过滤,多条件查询

//前端搜索
handleQuery() {
 // 搜索已选试题
        var objIsEmpty =
          this.filter.topic_dry == '' &&
          this.filter.difficulty == "" &&
          this.filter.topic == '' &&
          this.filter.dictionary_name =='';
        if (objIsEmpty) {
         // 都为空不做处理
        } else {
          let tempFilter = {};
          for (var key in this.filter) {
            if (
              typeof this.filter[key] != "undefined" &&
              typeof this.filter[key] != "null" &&
              this.filter[key] != null &&
              this.filter[key] != ""
            ) {
              tempFilter[key] = this.filter[key];
            }
          }
          console.log(tempFilter, "输出tempFilter");
          this.$nextTick(() => {
            this.echoListObj = this.echoListAllObj.filter((item) => {
              let flag = false;
              for (key in tempFilter) {
                if (
                  item[key].toString().indexOf(tempFilter[key].toString()) >= 0
                ) {
                  flag = true;
                } else {
                  flag = false;
                  break;
                }
              }
              if (flag) {
                return item;
              }
            });
            // 搜索结果回显选中
            this.$nextTick(() => {
              this.echoListObj.forEach((i) => {
                this.$refs.multipleTable.toggleRowSelection(i, true);
              });
            });
          });
        }
        console.log(this.echoListObj, "输出筛选结果");

}
### 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` 属性才能正常工作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值