An open API service indexing awesome lists of open source software.

https://github.com/simonsinclair/pgr

A tiny pagination range creator for your pagination UI.
https://github.com/simonsinclair/pgr

jsx pager paginate pagination paginator paging react typescript vue

Last synced: 8 months ago
JSON representation

A tiny pagination range creator for your pagination UI.

Awesome Lists containing this project

README

          

# pgr

> A tiny pagination range creator for your pagination UI.

![npm bundle size](https://img.shields.io/bundlephobia/minzip/@simonsinclair/pgr)

## Install

```sh
npm i @simonsinclair/pgr
```

## Usage

```js
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);
```

```js
[2, 3, 4, 5, 6, 7, 8];
```

## Tips

### Using the callback parameter

For example, pass a JSX Element as a callback.

```jsx


    {pgr(totalPages, currentPage, PAGINATION_LENGTH, (pageNum) => (

  1. {pageNum}

  2. ))}

```

## 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.