This module is a paginator of arrays for simplify the pagination with arrays.
This module split yours arrays in pages and you can recover this pages.
With npm :
npm install array-paginator
With yarn :
yarn add array-paginator
With CommonJS syntax :
const { Paginator } = require("array-paginator");
With module syntax :
import { Paginator } from "array-paginator";
Create a new Paginator with an array as a parameter and two optionals parameters : max items per page (default:5) and current page (default:1) :
const paginator = new Paginator(data, 15, 1);
Push data in array.
Parameters :
- newData : A new data (type :
any
).
Type : Boolean
paginator.push(data); // Push data
Set data in array.
Parameters :
- newData : An array with new data (type :
Array<any>
).
Type : Boolean
paginator.set(data); // Set data
Clear data in array.
Parameters :
- newData : An array with new data (type :
Array<any>
).
Type : Boolean
paginator.clear(data); // Clear data
Return a specific page.
Parameters :
- page : The page to get (type :
number
)
Type : Array
const page = paginator.page(1); // Get page one
Return the first page.
Type : Array
const firstPage = paginator.first(); // Get first page
Return the last page.
Type : Array
const lastPage = paginator.last(); // Get last page
Return the next page.
Type : Array
const nextPage = paginator.next(); // Get next page
Return the previous page.
Type : Array
const previousPage = paginator.previous(); // Get previous page
Return true if first page exist.
Type : Boolean
const previousPage = paginator.previous(); // Get previous page
Return true if next page exist.
Type : Boolean
const hasNextPage = paginator.hasNext(); // true if next page exists
Return true if previous page exist.
Type : Boolean
const hasPreviousPage = paginator.hasPrevious(); // true if previous page exists
Return true if last page exist.
Type : Boolean
const hasLastPage = paginator.hasLast(); // true if last page exists
The current page.
Type : Number
Return all data.
Type : Array
The total of pages.
Type : Number
const { Paginator } = require("array-paginator");
const data = Array.from(new Array(50).keys());
const pager = new Paginator(data, 10);
pager.page(2); // [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
pager.first(); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
pager.last(); // [40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
pager.next(); // [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
pager.previous(); // undefined
pager.hasNext(); // true
pager.hasPrevious(); // false
pager.current; //5
pager.all; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... 49]
pager.total; //5