Pagination 分页
把长列表分成多页,每次只加载和显示一页,并提供翻页、跳页和切换每页条数的入口。
何时使用
- 数据量较大、一次加载或显示会很慢时,按页加载。
- 需要让用户快速定位到某一页,或调整每页显示条数时。
- 表格和列表一般不需要单独放分页器:Table 的
Pagination/ServerPagination、List 的Pagination已内置分页;ProTable 也自带分页。 - 数据需要连续浏览(消息流、动态)时,用列表的「加载更多」或
InfiniteScroll无限滚动代替分页。
在线体验
切换场景可以看到基础分页、总数与每页条数、小尺寸、Element 风格的自定义布局,以及跳页、简洁模式、背景色、禁用和单页隐藏。
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"fmt"
"github.com/tangoui/tango/ui"
)
var current = 1
func page(c *ui.Context) {
ui.Pagination(c, ¤t, 500, 10).Show()
ui.Text(c, fmt.Sprintf("当前第 %d 页", current))
}ui.Pagination(c, &page, total, pageSize) 绑定当前页(从 1 开始),total 是数据总条数,pageSize 是每页条数,链式设置后调用 Show()。用户点击页码时分页器直接改写 *page,同一帧 Changed() 返回 true,可以在这里触发加载。
常见用法
以下片段省略了 import。
总数与每页条数
ShowTotal() 显示「共 N 条」;SizeChanger(&size) 显示每页条数选择器(10 / 20 / 50 / 100),绑定的变量同时作为 pageSize 传入。
var listPage, listSize = 1, 20
func sizedPager(c *ui.Context) {
p := ui.Pagination(c, &listPage, 85, listSize).ShowTotal().SizeChanger(&listSize).Show()
if p.Changed() {
// 按 listPage / listSize 重新加载数据
}
}跳页
ShowQuickJumper() 增加「跳至 [ ] 页」输入框,回车或离开输入框后跳转。
var jumpPage = 1
func jumperPager(c *ui.Context) {
ui.Pagination(c, &jumpPage, 500, 10).ShowTotal().ShowQuickJumper().Show()
}简洁模式与小尺寸
Simple() 只保留上一页、可编辑的当前页、「/ 总页数」和下一页;再加 ReadOnly() 时当前页显示为文字,是最窄的分页器,适合放在列表或穿梭框底部。Small()(等价于 Size(ui.Small))使用小尺寸。
var simplePage = 2
func compactPagers(c *ui.Context) {
ui.Pagination(c, &simplePage, 50, 10).Simple().Show()
ui.Pagination(c, &simplePage, 50, 10).Simple().ReadOnly().Small().Show()
ui.Pagination(c, &simplePage, 50, 10).Small().Show()
}背景色、禁用与单页隐藏
Background(true) 使用带底色的页码按钮(Element Plus 的 background,Element 主题下默认开启,Background(false) 关闭);Disabled(true) 禁用全部交互;HideOnSinglePage() 在只有一页时不显示分页器。
var bgPage = 1
func styledPagers(c *ui.Context, loading bool) {
ui.Pagination(c, &bgPage, 100, 10).Background(true).Disabled(loading).Show()
ui.Pagination(c, &bgPage, 5, 10).HideOnSinglePage().Show() // 只有 1 页,不显示
}自定义布局与页码数量
Layout(parts...) 按给定顺序排列分页器的各部分(对应 Element Plus 的 layout),只显示列出的部分:PaginationTotal、PaginationPrev、PaginationPager、PaginationNext、PaginationSizes(需要同时调用 SizeChanger)、PaginationJumper,以及把后面部分推到行尾的 PaginationSpacer。PagerCount(n) 设置最多显示多少个页码按钮(5–21 的奇数)。
var layoutPage, layoutSize = 1, 10
func customLayout(c *ui.Context) {
ui.Pagination(c, &layoutPage, 400, layoutSize).
SizeChanger(&layoutSize).
PagerCount(5).
Layout(ui.PaginationPrev, ui.PaginationPager, ui.PaginationNext,
ui.PaginationSpacer, ui.PaginationSizes, ui.PaginationTotal).
Show()
}主题与样式
| 风格 | 差异 |
|---|---|
| Ant Design | 当前页为描边按钮,其他页码无底色;Background 默认关闭。 |
| Element Plus | 默认 Background(true),页码为填充方块,当前页主色填充。 |
| shadcn | 页码是幽灵按钮,当前页为描边按钮;上一页 / 下一页按钮在图标旁带文字(小尺寸和简洁模式只显示图标)。 |
组件令牌在 theme.Components.Pagination(theme.PaginationToken):ItemSize / ItemSizeSM(按钮尺寸)、ItemBg、ItemColor、ItemHoverBg、ItemActiveBg、ItemActiveColor、ItemActiveBorderColor,以及背景模式的 ItemFilledBg、ItemFilledColor、ItemFilledActiveBg、ItemFilledActiveColor。
ConfigProvider.Defaults("Pagination", ...) 支持的键:"showQuickJumper"、"showTotal"、"simple"、"hideOnSinglePage"、"background"、"disabled"(均为 bool),"size"(ui.ControlSize 或 "small" / "default")。
func pagerDefaults(c *ui.Context) {
ui.ConfigProvider(c).
Defaults("Pagination", ui.Props{"showTotal": true, "size": "small"}).
Body(func() {
ui.Pagination(c, ¤t, 200, 10).Show()
})
}注意事项
- 状态放在哪:当前页和每页条数由应用持有(包级变量或页面闭包),分页器只读写绑定的指针。
total变小后当前页超出范围时,分页器会把*page收回到最后一页。 - 触发加载:在
Changed()为 true 的那一帧发起数据请求;请求放在 goroutine 中,结果写回 UI 线程后重绘,见 后台数据与线程。 - 文案与语言:「共 N 条」「条/页」「跳至」等文字随
ConfigProvider.Locale切换。 - RTL:在 RTL 布局中分页器整体镜像,上一页 / 下一页的箭头方向随之翻转。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Pagination
func Pagination(c *Context, page *int, total, pageSize int) *PaginationWPagination adds a pager bound to *page (1-based).
ConfigProvider.Defaults("Pagination", ...) keys: "showQuickJumper", "showTotal", "simple", "hideOnSinglePage", "background", "disabled", "showLessItems" (bool), "size" (ControlSize or "small" | "default" | "large"), "align" ("start" | "center" | "end"), "pageSizeOptions" ([]int), "pagerCount" (int).
The page buttons follow @rc-component/pagination: every page up to 7 pages (5 with ShowLessItems), else the first, the last, the pages around the current one and ••• jumps of 5 (3) pages. The Element theme uses PagerCount(7) by default (el-pagination pager-count), Background on, and page sizes 10/20/30/40/50/100 (PaginationToken). With total 0 a single disabled page 1 shows, like antd.
Under shadcn/ui page links are ghost buttons, the current page an outline button and Previous / Next carry their label next to the chevron (icon only in the small and simple pagers).
返回 *PaginationW,可链式调用的方法见 PaginationW 方法。
PaginationW 方法
ui.PaginationW:PaginationW is a page navigator: page buttons with ellipsis jumps, total text, size changer, quick jumper, simple mode, hide on a single page, disabled state and Element's background (filled) style.
| 方法 | 说明 | 签名 |
|---|---|---|
Align | Align places the pager in its row: "start", "center" or "end" (antd align); "" leaves it at its natural width. | Align(a string) *PaginationW |
Background | Background toggles the filled page buttons (Element background; the default of the Element theme, off by default in Ant). | Background(on bool) *PaginationW |
Changed | Changed() bool | |
Disabled | Disabled disables every part of the pager. | Disabled(on bool) *PaginationW |
GoButton | GoButton adds a confirm button after the quick jumper input (antd showQuickJumper.goButton) and turns the jumper on; label "" uses Locale().Pagination.Confirm. | GoButton(label string) *PaginationW |
HideOnSinglePage | HideOnSinglePage hides the pager when there is at most one page. | HideOnSinglePage() *PaginationW |
Layout | Layout arranges the pager from parts in the given order (Element's layout "total, prev, pager, next, sizes, jumper"); only the listed parts show, whatever ShowTotal / ShowQuickJumper say. | Layout(parts ...PaginationPart) *PaginationW |
NextIcon | NextIcon(ic *icon.Icon) *PaginationW | |
NextText | NextText(s string) *PaginationW | |
PageSizeOptions | PageSizeOptions sets the choices of the size changer (antd pageSizeOptions, default 10/20/50/100; Element page-sizes, default 10/20/30/40/50/100). | PageSizeOptions(sizes ...int) *PaginationW |
PagerCount | PagerCount sets how many page-number buttons the pager shows at most (Element pager-count: an odd number from 5 to 21, others are rounded up to the next odd number in range). | PagerCount(n int) *PaginationW |
PrevIcon | PrevIcon / NextIcon replace the chevrons (Element prev-icon / next-icon, antd itemRender for prev / next). | PrevIcon(ic *icon.Icon) *PaginationW |
PrevText | PrevText / NextText show a text instead of the chevron in the previous / next buttons (Element prev-text / next-text). | PrevText(s string) *PaginationW |
ReadOnly | ReadOnly shows the current page of a Simple pager as text instead of an input ("< 2 / 5 >", Ant simple.readOnly): the narrowest pager, for embedding at the bottom of a list. | ReadOnly() *PaginationW |
Show | Show builds the pager. | Show() *PaginationW |
ShowLessItems | ShowLessItems shows fewer page buttons: one page on each side of the current one and ••• jumps of 3 pages (antd showLessItems). | ShowLessItems() *PaginationW |
ShowQuickJumper | ShowQuickJumper adds the "go to [ ] page" input (Enter or leaving the input jumps). | ShowQuickJumper() *PaginationW |
ShowTotal | ShowTotal() *PaginationW | |
ShowTotalFunc | ShowTotalFunc shows the text fn returns before the pager (antd showTotal(total, range)): from and to are the 1-based items of the current page (0, 0 when total is 0). | ShowTotalFunc(fn func(total, from, to int) string) *PaginationW |
Simple | Simple shows only prev, an editable current page, "/ pages" and next. | Simple() *PaginationW |
Size | Size picks the small pager (Small), the default one or the large one (Large: 40px buttons, antd 6 size="large", Element size="large"); Small with Simple is the narrow pager for list and transfer footers. | Size(s ControlSize) *PaginationW |
SizeChanger | SizeChanger shows a page-size select bound to *size. | SizeChanger(size *int) *PaginationW |
Small | Small() *PaginationW |