Update pagination to handle any number of pages
This commit is contained in:
parent
9e3e89eb06
commit
da1936fbbf
|
@ -1,11 +1,13 @@
|
||||||
<script>
|
<script>
|
||||||
export let data
|
export let data
|
||||||
export let currentPage
|
export let currentPage = 0
|
||||||
export let pageItemCount
|
export let pageItemCount
|
||||||
export let ITEMS_PER_PAGE
|
export let ITEMS_PER_PAGE
|
||||||
|
|
||||||
let numPages = 0
|
let numPages = 0
|
||||||
$: numPages = Math.ceil((data?.length ?? 0) / ITEMS_PER_PAGE)
|
$: numPages = Math.ceil((data?.length ?? 0) / ITEMS_PER_PAGE)
|
||||||
|
$: displayAllPages = numPages <= 10
|
||||||
|
$: pagesAroundCurrent = getPagesAroundCurrent(currentPage, numPages)
|
||||||
|
|
||||||
const next = () => {
|
const next = () => {
|
||||||
if (currentPage + 1 === numPages) return
|
if (currentPage + 1 === numPages) return
|
||||||
|
@ -20,11 +22,23 @@
|
||||||
const selectPage = page => {
|
const selectPage = page => {
|
||||||
currentPage = page
|
currentPage = page
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPagesAroundCurrent(current, max) {
|
||||||
|
console.log(current)
|
||||||
|
const start = Math.max(current - 2, 1)
|
||||||
|
const end = Math.min(current + 2, max - 2)
|
||||||
|
let pages = []
|
||||||
|
for (let i = start; i <= end; i++) {
|
||||||
|
pages.push(i)
|
||||||
|
}
|
||||||
|
return pages
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="pagination">
|
<div class="pagination">
|
||||||
<div class="pagination__buttons">
|
<div class="pagination__buttons">
|
||||||
<button on:click={previous} disabled={currentPage === 0}><</button>
|
<button on:click={previous} disabled={currentPage === 0}><</button>
|
||||||
|
{#if displayAllPages}
|
||||||
{#each Array(numPages) as _, idx}
|
{#each Array(numPages) as _, idx}
|
||||||
<button
|
<button
|
||||||
class:selected={idx === currentPage}
|
class:selected={idx === currentPage}
|
||||||
|
@ -32,6 +46,29 @@
|
||||||
{idx + 1}
|
{idx + 1}
|
||||||
</button>
|
</button>
|
||||||
{/each}
|
{/each}
|
||||||
|
{:else}
|
||||||
|
<button class:selected={currentPage === 0} on:click={() => selectPage(0)}>
|
||||||
|
1
|
||||||
|
</button>
|
||||||
|
{#if currentPage > 3}
|
||||||
|
<button disabled>...</button>
|
||||||
|
{/if}
|
||||||
|
{#each pagesAroundCurrent as idx}
|
||||||
|
<button
|
||||||
|
class:selected={idx === currentPage}
|
||||||
|
on:click={() => selectPage(idx)}>
|
||||||
|
{idx + 1}
|
||||||
|
</button>
|
||||||
|
{/each}
|
||||||
|
{#if currentPage < numPages - 4}
|
||||||
|
<button disabled>...</button>
|
||||||
|
{/if}
|
||||||
|
<button
|
||||||
|
class:selected={currentPage === numPages - 1}
|
||||||
|
on:click={() => selectPage(numPages - 1)}>
|
||||||
|
{numPages}
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
<button
|
<button
|
||||||
on:click={next}
|
on:click={next}
|
||||||
disabled={currentPage === numPages - 1 || numPages === 0}>
|
disabled={currentPage === numPages - 1 || numPages === 0}>
|
||||||
|
@ -65,7 +102,8 @@
|
||||||
|
|
||||||
.pagination__buttons button {
|
.pagination__buttons button {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
padding: var(--spacing-s) var(--spacing-m);
|
padding: var(--spacing-s) 0;
|
||||||
|
text-align: center;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
background: #fff;
|
background: #fff;
|
||||||
border: none;
|
border: none;
|
||||||
|
@ -74,6 +112,9 @@
|
||||||
text-transform: capitalize;
|
text-transform: capitalize;
|
||||||
min-width: 20px;
|
min-width: 20px;
|
||||||
transition: 0.3s background-color;
|
transition: 0.3s background-color;
|
||||||
|
font-family: var(--font-sans);
|
||||||
|
color: var(--grey-6);
|
||||||
|
width: 40px;
|
||||||
}
|
}
|
||||||
.pagination__buttons button:last-child {
|
.pagination__buttons button:last-child {
|
||||||
border-right: none;
|
border-right: none;
|
||||||
|
@ -83,11 +124,13 @@
|
||||||
background-color: var(--grey-1);
|
background-color: var(--grey-1);
|
||||||
}
|
}
|
||||||
.pagination__buttons button.selected {
|
.pagination__buttons button.selected {
|
||||||
background: var(--grey-2);
|
background: var(--blue);
|
||||||
|
color: white;
|
||||||
}
|
}
|
||||||
|
|
||||||
p {
|
p {
|
||||||
font-size: var(--font-size-s);
|
font-size: var(--font-size-s);
|
||||||
margin: var(--spacing-xl) 0;
|
margin: var(--spacing-xl) 0;
|
||||||
|
color: var(--grey-6);
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
Loading…
Reference in New Issue