Skip to content

Commit 6921dc1

Browse files
committed
docs: add table pagination example
1 parent d038fb3 commit 6921dc1

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<script setup lang="ts">
2+
import type { TableColumn } from '@nuxt/ui'
3+
4+
type Payment = {
5+
id: string
6+
date: string
7+
email: string
8+
amount: number
9+
}
10+
const data = ref<Payment[]>([{
11+
id: '4600',
12+
date: '2024-03-11T15:30:00',
13+
14+
amount: 594
15+
}, {
16+
id: '4599',
17+
date: '2024-03-11T10:10:00',
18+
19+
amount: 276
20+
}, {
21+
id: '4598',
22+
date: '2024-03-11T08:50:00',
23+
24+
amount: 315
25+
}, {
26+
id: '4597',
27+
date: '2024-03-10T19:45:00',
28+
29+
amount: 529
30+
}, {
31+
id: '4596',
32+
date: '2024-03-10T15:55:00',
33+
34+
amount: 639
35+
}, {
36+
id: '4595',
37+
date: '2024-03-10T13:20:00',
38+
39+
amount: 428
40+
}, {
41+
id: '4594',
42+
date: '2024-03-10T11:05:00',
43+
44+
amount: 673
45+
}, {
46+
id: '4593',
47+
date: '2024-03-09T22:15:00',
48+
49+
amount: 382
50+
}, {
51+
id: '4592',
52+
date: '2024-03-09T20:30:00',
53+
54+
amount: 547
55+
}, {
56+
id: '4591',
57+
date: '2024-03-09T18:45:00',
58+
59+
amount: 291
60+
}, {
61+
id: '4590',
62+
date: '2024-03-09T16:20:00',
63+
64+
amount: 624
65+
}, {
66+
id: '4589',
67+
date: '2024-03-09T14:10:00',
68+
69+
amount: 438
70+
}, {
71+
id: '4588',
72+
date: '2024-03-09T12:05:00',
73+
74+
amount: 583
75+
}, {
76+
id: '4587',
77+
date: '2024-03-09T10:30:00',
78+
79+
amount: 347
80+
}, {
81+
id: '4586',
82+
date: '2024-03-09T08:15:00',
83+
84+
amount: 692
85+
}, {
86+
id: '4585',
87+
date: '2024-03-08T23:40:00',
88+
89+
amount: 419
90+
}, {
91+
id: '4584',
92+
date: '2024-03-08T21:25:00',
93+
94+
amount: 563
95+
}, {
96+
id: '4583',
97+
date: '2024-03-08T19:50:00',
98+
99+
amount: 328
100+
}, {
101+
id: '4582',
102+
date: '2024-03-08T17:35:00',
103+
104+
amount: 647
105+
}, {
106+
id: '4581',
107+
date: '2024-03-08T15:20:00',
108+
109+
amount: 482
110+
}])
111+
const columns: TableColumn<Payment>[] = [{
112+
accessorKey: 'id',
113+
header: '#',
114+
cell: ({ row }) => `#${row.getValue('id')}`
115+
}, {
116+
accessorKey: 'date',
117+
header: 'Date',
118+
cell: ({ row }) => {
119+
return new Date(row.getValue('date')).toLocaleString('en-US', {
120+
day: 'numeric',
121+
month: 'short',
122+
hour: '2-digit',
123+
minute: '2-digit',
124+
hour12: false
125+
})
126+
}
127+
}, {
128+
accessorKey: 'email',
129+
header: 'Email'
130+
}, {
131+
accessorKey: 'amount',
132+
header: () => h('div', { class: 'text-right' }, 'Amount'),
133+
cell: ({ row }) => {
134+
const amount = Number.parseFloat(row.getValue('amount'))
135+
const formatted = new Intl.NumberFormat('en-US', {
136+
style: 'currency',
137+
currency: 'EUR'
138+
}).format(amount)
139+
return h('div', { class: 'text-right font-medium' }, formatted)
140+
}
141+
}]
142+
const table = useTemplateRef('table')
143+
</script>
144+
145+
<template>
146+
<div class="w-full space-y-4 pb-4">
147+
<UTable
148+
ref="table"
149+
:data="data"
150+
:columns="columns"
151+
:tanstack-options="{ initialState: { pagination: { pageSize: 7 } } }"
152+
class="flex-1"
153+
/>
154+
<UPagination
155+
:default-page="(table?.tableApi.getState().pagination.pageIndex || 0) + 1"
156+
:items-per-page="table?.tableApi.getState().pagination.pageSize"
157+
:total="table?.tableApi.getFilteredRowModel().rows.length"
158+
class="w-fit mx-auto"
159+
@update:page="(p) => table?.tableApi.setPageIndex(p - 1)"
160+
/>
161+
</div>
162+
</template>

docs/content/3.components/table.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,17 @@ class: '!p-0'
404404
---
405405
::
406406

407+
### With pagination
408+
We already pass the `getPaginationRowModel` function to the table so you don't need to pass it again. There are different pagination approaches as explained in [Pagination Guide](https://tanstack.com/table/v8/docs/guide/pagination#pagination-guide). You can implement any of them by passing options to `tanstackOptions` prop. In the example below, we use the `initialState` prop to set the default page size and use `UPagination` component to control the pagination state.
409+
::component-example
410+
---
411+
prettier: true
412+
collapse: true
413+
name: 'table-pagination-example'
414+
class: '!p-0'
415+
---
416+
::
417+
407418
### With slots
408419

409420
You can use slots to customize the header and data cells of the table.

0 commit comments

Comments
 (0)