|
1 | 1 | <template> |
2 | 2 | <nav aria-label="Page navigation"> |
3 | 3 | <ul class="pagination mb-0"> |
4 | | - <li class="page-item" @click="changePage({ direction: 'previous' })" role="button"> |
| 4 | + <li class="page-item" @click="changePage({ direction: 'previous' })" role="button"> |
5 | 5 | <span class="page-link fw-bold arrow-button bg-dark-2" tabindex="-1" aria-disabled="true"><ChevronLeft /></span> |
6 | 6 | </li> |
7 | 7 | <li |
8 | 8 | v-for="n in pages" |
9 | 9 | :key="n" |
10 | 10 | :role="n === morePagesPlaceholder ? '' : 'button'" |
11 | 11 | class="page-item" |
12 | | - :class="{ |
13 | | - 'active': currentPage === n, |
14 | | - 'disabled': n === morePagesPlaceholder |
15 | | - }" |
| 12 | + :class="{ 'active': currentPage === n, 'disabled': n === morePagesPlaceholder }" |
16 | 13 | @click="changePage({ pageNo: n })" |
17 | 14 | > |
18 | | - <span class="page-list-item page-link fw-bold">{{ n }}</span> |
| 15 | + <span class="page-list-item bg-dark-2 page-link fw-bold">{{ n }}</span> |
19 | 16 | </li> |
20 | 17 | <li class="page-item" @click="changePage({ direction: 'next' })" role="button"> |
21 | 18 | <span class="page-link fw-bold arrow-button bg-dark-2"><ChevronRight /></span> |
|
24 | 21 | </nav> |
25 | 22 | </template> |
26 | 23 |
|
27 | | -<script> |
| 24 | +<script lang="ts" setup> |
| 25 | +import { computed, watch } from 'vue' |
28 | 26 | import ChevronLeft from "vue-material-design-icons/ChevronLeft.vue" |
29 | 27 | import ChevronRight from "vue-material-design-icons/ChevronRight.vue" |
30 | 28 |
|
31 | | -export default { |
32 | | - components: { ChevronLeft, ChevronRight }, |
33 | | - data() { |
34 | | - return { |
35 | | - currentPage: 1, |
36 | | - morePagesPlaceholder: "..." |
37 | | - } |
38 | | - }, |
39 | | - props: { |
40 | | - totalPages: { |
41 | | - type: Number, |
42 | | - required: true |
43 | | - }, |
44 | | - }, |
45 | | - mounted() { |
46 | | - if(this.$route.query.page) { |
47 | | - this.currentPage = parseInt(this.$route.query.page) |
48 | | - } |
49 | | - }, |
50 | | - methods: { |
51 | | - changePage(event) { |
52 | | - if(event.direction && event.direction === 'previous' && this.currentPage > 1) { |
53 | | - this.currentPage-- |
54 | | - } |
55 | | - else if(event.direction && event.direction === 'next' && this.currentPage < this.totalPages) { |
56 | | - this.currentPage++ |
57 | | - } |
58 | | - else if(event.pageNo && event.pageNo !== this.morePagesPlaceholder) { |
59 | | - this.currentPage = event.pageNo |
60 | | - } |
61 | | - }, |
62 | | - paginate(current_page, last_page) { |
63 | | - let pages = []; |
64 | | - for (let i = 1; i <= last_page; i++) { |
65 | | - let offset = 1; |
66 | | - if (i === 1 || (current_page - offset <= i && current_page + offset >= i) || i === current_page || i === last_page) { |
67 | | - pages.push(i); |
68 | | - } else if (i === current_page - (offset + 1) || i === current_page + (offset + 1)) { |
69 | | - pages.push(this.morePagesPlaceholder); |
70 | | - } |
71 | | - } |
72 | | - return pages; |
73 | | - } |
74 | | - }, |
75 | | - watch: { |
76 | | - currentPage(value) { |
77 | | - this.$emit('onPageChange', value) |
78 | | - }, |
79 | | - totalPages() { |
80 | | - this.currentPage = 1 |
81 | | - } |
82 | | - }, |
83 | | - computed: { |
84 | | - pages() { |
85 | | - return this.paginate(this.currentPage, this.totalPages) |
| 29 | +const morePagesPlaceholder = '...' as const |
| 30 | +
|
| 31 | +const props = defineProps<{ |
| 32 | + totalPages: number |
| 33 | +}>() |
| 34 | +
|
| 35 | +const currentPage = defineModel<number>('currentPage', { required: true }) |
| 36 | +
|
| 37 | +watch(() => props.totalPages, () => { |
| 38 | + currentPage.value = 1 |
| 39 | +}) |
| 40 | +
|
| 41 | +function changePage(event: { direction?: 'previous' | 'next', pageNo?: number | "..." }) { |
| 42 | + if (event.direction === 'previous' && currentPage.value > 1) { |
| 43 | + currentPage.value-- |
| 44 | + } else if (event.direction === 'next' && currentPage.value < props.totalPages) { |
| 45 | + currentPage.value++ |
| 46 | + } else if (event.pageNo && event.pageNo !== morePagesPlaceholder) { |
| 47 | + currentPage.value = event.pageNo |
| 48 | + } |
| 49 | +} |
| 50 | +
|
| 51 | +function paginate(current_page: number, last_page: number): (number | "...")[] { |
| 52 | + const pages: (number | "...")[] = [] |
| 53 | + for (let i = 1; i <= last_page; i++) { |
| 54 | + const offset = 1 |
| 55 | + if ( |
| 56 | + i === 1 || |
| 57 | + (current_page - offset <= i && current_page + offset >= i) || |
| 58 | + i === current_page || |
| 59 | + i === last_page |
| 60 | + ) { |
| 61 | + pages.push(i) |
| 62 | + } else if ( |
| 63 | + i === current_page - (offset + 1) || |
| 64 | + i === current_page + (offset + 1) |
| 65 | + ) { |
| 66 | + pages.push(morePagesPlaceholder) |
86 | 67 | } |
87 | 68 | } |
| 69 | + return pages |
88 | 70 | } |
| 71 | +
|
| 72 | +const pages = computed<(number | "...")[]>(() => paginate(currentPage.value, props.totalPages)) |
89 | 73 | </script> |
90 | 74 |
|
91 | 75 | <style scoped lang="scss"> |
|
0 commit comments