Search code examples
reactjsantd

How to set serial number in Antd Table for each row when we use pagination


I am using Antd Table in my react js project. I have a table with pagination set to 10 entries per page. my problem is when I click on the second page I see the serial number starts again from 1 instead of 11. its not in order. here is my code.

component

function createColumns(view, edit, remove, activate) {
 return [
{
  title: 'S NO',
  key: 'sno',
  width: '20px',
  render: (text, object, index) =>return  index + 1

},
...
...

}
 <TableWrapper
          rowKey={obj => obj.id}
          pagination={{ pageSize: 10 }}
          columns={this.columns}
          locale={{ emptyText: 'No data found' }}
          dataSource={data}              
        />

I have tried the solutions provided in the comment of this github issue but it didn't work. what am I missing here?


Solution

  • The index field referred here is relative to the data currently rendered, so it will always start from 0 on a new page. To improvise and fit your situation, you can store the current page number and use it to change the index

    const [page, setPage] = React.useState(1);
    return (
      <Table
        dataSource={data}
        pagination={{
          onChange(current) {
            setPage(current);
          }
        }}
      >
        <Column
          title="Index"
          key="index"
          render={(value, item, index) => (page - 1) * 10 + index}
        />
        ...
      </Table>
    )