Pagination 分页
数据量大、需要分页加载时使用。可以显示总条数、每页条数的选择、页码和跳转输入框,显示哪些部分、按什么顺序由 layout 决定。
引入
import { HuiPagination } from '@nscode/handy-ui'
基础用法
total 是数据总条数,当前页用 v-model:current-page 绑定,每页条数用 v-model:page-size 绑定。不写 layout 时全部显示:总条数、每页条数、上一页、页码、下一页、跳转。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiPagination } from '@nscode/handy-ui'
const currentPage = ref(1)
const pageSize = ref(10)
</script>
<template>
<HuiPagination v-model:current-page="currentPage" v-model:page-size="pageSize" :total="100" />
<p style="margin-top: 12px">当前页:{{ currentPage }},每页条数:{{ pageSize }}</p>
</template>
页码自持与受控
未传 currentPage 时从1开始自行保存页码,只监听 update:currentPage 也不会改变控制方式。传入属性即受控,显式 undefined 按第一页显示;点击、跳页和范围修正都发出请求,实际高亮、跳页框及上一页/下一页状态以父组件回填为准。
父组件不接收更新时保留原页码;total减少后若父页码仍超出范围,页码列表没有对应激活项,跳页框保留父值,请接收更新并同步查询。移除属性恢复此前内部页码,切换控制方式会给开发诊断;监听器增删不切换。下面只对比页码绑定,选择后重绘父级观察保存效果。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiPagination, HuiSpace } from '@nscode/handy-ui'
const stateBound = ref(2)
const stateRedraw = ref(0)
const stateLast = ref<number | string>('暂无')
const recordState = (page: number) => {
stateLast.value = page
}
</script>
<template>
<HuiSpace class="pagination-current-q1-demo" direction="vertical" fill :data-parent="stateRedraw">
<HuiSpace wrap>
<HuiButton class="page-redraw" @click="stateRedraw++">父级重绘 {{ stateRedraw }}</HuiButton>
<span>点第三页,再重绘父级比较实际页码。</span>
</HuiSpace>
<HuiSpace class="page-local" wrap>
<span>未传页码</span>
<HuiPagination :total="50" layout="prev, pager, next, jumper" />
</HuiSpace>
<HuiSpace class="page-listener" wrap>
<span>仅监听更新</span>
<HuiPagination
:total="50"
layout="prev, pager, next, jumper"
@update:current-page="recordState"
/>
</HuiSpace>
<HuiSpace class="page-empty" wrap>
<span>显式 undefined</span>
<HuiPagination :current-page="undefined" :total="50" layout="prev, pager, next, jumper" />
</HuiSpace>
<HuiSpace class="page-fixed" wrap>
<span>固定第二页,父组件不接收更新</span>
<HuiPagination :current-page="2" :total="50" layout="prev, pager, next, jumper" />
</HuiSpace>
<HuiSpace class="page-bound" wrap>
<span>双向绑定</span>
<HuiPagination
v-model:current-page="stateBound"
:total="50"
layout="prev, pager, next, jumper"
/>
</HuiSpace>
<span class="page-status">监听请求:{{ stateLast }};绑定页码:{{ stateBound }}</span>
</HuiSpace>
</template>
每页条数自持与受控
currentPage 与 pageSize 分别判断是否受控。未传 pageSize 或仅监听更新时,从10保存内部条数;显式传入(含 undefined)即受控,undefined 按10显示。父组件拒绝条数更新时,总页数和当前页不会跟着请求值变化。下面各行都绑定页码,便于观察接受条数后才发生的页码修正。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiPagination, HuiSpace } from '@nscode/handy-ui'
const sizeBound = ref(10)
const sizeRedraw = ref(0)
const sizeLast = ref<number | string>('暂无')
const sizePages = ref({ local: 9, listener: 9, empty: 9, fixed: 9, bound: 9 })
const recordSize = (size: number) => {
sizeLast.value = size
}
</script>
<template>
<HuiSpace class="pagination-size-q1-demo" direction="vertical" fill :data-parent="sizeRedraw">
<HuiSpace wrap>
<HuiButton class="size-redraw" @click="sizeRedraw++">父级重绘 {{ sizeRedraw }}</HuiButton>
<span>各行从第9页开始,选择50条,比较条数和实际页码。</span>
</HuiSpace>
<HuiSpace class="size-local" wrap>
<span>未传条数</span>
<HuiPagination
v-model:current-page="sizePages.local"
:total="100"
layout="sizes, pager, jumper"
/>
</HuiSpace>
<HuiSpace class="size-listener" wrap>
<span>仅监听条数更新</span>
<HuiPagination
v-model:current-page="sizePages.listener"
@update:page-size="recordSize"
:total="100"
layout="sizes, pager, jumper"
/>
</HuiSpace>
<HuiSpace class="size-empty" wrap>
<span>显式 undefined</span>
<HuiPagination
v-model:current-page="sizePages.empty"
:page-size="undefined"
:total="100"
layout="sizes, pager, jumper"
/>
</HuiSpace>
<HuiSpace class="size-fixed" wrap>
<span>固定10,父组件拒绝条数更新</span>
<HuiPagination
v-model:current-page="sizePages.fixed"
:page-size="10"
:total="100"
layout="sizes, pager, jumper"
/>
</HuiSpace>
<HuiSpace class="size-bound" wrap>
<span>双向绑定</span>
<HuiPagination
v-model:current-page="sizePages.bound"
v-model:page-size="sizeBound"
:total="100"
layout="sizes, pager, jumper"
/>
</HuiSpace>
<span class="size-status"
>监听请求:{{ sizeLast }};绑定条数:{{ sizeBound }};实际页码:{{ sizePages }}</span
>
</HuiSpace>
</template>
布局
layout 是用逗号隔开的字符串,可以用的项有 6 个:
total:共多少条。sizes:每页条数的下拉框。prev、next:上一页、下一页按钮。pager:页码。jumper:“前往 [ ] 页”的跳转输入框,按回车或失去焦点时跳转。
写了哪些就显示哪些,显示的顺序和书写的顺序一致。下面每一行上方是这一行用的 layout,几个分页共用同一个当前页:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiPagination, HuiSpace } from '@nscode/handy-ui'
const currentPage = ref(1)
</script>
<template>
<HuiSpace direction="vertical" fill>
<div>
<div class="demo-label">prev, pager, next</div>
<HuiPagination v-model:current-page="currentPage" :total="100" layout="prev, pager, next" />
</div>
<div>
<div class="demo-label">total, prev, pager, next</div>
<HuiPagination
v-model:current-page="currentPage"
:total="100"
layout="total, prev, pager, next"
/>
</div>
<div>
<div class="demo-label">sizes, prev, pager, next</div>
<HuiPagination
v-model:current-page="currentPage"
:total="100"
layout="sizes, prev, pager, next"
/>
</div>
<div>
<div class="demo-label">prev, pager, next, jumper</div>
<HuiPagination
v-model:current-page="currentPage"
:total="100"
layout="prev, pager, next, jumper"
/>
</div>
<div>
<div class="demo-label">prev, next(简洁模式)</div>
<HuiPagination v-model:current-page="currentPage" :total="100" layout="prev, next" />
</div>
<div>
<div class="demo-label">pager(只有页码)</div>
<HuiPagination v-model:current-page="currentPage" :total="100" layout="pager" />
</div>
<div>
<div class="demo-label">jumper, prev, pager, next, total(顺序跟着书写顺序走)</div>
<HuiPagination
v-model:current-page="currentPage"
:total="100"
layout="jumper, prev, pager, next, total"
/>
</div>
</HuiSpace>
</template>
<style scoped>
.demo-label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
页数很多时
总页数不超过 7 时页码全部显示。超过 7 时固定显示 7 个页码按钮:首页和末页一直显示,中间显示当前页附近的几页,其余的折叠成省略号。省略号只出现在确实有页码被折叠的一侧:当前页靠前时只有右边有,靠后时只有左边有,在中间时两边都有。
点下面的按钮跳到不同位置,看省略号的变化:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiPagination, HuiSpace } from '@nscode/handy-ui'
const currentPage = ref(50)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<HuiButton size="small" @click="currentPage = 1">第 1 页(只有右边折叠)</HuiButton>
<HuiButton size="small" @click="currentPage = 50">第 50 页(两边都折叠)</HuiButton>
<HuiButton size="small" @click="currentPage = 100">第 100 页(只有左边折叠)</HuiButton>
</HuiSpace>
<HuiPagination v-model:current-page="currentPage" :total="1000" layout="prev, pager, next" />
<HuiPagination :total="70" layout="prev, pager, next" />
</HuiSpace>
</template>
第二个分页只有 7 页,页码全部显示,不折叠。
每页条数
layout 里有 sizes 时显示每页条数的下拉框,选项由 page-sizes 指定。改变每页条数后,如果当前页超出了新的总页数,会自动回到最后一页:下面第一个分页现在在第 10 页,改成每页 100 条后只有 3 页,当前页变成 3。
当前的每页条数不在 page-sizes 里时,下拉框里会自动补上这一项,例如第二个分页每页 15 条。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiPagination, HuiSpace } from '@nscode/handy-ui'
const currentPage = ref(10)
const pageSize = ref(20)
const message = ref('')
const handleSizeChange = (size: number) => {
message.value = `sizeChange:每页条数改成了 ${size}`
}
const otherPage = ref(1)
const otherSize = ref(15)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiPagination
v-model:current-page="currentPage"
v-model:page-size="pageSize"
:total="235"
:page-sizes="[20, 50, 100]"
layout="total, sizes, prev, pager, next"
@size-change="handleSizeChange"
/>
<p>当前第 {{ currentPage }} 页,每页 {{ pageSize }} 条。{{ message }}</p>
<HuiPagination
v-model:current-page="otherPage"
v-model:page-size="otherSize"
:total="235"
:page-sizes="[10, 20, 50]"
layout="sizes, prev, pager, next"
/>
</HuiSpace>
</template>
带背景色
background 让页码按钮和上一页、下一页带上浅色底,悬停时变成主色底、白字(不带底色时悬停只有文字和边框变成主色)。可以和任意 layout 搭配。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiPagination, HuiSpace } from '@nscode/handy-ui'
const currentPage = ref(3)
</script>
<template>
<HuiSpace direction="vertical" fill>
<div>
<div class="demo-label">不带背景色</div>
<HuiPagination v-model:current-page="currentPage" :total="100" layout="prev, pager, next" />
</div>
<div>
<div class="demo-label">background</div>
<HuiPagination
v-model:current-page="currentPage"
:total="100"
background
layout="prev, pager, next"
/>
</div>
<div>
<div class="demo-label">background + 完整布局</div>
<HuiPagination v-model:current-page="currentPage" :total="100" background />
</div>
</HuiSpace>
</template>
<style scoped>
.demo-label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
不同尺寸
size 有 large、default、small 三种。页码、上一页、下一页的高度分别是 40、32、24px,和同尺寸的按钮一样高;字号是 16、14、12px,圆角是 8、6、4px。每页条数下拉框、跳转输入框、总条数和“前往”“页”这些文字也跟着变。不写 size 时跟随 ConfigProvider,都没有时是 default。
下面前三行是完整布局,后三行是带背景色的页码:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiPagination, HuiSpace } from '@nscode/handy-ui'
const currentPage = ref(1)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">large</span>
<HuiPagination v-model:current-page="currentPage" :total="100" size="large" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">default</span>
<HuiPagination v-model:current-page="currentPage" :total="100" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">small</span>
<HuiPagination v-model:current-page="currentPage" :total="100" size="small" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">large</span>
<HuiPagination
v-model:current-page="currentPage"
:total="100"
size="large"
background
layout="prev, pager, next"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">default</span>
<HuiPagination
v-model:current-page="currentPage"
:total="100"
background
layout="prev, pager, next"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">small</span>
<HuiPagination
v-model:current-page="currentPage"
:total="100"
size="small"
background
layout="prev, pager, next"
/>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
原来的 small 属性已不推荐,请改用 size="small"。它还能用,写 small 等同于 size="small";同时写了 size 时以 size 为准。
禁用
disabled 为 true 时整个分页不能操作:页码、上一页、下一页、每页条数下拉框和跳转框都变成禁用样式,页码按钮也不能用 Tab 聚焦。
<template>
<HuiSpace direction="vertical" fill>
<HuiPagination :current-page="3" :total="100" disabled />
<HuiPagination :current-page="3" :total="100" background disabled layout="prev, pager, next" />
</HuiSpace>
</template>
只有一页时隐藏
hide-on-single-page 为 true 时,总页数只有一页就不渲染分页。下面每页 10 条,总条数是 5 时只有一页,分页不显示。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiPagination, HuiSpace } from '@nscode/handy-ui'
const currentPage = ref(1)
const total = ref(5)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiButton @click="total = total > 10 ? 5 : 50">切换总条数(现在 {{ total }})</HuiButton>
<HuiPagination
v-model:current-page="currentPage"
:total="total"
hide-on-single-page
layout="total, prev, pager, next"
/>
</HuiSpace>
</template>
监听翻页
用户翻页时触发 change,参数是新的页码;改每页条数时触发 sizeChange。一般在这里请求新一页的数据。下面用定时器模拟请求,事件按触发顺序列出来:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiPagination, HuiSpace } from '@nscode/handy-ui'
const currentPage = ref(1)
const pageSize = ref(10)
const status = ref('点页码、上一页、下一页,或者改每页条数试试')
const events = ref<string[]>([])
const record = (text: string) => {
events.value = [text, ...events.value].slice(0, 5)
}
const loadData = () => {
status.value = `正在请求第 ${currentPage.value} 页,每页 ${pageSize.value} 条……`
const page = currentPage.value
window.setTimeout(() => {
if (page === currentPage.value) status.value = `已加载第 ${page} 页`
}, 500)
}
const handleChange = (page: number) => {
record(`change:${page}`)
loadData()
}
const handleSizeChange = (size: number) => {
record(`sizeChange:${size}`)
currentPage.value = 1
loadData()
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiPagination
v-model:current-page="currentPage"
v-model:page-size="pageSize"
:total="200"
@change="handleChange"
@size-change="handleSizeChange"
/>
<p>{{ status }}</p>
<p v-if="events.length">最近的事件:{{ events.join(',') }}</p>
</HuiSpace>
</template>
与外层表单的边界
分页是浏览操作:页容量和跳页控件不继承外层 Form 的禁用、只读或校验,不登记 FormItem 标签。下方业务输入仍继承表单,标签不会指向分页内部控件。分页自身 disabled 与 size 仍生效,未传尺寸仍按原 ConfigProvider 链取值。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiForm, HuiFormItem, HuiInput, HuiSpace, HuiPagination } from '@nscode/handy-ui'
const boundaryPage = ref(1)
const boundaryPageSize = ref(10)
</script>
<template>
<HuiForm disabled readonly size="large">
<HuiFormItem label="业务输入">
<HuiSpace direction="vertical" fill>
<HuiPagination
v-model:current-page="boundaryPage"
v-model:page-size="boundaryPageSize"
:total="100"
layout="sizes,prev,pager,next,jumper"
size="small"
/>
<HuiInput model-value="业务值" />
</HuiSpace>
</HuiFormItem>
</HuiForm>
</template>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| total | number | 0 | 数据总条数。小于等于 0 时按只有一页处理 |
| currentPage | number | 1 | 当前页码,支持 v-model:current-page |
| pageSize | number | 10 | 未传或仅监听时从10自持;传入即受控(含undefined,按10显示)。页数读取实际条数;小于等于0按一页处理 |
| pageSizes | number[] | [10, 20, 30, 40, 50, 100] | 每页条数下拉框的选项。当前的 pageSize 不在里面时会自动补进去 |
| layout | string | 'total, sizes, prev, pager, next, jumper' | 显示哪些部分和它们的顺序,用逗号隔开。可用的项:total、sizes、prev、pager、next、jumper |
| background | boolean | false | 页码按钮和上一页、下一页是否带底色 |
| disabled | boolean | false | 是否禁用 |
| size | 'large' | 'default' | 'small' | - | 尺寸,页码按钮高 40 / 32 / 24px。不写时跟随 ConfigProvider,都没有时是 'default' |
| small | boolean | false | 已不推荐,请用 size="small"。是否用小尺寸,同时写了 size 时以 size 为准 |
| hideOnSinglePage | boolean | false | 只有一页时是否隐藏整个分页 |
Events
| 事件 | 回调参数 | 说明 |
|---|---|---|
| update:currentPage | (value: number) | 当前页变化时触发,v-model:current-page 用的就是它。除了用户翻页,当前页因为总页数变少被拉回最后一页时也会触发 |
| update:pageSize | (value: number) | 用户改变每页条数时触发,v-model:page-size 用的就是它 |
| change | (currentPage: number) | 用户翻页时触发(点页码、上一页、下一页,或在跳转框里输入页码) |
| sizeChange | (pageSize: number) | 用户改变每页条数时触发 |
每页条数事件公开声明和实际发射统一为 sizeChange。Vue模板仍可写 @size-change,渲染函数使用 onSizeChange,只发新声明事件。原先先 update:pageSize、再条数通知、最后按父组件实际接收条数修正页码的顺序与参数保持。
类型
import type { PaginationProps, PaginationLayoutItem, PaginationSize } from '@nscode/handy-ui'
type PaginationLayoutItem = 'total' | 'sizes' | 'prev' | 'pager' | 'next' | 'jumper'
type PaginationSize = 'large' | 'default' | 'small'
interface PaginationProps {
total: number
pageSize?: number
currentPage?: number
pageSizes?: number[]
layout?: string
background?: boolean
disabled?: boolean
size?: PaginationSize
/** @deprecated 用 size="small" */
small?: boolean
hideOnSinglePage?: boolean
}
另外还导出了 PaginationEmits。