欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

React Antd 表格跨页选择

程序员文章站 2022-07-14 09:26:21
...

 效果图

React Antd 表格跨页选择

代码

jsx: 

     <Table
        loading={loading}
        columns={columns}
        dataSource={dataList}
        rowKey="jobId"
        pagination={pagination}
        rowSelection={{
          columnWidth: 40,
          selectedRowKeys: store.selectedRowKeys,
          onChange: store.onSelectChange,
          onSelect: store.onSelect,
          onSelectAll: store.onSelectAll
        }}
      />

store:

class Store {
  @observable selectedRowKeys = [] // 选择的数据

  // checkbox 选择变化
  @action onSelectChange = (selectedRowKeys) => {
    this.selectedRowKeys = [...new Set([...toJS(this.selectedRowKeys), ...selectedRowKeys])];
  }

  // 单选
  @action onSelect = (record, selected) => {
    if (!selected) {
      const index = toJS(this.selectedRowKeys).findIndex(item => {
        return item === record.jobId
      })
      this.selectedRowKeys.splice(index, 1)
    }
  }

  // 全选
  @action onSelectAll = (selected, selectedRows, changeRows) => {
    if (!selected) {
      const unSelectedObj = {}
      for (const item of changeRows) {
        unSelectedObj[item.jobId] = item.jobId
      }
      const selectedRowKeys = [...toJS(this.selectedRowKeys)]
      let count = 0;
      let len = changeRows.length;
      for (let i = 0; selectedRowKeys.length; i++) {
        const item = selectedRowKeys[i]
        if (count === len) {
          break
        }
        if (unSelectedObj[item]) {
          selectedRowKeys.splice(i, 1)
          i--
          count++
        }
      }
      this.selectedRowKeys = selectedRowKeys
    }
  }
}