Git Product home page Git Product logo

pgr's Introduction

pgr

A tiny pagination range creator for your pagination UI.

npm bundle size

Install

npm i @simonsinclair/pgr

Usage

const { default: pgr } = require('@simonsinclair/pgr');

const ARTICLES_PER_PAGE = 20;
const PAGINATION_LENGTH = 7;

const articles = [
  { title: 'Article 1' },
  { title: 'Article 2' },
  // ...
];

const totalPages = Math.ceil(articles.length / ARTICLES_PER_PAGE);
const currentPage = 5;

const pagination = pgr(totalPages, currentPage, PAGINATION_LENGTH);

console.log(pagination);
[2, 3, 4, 5, 6, 7, 8];

Tips

Using the callback parameter

For example, pass a JSX Element as a callback.

<ol>
  {pgr(totalPages, currentPage, PAGINATION_LENGTH, (pageNum) => (
    <li key={pageNum}>
      <button>{pageNum}</button>
    </li>
  ))}
</ol>

API

pgr(totalPages, currentPage, length, callback?)

Returns an array of pages the specified length. pgr will attempt to center the array around the current page.

totalPages

Type: number.

The total number of pages.

currentPage

Type: number.

The current page.

length

Type: number.

The length of the pagination array.

callback

Type: Function.

Default: (page) => page.

A function that is called on each page in the range.

Supported browsers

IE11, and all modern browsers.

pgr's People

Contributors

simonsinclair avatar

Watchers

James Cloos avatar  avatar

pgr's Issues

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.