ant Design3中使用table的rowSelection选中前一页数据会被清空

antd3官方组件的onChange方法在分页重新加载数据的时候,会将selectedRows清空
使用preserveSelectedRowKeys可以保留
在这里插入图片描述

const selectedRowKeys = ref<Key[]>([])
const rowSelection = computed(() => {
  return {
    selectedRowKeys: selectedRowKeysA,
    preserveSelectedRowKeys: true, // 保留之前数据的key机上这一行就好了
    onChange: (selectedRowKeys: Key[]) => {
      selectedRowKeys.value = selectedRowKeys
    },
    getCheckboxProps: (record: { isRead: string }) => ({
      disabled: record.isRead == 'Y',
    }),
  }
})
  • 1
    点赞
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值