Giter Site home page Giter Site logo

svelte-paginate's Introduction

svelte-paginate

A Svelte plugin for paginating your data in no time.

Installation

npm install -D svelte-paginate

Usage

<script>
  import { paginate, LightPaginationNav } from 'svelte-paginate'

  let items = [...]
  let currentPage = 1
  let pageSize = 4
  $: paginatedItems = paginate({ items, pageSize, currentPage })
</script>

<ul class="items">
  {#each paginatedItems as item}
    <li class="item">
      {item}
    </li>
  {/each}
</ul>

<LightPaginationNav
  totalItems="{items.length}"
  pageSize="{pageSize}"
  currentPage="{currentPage}"
  limit="{1}"
  showStepOptions="{true}"
  on:setPage="{(e) => currentPage = e.detail.page}"
/>

In this example, we're paginating the data in the items list. Instead of displaying the list directly, we first pass it into the paginate function to return a subset of the list that we should display based on the current page and the page size.

Since we've defined paginatedItems as a computed data, it should update the displayed paginated items whenever the currentPage changes.

To navigate between pages you can either create your own navigation component or use one of the navigation components that come with svelte-paginate (such as <PaginationNav/>, <LightPaginationNav/>, or <DarkPaginationNav/>). Either way, you just need a way to update currentPage.

<PaginationNav/>

For this component to work, it needs to know:

  • totalItems: the total number of the original list (unpaginated list).
  • pageSize: the number of items displayed per page.
  • currentPage: the currently selected page.

This will display all page links in the navigation. If you want to limit the maximum number of the displayed links, use the limit prop.

limit

This prop takes a number that specifies how many items to display on each side. If you give it 1, for example, your nav will look like this:

1 … 4 5 6 … 13

In this case, 5 is the active page, and it has one page on each side, 4 and 6.

Let's see another example. This is how it will look if limit = 2:

1 … 4 5 6 7 8 … 13

In this example, 6 is the active page, and it has two pages on each side.

You can disable limit by setting it to null, which is the default value.

showStepOptions

If you set this prop to true, it will display the previous and next arrows on the navigation.

Customizing the styling of the navigation component

svelte-paginate comes with two themed navigation components: <LightPaginationNav/> and <DarkPaginationNav/>. Both components use <PaginationNav/> as the base navigation component. They are just modifying its CSS.

If you want to create your own version, wrap the <PaginationNav/> with a div and then use the following CSS selector:

  • .your-nav :global(.pagination-nav): The navigation container element.
  • .your-nav :global(.option): Each option in the navigation (including ellipsis, next and prev buttons).
  • .your-nav :global(.option.active): The currently active page number.
  • .your-nav :global(.option.ellipsis): The ellipsis option.
  • .your-nav :global(.option.number): Only page numbers.
  • .your-nav :global(.option.prev): The prev option.
  • .your-nav :global(.option.next): The next option.
  • .your-nav :global(.option.disabled): Targets the prev and next options when they are disabled (when you're on the first or last page).

Customizing the options of the navigation component

You can also change the content of each option type through slots. For example, if you want to display page numbers using this format: Page: {pageNumber}, you can do this:

<PaginationNav
  ...
  let:value="{pageNumber}"
>
  <span slot="number">
    Page: {pageNumber}
  </span>
</PaginationNav>

So using the number slot with its value, you can update how page numbers are displayed. Here's a list for the other option types:

  • Ellipsis: <span slot="ellipsis">
  • Previous: <span slot="prev">
  • Next: <span slot="next">

License

MIT

svelte-paginate's People

Contributors

matschik avatar tahash avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

svelte-paginate's Issues

Project maintenance

Hi @TahaSh.

I think we have some pending pull request untouched for years.
My suggestions is that allowing one of us to have access to merge this PR.

Thanks for svelte-paginate.

Uncaught TypeError: items is undefined

In the example code, the list of objects is in "items", which is used in paginatedItems. To fit this into an existing app that uses "articles", I have the following (I have left out a lot of the code)

let hero, articles;
...
async function fetchData() {
....
const [first, ...rest] = formattedArticles;
hero = first;
articles = rest;
}
onMount(fetchData);
$: paginatedItems = paginate({ articles, pageSize, currentPage })
.....
{#if articles}


{#each paginatedItems as article, index}
<Article {article} bordered={index !== articles.length - 1} />
{/each}

{/if}

When I run this in the browser, I get an error of
Uncaught TypeError: items is undefined
paginate paginate.js:2

Is this expecting the data to be in "items". I have essentially replaced "items" in the example with "articles"

Regards,

Update controls for accessibilty

svelte-paginate could be more accessible by making a few small changes.

  1. Update the <span class="option"> to be a <button> (remove default styling)
  2. Add aria-selected=true to the button that matches the active page
  3. Add an aria-label to the buttons e.g. Go to page 1
  4. Add a role and label to the container: <nav role="navigation" aria-label="Items Navigation">

3 & 4 could be passed in as props.

@TahaSh I can create a PR if you agree with these proposed changes?

For context, I use svelte-paginate on my site: http://aoelibrary.com/. I'm doing some work to make it accessible at the moment.

Repo: https://github.com/collinstommy/aoe-library

Thanks for making this package. It saved me a lot of work!

pass state to slot

If we use "next" or "prev" slot to change contents to <a href=...> instead of event, then there is no option check is element disabled or not.
It could be fixed by adding disabled prop to slots

Question no a bug

I am trying to connect the next, prev and page to send a request to the server, the server is specting a page number (req.params.page). How can I do this?

totalPages become undefined

I am using Svelte-Paginate to paginate at three places. However, at one particular invocation which is similar to other two

const totalPages = Math.ceil(totalItems / pageSize) line gives a NaN because totalItems become undefined for no good reason. I am using it with SSR using Sapper. Works fie at two other places.

Bug with using it in the newest version(s) of SvelteKit

Probably because of this change: sveltejs/kit#5332 the following error will occur:

/node_modules/svelte-paginate/src/index.js:1
export { default as paginate } from './paginate.js'
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:190:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)

Found a related issue: vitejs/vite#7130

Reinstalling did work on the first try, but after that still the same error.

Updating the package to ES Modules obviously doesn't fix the problem and will produce a Unknown file extension ".svelte" type error.

const { paginate, LightPaginationNav } = 'svelte-paginate'; wont' fix it neither.

svelte-paginate doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix.

Go to top after change currentPage

Hello.

Is there a way to go to the top of the page after navigating?
I need something like this:

function scrollTo(elId) {
  const el = document.getElementById(elId);
  el.scrollIntoView({
    behavior: 'smooth',
    block: 'start'
  });
};

export 'paginate' (imported as 'paginate') was not found in 'svelte-paginate'

Hey there,

today I was trying to use svelte-paginate in my Golang-Buffalo-Project, which runs a webpacke-configuration of svelte using svelte-loader as described in the official documentation.

I run into a loading/resolving error that I cannot figure out. I do NOT use TypeScript:

WARNING in ./assets/js/components/SearchApp.svelte 498:39-47
export 'paginate' (imported as 'paginate') was not found in 'svelte-paginate' (possible exports: DarkPaginationNav, LightPaginationNav, PaginationNav)
 @ ./assets/js/search.js 1:0-54 2:16-25

I see more consequent(?) errors:

ERROR in ./node_modules/svelte-paginate/generateNavigationOptions.js 1:0-37
Module not found: Error: Can't resolve './types' in '/Users/tim/Workspace/wescale/wapiti/node_modules/svelte-paginate'
Did you mean 'types.js'?
BREAKING CHANGE: The request './types' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request

and the same for paginate after that:

ERROR in ./node_modules/svelte-paginate/index.js 1:0-38
Module not found: Error: Can't resolve './paginate' in '/Users/tim/Workspace/wescale/wapiti/node_modules/svelte-paginate'
Did you mean 'paginate.js'?
BREAKING CHANGE: The request './paginate' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

I use this to import svelte-paginate:

  import { paginate, LightPaginationNav } from 'svelte-paginate'
  ...

  let results = [];
  let currentPage = 1;
  let pageSize = 50;
  $: paginatedItems = paginate({ items: results, pageSize, currentPage })

Any ideas?

on:click

how can implement on:click in the paginated anchors?

Hint: Could not find a declaration file for module './DarkPaginationNav.svelte'.

How to fix Hint: Could not find a declaration file for module './DarkPaginationNav.svelte'.?

> [email protected] check /home/username/softwares/learn/pokedex-svelte
> svelte-check --tsconfig ./tsconfig.json --workspace src


====================================
Loading svelte-check in workspace: /home/username/softwares/learn/pokedex-svelte/src
Getting Svelte diagnostics...

/home/username/softwares/learn/pokedex-svelte/src/../node_modules/.pnpm/file+..+..+forks+svelte-paginate/node_modules/svelte-paginate/src/main.ts:2:42
Hint: Could not find a declaration file for module './PaginationNav.svelte'. '/home/username/softwares/learn/pokedex-svelte/node_modules/.pnpm/file+..+..+forks+svelte-paginate/node_modules/svelte-paginate/src/PaginationNav.svelte' implicitly has an 'any' type.
export { default as paginate } from './paginate'
export { default as PaginationNav } from './PaginationNav.svelte'
export { default as LightPaginationNav } from './LightPaginationNav.svelte'


/home/username/softwares/learn/pokedex-svelte/src/../node_modules/.pnpm/file+..+..+forks+svelte-paginate/node_modules/svelte-paginate/src/main.ts:3:47
Hint: Could not find a declaration file for module './LightPaginationNav.svelte'. '/home/username/softwares/learn/pokedex-svelte/node_modules/.pnpm/file+..+..+forks+svelte-paginate/node_modules/svelte-paginate/src/LightPaginationNav.svelte' implicitly has an 'any' type.
export { default as PaginationNav } from './PaginationNav.svelte'
export { default as LightPaginationNav } from './LightPaginationNav.svelte'
export { default as DarkPaginationNav } from './DarkPaginationNav.svelte'


/home/username/softwares/learn/pokedex-svelte/src/../node_modules/.pnpm/file+..+..+forks+svelte-paginate/node_modules/svelte-paginate/src/main.ts:4:46
Hint: Could not find a declaration file for module './DarkPaginationNav.svelte'. '/home/username/softwares/learn/pokedex-svelte/node_modules/.pnpm/file+..+..+forks+svelte-paginate/node_modules/svelte-paginate/src/DarkPaginationNav.svelte' implicitly has an 'any' type.
export { default as LightPaginationNav } from './LightPaginationNav.svelte'
export { default as DarkPaginationNav } from './DarkPaginationNav.svelte'

@bas-baskara, Do you have this issue in your branch when running the svelte-check?
I have my own fork https://github.com/azzamsa/svelte-paginate/commits/mine which also contains your changes. But this error pops up.

If we could solve this issue we also able to patch https://github.com/TahaSh/vue-paginate.

Question

I am trying to add active class to the pagination on hashchange event. I can't figure this out.
I made some modifications and here is my repo and the pagination src is under src/components/UI/paginate
Do you know how can I accomplish this?

Style LightPaginationNav

Hello! Is there any way to add own styles to LightPaginationNav? I don't want to edit the package.

Thanks!

Is items a keyword

When trying to do $: paginatedItems = paginate({ _items, pageSize, currentPage }). When saying _items, it wont show anything. However when doing items instead of _items, it works. So is it something I am doing wrong? Or does it need "items"?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.