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:“前往 [ ] 頁”的跳轉輸入框,按 Enter 或失去焦點時跳轉。
寫了哪些就顯示哪些,顯示的順序和書寫的順序一致。下面每一行上方是這一行用的 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。