|
2 | 2 | <nav v-if="shouldShowPagination">
|
3 | 3 | <ul class="pagination justify-content-center">
|
4 | 4 | <li :class="{ disabled: pagination.currentPage === 1 }">
|
5 |
| - <a |
6 |
| - :class="{ disabled: pagination.currentPage === 1 }" |
7 |
| - @click="pageClicked( pagination.currentPage - 1 )"> |
| 5 | + <a :class="{ disabled: pagination.currentPage === 1 }" |
| 6 | + @click="pageClicked( pagination.currentPage - 1 )"> |
8 | 7 | <i class="left chevron icon">«</i>
|
9 | 8 | </a>
|
10 | 9 | </li>
|
| 10 | + <li v-if="hasFirst" class="page-item" :class="{ active: isActive(1) }"> |
| 11 | + <a class="page-link" @click="pageClicked(1)">1</a> |
| 12 | + </li> |
| 13 | + <li v-if="hasFirstEllipsis"><span class="pagination-ellipsis">…</span></li> |
11 | 14 | <li class="page-item" :class="{ active: isActive(page), disabled: page === '...' }" v-for="page in pages">
|
12 | 15 | <a class="page-link" @click="pageClicked(page)">{{ page }}</a>
|
13 | 16 | </li>
|
| 17 | + <li v-if="hasLastEllipsis"><span class="pagination-ellipsis">…</span></li> |
| 18 | + <li v-if="hasLast" class="page-item" |
| 19 | + :class="{ active: isActive(this.pagination.totalPages) }"> |
| 20 | + <a class="page-link" @click="pageClicked(pagination.totalPages)">{{pagination.totalPages}}</a> |
| 21 | + </li> |
14 | 22 | <li>
|
15 |
| - <a |
16 |
| - :class="{ disabled: pagination.currentPage === this.pagination.totalPages }" |
17 |
| - @click="pageClicked( pagination.currentPage + 1 )"> |
| 23 | + <a :class="{ disabled: pagination.currentPage === pagination.totalPages }" |
| 24 | + @click="pageClicked( pagination.currentPage + 1 )"> |
18 | 25 | <i class="right chevron icon">»</i>
|
19 | 26 | </a>
|
20 | 27 | </li>
|
|
39 | 46 | : this.pageLinks();
|
40 | 47 | },
|
41 | 48 |
|
42 |
| - shouldShowPagination() { |
43 |
| - if (this.pagination.totalPages === undefined) { |
44 |
| - return false; |
45 |
| - } |
| 49 | + hasFirst() { |
| 50 | + return this.pagination.currentPage >= 4 || this.pagination.totalPages < 10 |
| 51 | + }, |
46 | 52 |
|
47 |
| - if (this.pagination.count === 0) { |
48 |
| - return false; |
49 |
| - } |
| 53 | + hasLast() { |
| 54 | + return this.pagination.currentPage <= this.pagination.totalPages - 3 || this.pagination.totalPages < 10 |
| 55 | + }, |
50 | 56 |
|
51 |
| - return this.pagination.totalPages > 1; |
| 57 | + hasFirstEllipsis() { |
| 58 | + return this.pagination.currentPage >= 4 && this.pagination.totalPages >= 10 |
| 59 | + }, |
| 60 | +
|
| 61 | + hasLastEllipsis() { |
| 62 | + return (this.pagination.currentPage < this.pagination.totalPages - 2 && |
| 63 | + this.pagination.currentPage <= this.pagination.totalPages - 3) && |
| 64 | + this.pagination.totalPages >= 10 |
52 | 65 | },
|
53 | 66 |
|
54 | 67 | shouldShowPagination() {
|
|
62 | 75 |
|
63 | 76 | return this.pagination.totalPages > 1;
|
64 | 77 | },
|
| 78 | +
|
65 | 79 | },
|
66 | 80 |
|
67 | 81 | methods: {
|
|
84 | 98 |
|
85 | 99 | pageLinks() {
|
86 | 100 | const pages = [];
|
87 |
| - let preDots = false; |
88 |
| - let postDots = false; |
89 |
| -
|
90 |
| - for (let i = 1; i <= this.pagination.totalPages; i++) { |
91 |
| -
|
92 |
| - if (this.pagination.totalPages <= 10) { |
93 |
| - pages.push(i); |
94 |
| - } else { |
95 |
| - if (i === 1) { |
96 |
| - pages.push(i); |
97 |
| - continue; |
98 |
| - } |
99 |
| -
|
100 |
| - if (i === this.pagination.totalPages) { |
101 |
| - pages.push(i); |
102 |
| - continue; |
103 |
| - } |
104 |
| -
|
105 |
| - if ( |
106 |
| - // link is within 4 of current |
107 |
| - (i > this.pagination.currentPage - 4 && i < this.pagination.currentPage + 4) || |
108 |
| - // current and link less than 4 |
109 |
| - (i < 4 && this.pagination.currentPage < 4) || |
110 |
| - // current and link within 4 of end |
111 |
| - (i > this.pagination.totalPages - 4 && this.pagination.currentPage > this.pagination.totalPages - 4)) { |
112 |
| - pages.push(i); |
113 |
| - continue; |
114 |
| - } |
115 |
| -
|
116 |
| - if (i < this.pagination.currentPage && !preDots) { |
117 |
| - pages.push('...'); |
118 |
| - preDots = true; |
119 |
| - continue; |
120 |
| - } |
121 |
| -
|
122 |
| - if (i > this.pagination.currentPage && !postDots) { |
123 |
| - pages.push('...'); |
124 |
| - postDots = true; |
125 |
| - } |
126 |
| - } |
| 101 | +
|
| 102 | + let left = 2; |
| 103 | + let right = this.pagination.totalPages - 1; |
| 104 | +
|
| 105 | + if (this.pagination.totalPages >= 10) { |
| 106 | + left = Math.max(1, this.pagination.currentPage - 2); |
| 107 | + right = Math.min(this.pagination.currentPage + 2, this.pagination.totalPages); |
| 108 | + } |
| 109 | +
|
| 110 | + for (let i = left; i <= right; i++) { |
| 111 | + pages.push(i); |
127 | 112 | }
|
128 | 113 |
|
129 | 114 | return pages;
|
|
0 commit comments