|
13 | 13 | offset = limit * (page - 1);
|
14 | 14 | currentPage = page;
|
15 | 15 | dispatch('change');
|
| 16 | + pages = pagination(currentPage, totalPages); |
16 | 17 | }
|
17 | 18 | }
|
18 | 19 |
|
|
21 | 22 | currentPage += 1;
|
22 | 23 | offset = limit * (currentPage - 1);
|
23 | 24 | dispatch('change');
|
| 25 | + pages = pagination(currentPage, totalPages); |
24 | 26 | } else if (direction === 'prev' && currentPage > 1) {
|
25 | 27 | currentPage -= 1;
|
26 | 28 | offset = limit * (currentPage - 1);
|
27 | 29 | dispatch('change');
|
| 30 | + pages = pagination(currentPage, totalPages); |
28 | 31 | }
|
29 | 32 | }
|
30 | 33 |
|
31 | 34 | let pages = pagination(currentPage, totalPages);
|
32 | 35 |
|
33 |
| - function pagination(current: number, total: number) { |
34 |
| - let delta = 2, |
35 |
| - left = current - delta, |
36 |
| - right = current + delta + 1, |
37 |
| - range = [], |
38 |
| - rangeWithDots = []; |
39 |
| -
|
40 |
| - for (let i = 1; i <= total; i++) { |
41 |
| - if (i == 1 || i == total || (i >= left && i < right)) { |
42 |
| - range.push(i); |
43 |
| - } |
44 |
| - } |
45 |
| -
|
46 |
| - rangeWithDots = range.reduce((prev, current, index) => { |
47 |
| - if (current - prev[index - 1] > delta) { |
48 |
| - prev.push('...'); |
49 |
| - } |
50 |
| - prev.push(current); |
51 |
| - return prev; |
52 |
| - }, []); |
53 |
| - return rangeWithDots; |
| 36 | + function pagination(page: number, total: number) { |
| 37 | + const pagesShown = 5; |
| 38 | + const start = Math.max( |
| 39 | + 1, |
| 40 | + Math.min(page - Math.floor((pagesShown - 3) / 2), total - pagesShown + 2) |
| 41 | + ); |
| 42 | + const end = Math.min( |
| 43 | + total, |
| 44 | + Math.max(page + Math.floor((pagesShown - 2) / 2), pagesShown - 1) |
| 45 | + ); |
| 46 | + return [ |
| 47 | + ...(start > 2 ? [1, '...'] : start > 1 ? [1] : []), |
| 48 | + ...Array.from({ length: end + 1 - start }, (_, i) => i + start), |
| 49 | + ...(end < total - 1 ? ['...', total] : end < total ? [total] : []) |
| 50 | + ]; |
54 | 51 | }
|
55 | 52 | </script>
|
56 | 53 |
|
|
93 | 90 | <span class="icon-cheveron-right" aria-hidden="true" />
|
94 | 91 | </button>
|
95 | 92 | </nav>
|
| 93 | +{:else} |
| 94 | + <nav class="pagination"> |
| 95 | + <button class="button is-text is-disabled" aria-label="prev page"> |
| 96 | + <span class="icon-cheveron-left" aria-hidden="true" /> |
| 97 | + <span class="text">Prev</span> |
| 98 | + </button> |
| 99 | + <ol class="pagination-list is-only-desktop"> |
| 100 | + <li class="pagination-item"> |
| 101 | + <button class="button is-disabled" aria-label="page"> |
| 102 | + <span class="text">1</span> |
| 103 | + </button> |
| 104 | + </li> |
| 105 | + </ol> |
| 106 | + <button class="button is-text is-disabled" aria-label="next page"> |
| 107 | + <span class="text">Next</span> |
| 108 | + <span class="icon-cheveron-right" aria-hidden="true" /> |
| 109 | + </button> |
| 110 | + </nav> |
96 | 111 | {/if}
|
0 commit comments