ant-vue Table组件selectedRows翻页后不保留上一页已选

思路:把当前页的selectedRowKeys存下来,其他页的selectedRowKeys拼接一下,然后去重并删除已取消项
代码我就不贴的很好了,因为每个人需求不一样:
我这里是用的数据的goodsID作为唯一标示进行操作的

    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      if (this.selectedData.length > 0) {
        this.selectedData = this.MergeArray(this.selectedData, selectedRows)
      } else {
        this.selectedData = selectedRows
      }
    },
    // 去重
  MergeArray (arr1, arr2) {
      var _arr = []
      for (var i = 0; i < arr1.length; i++) {
        _arr.push(arr1[i])
      }
      for (var x = 0; x < arr2.length; x++) {
        var flag = true
        for (var j = 0; j < arr1.length; j++) {
          if (arr2[x].goodsID === arr1[j].goodsID) {
            flag = false
            break
          }
        }
        if (flag) {
          _arr.push(arr2[x])
        }
      }
       _arr = this.spliceArray(_arr, this.selectedRowKeys)
      return _arr
    },
    // 删除未选择数据
    spliceArray (arr1, arr2) {
      var _arr = []
      for (var x = 0; x < arr1.length; x++) {
        for (var j = 0; j < arr2.length; j++) {
          if (arr1[x].goodsID === arr2[j]) {
            _arr.push(arr1[x])
          }
        }
      }
      return _arr
    }
  },

这样的话 this.selectedData 就会是你需要的啦

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
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> ```
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值