Ant Design of Vue的table换页以后选中的数据无法记住前一页已勾选的数据

<a-table
        ref="table"
        size="small"
        :scroll="{ x: true, y: 465 }"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ onSelect: onSelect, onSelectAll: onSelectAll, selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
  onSelect (record, selected) {
      selected
        ? this.selectionRows.push(record)
        : this.selectionRows.splice(
          this.selectionRows.findIndex(x => x.id === record.id),
          1
        )
    },
    onSelectAll (selected, selectedRows, changeRows) {
      this.selectionRows = selected
        ? this.selectionRows.concat(changeRows)
        : this.selectionRows.filter(x => !changeRows.find(i => i.id === x.id))
    },
    // 选中
    onSelectChange (selectedRowKeys) {
      this.$nextTick(() => {
        this.selectedRowKeys = selectedRowKeys
      })
    },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Ant Design Vue 中,`Table` 组件的 `selectedRows` 属性只会保存当选中的行数据,翻页后之选中的行数据会被清空。如果需要在翻页后保留之选中的行数据,可以使用以下方法: 1. 在 `Table` 组件中添加 `rowSelection` 属性,并设置 `getCheckboxProps` 函数,该函数返回一个对象,用于设置行的 Checkbox 的属性。在该函数中可以设置 Checkbox 的初始状态,以及在翻页后保留之选中的行数据。 ```html <template> <a-table :columns="columns" :dataSource="dataSource" :rowSelection="rowSelection" /> </template> <script> export default { data() { return { dataSource: [], // 表格数据源 selectedRows: [], // 已选中的行数据 rowSelection: { selectedRowKeys: [], // 已选中的行的 key 值 onChange: this.handleRowSelectionChange, getCheckboxProps: record => ({ // 设置行的 Checkbox 的属性 checked: this.selectedRows.some(item => item.id === record.id), // 初始状态为已选中的行 disabled: record.disabled, // 是否禁用 Checkbox }), }, }; }, methods: { handleRowSelectionChange(selectedRowKeys, selectedRows) { // 更新已选中的行数据 this.selectedRows = selectedRows; this.rowSelection.selectedRowKeys = selectedRowKeys; }, }, }; </script> ``` 2. 在翻页时,保存已选中的行数据,以及更新 `rowSelection` 属性中的 `selectedRowKeys` 和 `selectedRows`。 ```html <template> <a-table :columns="columns" :dataSource="dataSource" :rowSelection="rowSelection" @change="handleTableChange" /> </template> <script> export default { data() { return { dataSource: [], // 表格数据源 selectedRows: [], // 已选中的行数据 rowSelection: { selectedRowKeys: [], // 已选中的行的 key 值 onChange: this.handleRowSelectionChange, getCheckboxProps: record => ({ // 设置行的 Checkbox 的属性 checked: this.selectedRows.some(item => item.id === record.id), // 初始状态为已选中的行 disabled: record.disabled, // 是否禁用 Checkbox }), }, pagination: { current: 1, // 当页码 pageSize: 10, // 每页显示条数 total: 0, // 数据总数 }, }; }, methods: { handleRowSelectionChange(selectedRowKeys, selectedRows) { // 更新已选中的行数据 this.selectedRows = selectedRows; this.rowSelection.selectedRowKeys = selectedRowKeys; }, handleTableChange(pagination) { // 在翻页时保存已选中的行数据 this.pagination = pagination; this.rowSelection.selectedRowKeys = this.selectedRows.map(item => item.id); // 更新选中的行的 key 值 this.rowSelection.selectedRows = this.selectedRows; // 更新选中的行数据 }, }, }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

My&Liu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值