Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

antd vue表格分页序号重置问题 - 爱就码上行动 - 博客园 #216

Open
xiaodongxier opened this issue Oct 27, 2023 · 0 comments

Comments

@xiaodongxier
Copy link
Owner

问题描述:

  使用antd表格a-table组件时,有时需要展示每条数据的序号。

  通常在columns定义时写为如下形式:



const columns = \[
  {
    title: '序号',
    align: 'center',
    width: 100,
    customRender: (text, record, index) =\> `${index + 1}`
  }
}




  在不设置分页的情况下,即pagination="false",表格数据单页显示,且序号正常

  

  如果需要分页,配置pagination="true"(默认,也可以不写),会导致切换页面后序号重新从1开始

  

  很明显,我们期待上图中第二页序号应从11开始,因此只要拿到currentPagepageSize,便可以计算当前页的正确序号。

解决方法:

  配置pagination对象,【序号】书写为插槽形式。

  1、data中定义pagination:



pagination: {
    current: 1,
    defaultCurrent: 1,
    pageSize: 10,
    onChange: this.onPageChange.bind(this)
}




  2、method中增加onPageChange方法:



onPageChange (current) { this.pagination.current = current
}




  3、【序号】书写为插槽形式:



const columns = \[
  {
    title: '序号',
    align: 'center',
    width: 200,
    scopedSlots: { customRender: 'dataIndex' }
  }
\]




  4、table组件配置



<a-table
    :columns="columns" :dataSource="dataSource" bordered
    :pagination="pagination" :rowKey="(record, index) => index" :loading="tableLoading">
    <template slot="dataIndex" slot-scope="text, record, index">
      <span>{{ (pagination.current - 1) * pagination.pageSize + index + 1 }}</span>
    </template>
</a-table>





__EOF__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant