当前位置: 首页 > news >正文

vue2 el-table跨分页多选以及多选回显

个人博客 | snows_l.s BLIOGhttp://snows-l.site

一、多选

        1、特别注意的属性以及方法:

                :row-key="(_) => _.mac"  

                :reserve-selection="true"

                 @select="handleSelectionChange"

                @select-all="handleSelectionChangeAll"

        详情以及使用方法位置请查看代码

        2、代码:

        1)、template
<el-table
  ref="multipleTable"
  v-loading="loading"
  :data="terminalList"
  @select="handleSelectionChange"
  @select-all="handleSelectionChangeAll"
  :row-key="(_) => _.mac"
>
  <el-table-column
    type="selection"
    align="center"
    :reserve-selection="true"
    width="55"
  />

  // 其他列...
</el-table>
        2)、js
data() {
  return {
    ids:[], // 用于维护选中的id集合
  }
},


methods: {    
// 多选框选中数据
handleSelectionChange(selection, row) {
  let selectRow = selection.filter((item) => {
    return item.mac == row.mac;
  });
  // 选中 
  if (selectRow.length) {
    if (!this.ids.includes(row.mac)) {
      this.ids.push(row.mac);
    }
  } else {
    // 取消选中
    if (this.ids.includes(row.mac)) {
      this.ids.splice(this.ids.indexOf(row.mac), 1);
    }
  }
  this.single = selection.length !== 1;
  this.multiple = !selection.length;
},

// 全选按钮选中数据
handleSelectionChangeAll(selection) {
  if (selection.length) {
    // 全选
    selection.forEach((item) => {
      if (!this.ids.includes(item.mac)) {
        this.ids.push(item.mac);
      }
    });
  } else {
    // 取消全选
    this.terminalList.forEach((item) => {
      if (this.ids.includes(item.mac)) {
        this.ids.splice(this.ids.indexOf(item.mac), 1);
      }
    });
  }
  this.single = selection.length !== 1;
  this.multiple = !selection.length;
},
}

二、多选回显

        1、关键方法

// 用于清空选中的
this.$refs["multipleTable"].clearSelection();

// 用于回显选中
this.$refs["multipleTable"].toggleRowSelection(item, true);

        2、实现        

        1)、首先的把绑定的值带回来
        2)、然后再每次调接口, 也就是当前页中进行处理就好了(翻页/查询/重置,只要每次调接口)

        3)代码如下
// 回显table数据的方法
handleSelectTable() {
  // 首先清空当前页的勾选
  this.$refs["multipleTable"].clearSelection(); 
  
  // 再当前页中找到回显勾选的行  terminalList 是绑定的table数据
  let arr = this.terminalList.filter((item) => {
    return this.ids.includes(item.mac);
  });

  // 循环勾选
  arr.forEach((item) => {
    this.$refs["multipleTable"].toggleRowSelection(item, true);
  });
},


/** 查询终端列表 */
getList() {
  this.loading = true;
  listTerminal(this.queryParams)
    .then((response) => {
      this.terminalList = response.rows;
      this.total = response.total;
      this.handleSelectTable();  // 每次都调用接口(分页/重置查询),都去重置当前页的选中状态
    })
    .finally(() => {
      this.loading = false;
    });
},

相关文章:

  • Springboot的MultipartFile,获取不到inputStream
  • SeaCMS代码审计
  • 基于深度学习的OCR+NLP,医疗化验单智能识别方案
  • 【量化实战】利用miniqmt实现远程下单的完整指南
  • 阿里开源QwQ-32B推理模型!32.5B vs 671B|仅需1/10成本
  • python函数的多种参数使用形式
  • R语言软件配置(自用)
  • 人工智能之数学基础:矩阵的降维
  • 对上传的图片进行压缩,以保证它的大小不超过X MB
  • 亚马逊新品广告投放策略:从零到爆单的全链路解析
  • 黑客如何查找网络安全漏洞
  • 用 pytorch 从零开始创建大语言模型(四):从零开始实现一个用于生成文本的GPT模型
  • 关于Docker是否被淘汰虚拟机实现连接虚拟专用网络Ubuntu 22.04 LTS部署Harbor仓库全流程
  • C++ 各种map对比
  • 【JVM】内存区域划分,类加载机制和垃圾回收机制
  • openEuler24.03 LTS下安装Hadoop3完全分布式
  • 第十五届蓝桥杯C/C++组:宝石组合题目(从小学奥数到编程题详解)
  • 算法模型从入门到起飞系列——递归(探索自我重复的奇妙之旅)
  • C++内存分配方式
  • 数据结构-排序
  • 国台办:相关优化离境退税政策适用于来大陆的台湾同胞
  • IMF前副总裁朱民捐赠1000万元,在复旦设立青云学子基金
  • 巴西外长维埃拉:国际形势日益复杂,金砖国家必须发挥核心作用
  • 辽宁辽阳火灾事故饭店经营者已被控制,善后处置全面展开
  • 北京动物园:大熊猫“萌兰”没有参加日本大阪世博会的计划
  • 俄乌战火不熄,特朗普在梵蒂冈与泽连斯基会晤后口风突变