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;
    });
},

### element-ui el-table 数据回显 实现方法 为了在 `el-table` 组件中实现数据回显,可以按照以下方式构建逻辑: #### 1. 设置表格的基础配置 通过设置 `row-key` 和 `reserve-selection` 属性来支持以及数据回显的功能。 ```html <el-table ref="multipleTable" :data="tableData" row-key="id" > <el-table-column type="selection" :reserve-selection="true" width="40" align="center"></el-table-column> </el-table> ``` 上述代码片段中设置了 `row-key="id"` 来唯一标识每一行的数据[^1]。同时,在 `<el-table-column>` 中启用了 `:reserve-selection="true"` 属性,用于保留用户的状态[^2]。 --- #### 2. 定义存储已数据的变量 定义一个数组 `selectionData` 用来保存当前用户已经择的数据项。 ```javascript data() { return { tableData: [ { id: '1', name: 'Item A' }, { id: '2', name: 'Item B' } ], selectionData: [] // 存储已中的数据 }; } ``` 此部分代码初始化了一个空数组 `selectionData`,它将在后续操作中被填充以记录用户的择情况。 --- #### 3. 更新表格的状态 当页面加载完成或者切换到新的分页时,需要手动更新表格内部关于哪些条目已被中的状态。 ```javascript this.$nextTick(() => { this.$refs.multipleTable.store.states.selection = this.selectionData; this.$refs.multipleTable.store.updateAllSelected(); }); ``` 这里利用 Vue 的 `$nextTick()` 方法确保 DOM 已经渲染完毕后再执行相关逻辑。具体来说,将之前保存下来的 `selectionData` 赋值给表格实例内的 `_store.states.selection` 对象,并调用 `updateAllSelected()` 方法刷新视图示。 另外需要注意的是,如果要让每一页都能记住各自的勾项,则还需要额外维护一份全局范围内的全量择集合;而不仅仅是依赖单次请求返的结果集作为依据去匹配是否应该打钩某一行[^3]。 --- #### 4. 自定义获取 key 函数(可) 对于某些复杂场景下可能需要用到自定义函数来自动生成唯一的键值而非简单字段名映射关系的时候,可以通过下面这种方式指定具体的计算规则: ```javascript const getRowKey = (row) => { return row.id; // 或者其他更复杂的业务逻辑生成唯一ID }; ``` 然后将其绑定至表头组件属性里即可生效: ```html <el-table ... :row-key="getRowKey"> </el-table> ``` 这样做的好处是可以更加灵活地应对不同类型的实体对象结构差异带来的挑战。 --- ### 总结 综上所述,要在 Element UI 的 `el-table` 上面达成良好的用户体验效果——即保持住个子页面之间所做出过的任意数量级项目取动作的一致性和连贯性的话,就需要综合运用以上提到的技术手段共同协作才能顺利完成目标设定下的任务要求。 ```python # 示例 Python 风格伪代码展示整体流程控制思路 def handle_page_change(newPageNumber): global_selections = load_global_selection_state() fetch_data_for_new_page(newPageNumber).then((newPageRecords)=>{ update_local_table_records_with(global_selections, newPageRecords); render_updated_ui_elements(); }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冬零七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值