Created
July 8, 2023 14:43
-
-
Save begoon/78c42eb24974ac3190e042f095119f58 to your computer and use it in GitHub Desktop.
Simple paginator for Svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
import PageLink from "./PageLink.svelte"; | |
export let pages: number; | |
export let current: number; | |
export let query: string | undefined; | |
</script> | |
{#if pages > 1} | |
{#if pages < 20} | |
{#each Array(pages) as _, i} | |
<PageLink {current} {i} {query} /> | |
{/each} | |
{:else} | |
<PageLink {current} i={0} {query} /> | |
{#if current > 3}...{/if} | |
{#each Array(5) as _, i} | |
{@const j = current - 2 + i} | |
{#if j >= 1 && j <= pages - 2} | |
<PageLink {current} i={j} {query} /> | |
{/if} | |
{/each} | |
{#if current < pages - 4}...{/if} | |
<PageLink {current} i={pages - 1} {query} /> | |
{/if} | |
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
export let current: number; | |
export let i: number; | |
export let query: string | undefined; | |
if (query) query = "&"+ query; | |
</script> | |
<a href="?page={i}{query}" class:current={i === current}>{i + 1}</a> | |
<style> | |
a { | |
padding: 0.2rem; | |
font-weight: normal; | |
} | |
a:first-child { | |
margin-left: 0.5rem; | |
} | |
a.current { | |
font-weight: bold; | |
background-color: #eee; | |
pointer-events: none; | |
text-decoration: none; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment