-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathNPagination.vue
110 lines (92 loc) · 2.91 KB
/
NPagination.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<script setup lang="ts">
import { computed } from 'vue'
import { useVModel } from '@vueuse/core'
const props = withDefaults(defineProps<{
modelValue: number
total: number
pageCount?: number
max?: number
divider?: string
}>(), {
pageCount: 5,
divider: '...',
max: 10,
})
const emit = defineEmits<{ (...args: any): void }>()
const currentPage = useVModel(props, 'modelValue', emit, { passive: true })
const pages = computed(() => Array.from({ length: Math.ceil(props.total / props.pageCount) }, (_, i) => i + 1))
const displayedPages = computed(() => {
if (!props.max || pages.value.length <= 5) {
return pages.value
}
else {
const current = currentPage.value
const max = pages.value.length
const r = Math.floor((Math.min(props.max, max) - 5) / 2)
const r1 = current - r
const r2 = current + r
const beforeWrapped = r1 - 1 > 1
const afterWrapped = r2 + 1 < max
const items: Array<number | string> = [1]
if (beforeWrapped)
items.push(props.divider)
if (!afterWrapped) {
const addedItems = (current + r + 2) - max
for (let i = current - r - addedItems; i <= current - r - 1; i++)
items.push(i)
}
for (let i = r1 > 2 ? (r1) : 2; i <= Math.min(max, r2); i++)
items.push(i)
if (!beforeWrapped) {
const addedItems = 1 - (current - r - 2)
for (let i = current + r + 1; i <= current + r + addedItems; i++)
items.push(i)
}
if (afterWrapped)
items.push(props.divider)
if (r2 < max)
items.push(max)
// Replace divider by number on start edge case [1, '…', 3, ...]
if (items.length >= 3 && items[1] === props.divider && items[2] === 3)
items[1] = 2
// Replace divider by number on end edge case [..., 48, '…', 50]
if (items.length >= 3 && items[items.length - 2] === props.divider && items[items.length - 1] === items.length)
items[items.length - 2] = items.length - 1
return items
}
})
const hasPrev = computed(() => currentPage.value > 1)
const hasNext = computed(() => currentPage.value < pages.value.length)
function clickNext() {
if (!hasNext.value)
return
currentPage.value++
}
function clickPrev() {
if (!hasPrev.value)
return
currentPage.value--
}
</script>
<template>
<div flex="~ ">
<slot name="prev" @click="clickPrev">
<NButton rounded="l none" icon="carbon-chevron-left" @click="clickPrev" />
</slot>
<NButton
v-for="(page, index) of displayedPages"
:key="`${page}-${index}`"
rounded-none
:class="[
{ 'pointer-events-none': typeof page === 'string' },
{ 'border-primary text-primary': page === currentPage },
]"
:n="page === currentPage ? 'primary' : ''"
@click="currentPage = page as number"
v-text="page"
/>
<slot name="next" @click="clickNext">
<NButton rounded="r none" icon="carbon-chevron-right" @click="clickNext" />
</slot>
</div>
</template>