antd table 記憶翻頁勾選


重點就是給 表格列表 指定一個唯一的 key。

 

import React, {Component} from 'react';
import {Table} from 'antd';

export default class index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedRowKeys: [], // 保存選中的key
      columns: [ // 表格標題
        {title: 'Name', dataIndex: 'name'},
        {title: 'Age', dataIndex: 'age'},
        {title: 'Address', dataIndex: 'address'},
      ],
    };
  }

  render() {
    const data = []; // 表格列表的數據
    for (let i = 0; i < 46; i++) {
      data.push({
        id: i,
        name: `Edward King ${i}`,
        age: 32,
        address: `London, Park Lane no. ${i}`,
      });
    }
    const rowSelection = { // 表格配置項
      onChange: (selectedRowKeys) => { // 監聽列表選擇
        this.setState({selectedRowKeys});
      },
    };
    return (
      <div>
        <Table
          rowSelection={rowSelection}
          rowKey={record => record.id} // 這里很重要,指定唯一的 key 后就能記住翻頁勾選。
          columns={this.state.columns}
          dataSource={data}
        />
      </div>
    );
  }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM