-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathPaginationStrip.vue
More file actions
148 lines (129 loc) · 3.28 KB
/
Copy pathPaginationStrip.vue
File metadata and controls
148 lines (129 loc) · 3.28 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<script setup lang="ts">
import { computed, watch } from "vue";
const props = withDefaults(
defineProps<{
itemsPerPage: number;
totalCount: number;
pageBuffer?: number;
}>(),
{ pageBuffer: 5 }
);
const pageNumber = defineModel<number>({ required: true });
const numberOfPages = computed(() => {
return Math.ceil(props.totalCount / props.itemsPerPage);
});
const showPagination = computed(() => {
return numberOfPages.value > 1;
});
const doublePageBuffer = computed(() => props.pageBuffer * 2);
watch(numberOfPages, (newValue) => {
if (newValue < pageNumber.value) {
pageNumber.value = 1;
}
});
interface PageData {
label: string;
page: number;
key: string;
class?: {
disabled?: boolean;
active?: boolean;
};
}
const pages = computed(() => {
const pages: PageData[] = [];
pages.push({
label: "Previous",
page: Math.max(pageNumber.value - 1, 1),
key: "Previous Page",
class: {
disabled: pageNumber.value === 1,
},
});
if (pageNumber.value > props.pageBuffer + 1 && numberOfPages.value >= doublePageBuffer.value) {
pages.push(
{
label: "1",
page: 1,
key: "First Page",
},
{
label: "...",
page: pageNumber.value - props.pageBuffer,
key: `Back ${props.pageBuffer}`,
}
);
}
let startIndex = pageNumber.value - props.pageBuffer;
let endIndex = pageNumber.value + props.pageBuffer;
if (startIndex < 1) {
// Increase the end index by the offset
endIndex -= startIndex;
startIndex = 1;
}
let showEnd = false;
if (endIndex >= numberOfPages.value) {
endIndex = numberOfPages.value;
showEnd = true;
}
// All of the surrounding pages
for (let n = startIndex; n <= endIndex; n++) {
pages.push({
label: `${n}`,
page: n,
key: `Page ${n}`,
class: {
active: n === pageNumber.value,
},
});
}
if (!showEnd) {
pages.push(
{
label: "...",
page: pageNumber.value + props.pageBuffer,
key: `Forward ${props.pageBuffer}`,
},
{
label: `${numberOfPages.value}`,
page: numberOfPages.value,
key: "Last Page",
}
);
}
pages.push({
label: "Next",
page: Math.min(pageNumber.value + 1, numberOfPages.value),
key: "Next Page",
class: {
disabled: pageNumber.value === numberOfPages.value,
},
});
return pages;
});
</script>
<template>
<div v-if="showPagination" class="pagination-controls col align-self-center">
<ul aria-label="pagination" class="pagination justify-content-center">
<li v-for="page of pages" class="page-item" :key="page.key">
<button :aria-pressed="page.class?.active" :disabled="page.class?.disabled" :aria-label="page.key" class="page-link" @click="pageNumber = page.page" :class="page.class">{{ page.label }}</button>
</li>
</ul>
</div>
</template>
<style scoped>
.page-link {
cursor: pointer;
}
.page-link.disabled {
cursor: not-allowed;
pointer-events: all !important;
}
.pagination-controls {
display: flex;
justify-content: center;
}
.pagination-controls .page-link.active {
z-index: 0; /* pagination.scss has this set higher, for no obvious reason, which messes up with sticky-pagination over the top of other pagination */
}
</style>