a simple pagination for calculating offset, limit and number of pages
Install the library with npm install pagination-helper
yarn add pagination-helper
var paginationHelper = require('pagination-helper');
import pagination, { IPaginationHelper, IPageNumberDetails, ITakeAndSkip } from "pagination-helper";
const pagiantionHelper: IPaginationHelper = new pagination({
numberOfDataPerPage: 2, // number of data that you want to show them per page
current_page_number: 8, // the number of page that we are currently in
short: true
})
this function gives you the page number details, it also short them for you to can use them in your front end framework
const pageNumberDetails: IPageNumberDetails[] = pagiantionHelper.getDetailsOfTheNumberOfPages(100) /* 100 is the number of all of your data */
console.log(pageNumberDetails)
/* Output:
[
{ disabled: false, value: 1 },
{ disabled: true, value: '...' },
{ disabled: false, value: 5 },
{ disabled: false, value: 6 },
{ disabled: false, value: 7 },
{ disabled: false, value: 8 },
{ disabled: false, value: 9 },
{ disabled: false, value: 10 },
{ disabled: false, value: 11 },
{ disabled: true, value: '...' },
{ disabled: false, value: 50 }
]
*/
if you set short variable to false, pagination-helper
dosent short the number of pages
EXAMPLE:
pagiantionHelper.short = false
console.log(pagiantionHelper.getDetailsOfTheNumberOfPages(100))
/*
Output:
[
{ disabled: false, value: 1 },
{ disabled: false, value: 2 },
{ disabled: false, value: 3 },
{ disabled: false, value: 4 },
{ disabled: false, value: 5 },
{ disabled: false, value: 6 },
{ disabled: false, value: 7 },
{ disabled: false, value: 8 },
{ disabled: false, value: 9 },
{ disabled: false, value: 10 },
{ disabled: false, value: 11 },
{ disabled: false, value: 12 },
{ disabled: false, value: 13 },
{ disabled: false, value: 14 },
{ disabled: false, value: 15 },
{ disabled: false, value: 16 },
{ disabled: false, value: 17 },
{ disabled: false, value: 18 },
{ disabled: false, value: 19 },
{ disabled: false, value: 20 },
{ disabled: false, value: 21 },
{ disabled: false, value: 22 },
{ disabled: false, value: 23 },
{ disabled: false, value: 24 },
{ disabled: false, value: 25 },
{ disabled: false, value: 26 },
{ disabled: false, value: 27 },
{ disabled: false, value: 28 },
{ disabled: false, value: 29 },
{ disabled: false, value: 30 },
{ disabled: false, value: 31 },
{ disabled: false, value: 32 },
{ disabled: false, value: 33 },
{ disabled: false, value: 34 },
{ disabled: false, value: 35 },
{ disabled: false, value: 36 },
{ disabled: false, value: 37 },
{ disabled: false, value: 38 },
{ disabled: false, value: 39 },
{ disabled: false, value: 40 },
{ disabled: false, value: 41 },
{ disabled: false, value: 42 },
{ disabled: false, value: 43 },
{ disabled: false, value: 44 },
{ disabled: false, value: 45 },
{ disabled: false, value: 46 },
{ disabled: false, value: 47 },
{ disabled: false, value: 48 },
{ disabled: false, value: 49 },
{ disabled: false, value: 50 }
]
*/
console.log(pagiantionHelper.getNumberOfPages(98))
/*
Output: 49
it means that you have 49 pages
*/
console.log(pagiantionHelper.getNumberOfPages(98.5))
/*
Output: 50
it means that you have 49 pages, it support the float numbers.
*/
console.log(pagiantionHelper.getPageNumberByOffsetAndLimit(30, 5)) // => 4, it means you are in page 7
this function give you the number of your (take, skip) by page number, it's useful for when you want to write a query to get the data
const takeAndOffset: ITakeAndSkip = pagiantionHelper.getTakeAndSkip(7)
console.log(takeAndOffset)
/*
=> { take: 2, skip: 12 }, it means, if you want to go to page 7 set
set take 2 and skip 12 on your database query.
*/
- alireza kargar - alireza kargar (author)