antd vue表格分页序号重置问题
问题描述:
使用antd表格a-table组件时,有时需要展示每条数据的序号。
通常在columns定义时写为如下形式:
const columns = [
{
title: '序号',
align: 'center',
width: 100,
customRender: (text, record, index) => `${index + 1}`
}
}
在不设置分页的情况下,即pagination="false",表格数据单页显示,且序号正常;
如果需要分页,配置pagination="true"(默认,也可以不写),会导致切换页面后序号重新从1开始。
很明显,我们期待上图中第二页序号应从11开始,因此只要拿到currentPage及pageSize,便可以计算当前页的正确序号。
解决方法:
配置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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?