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 paginationHelper from "pagination-helper";
let pagination = new paginationHelper({
data_per_page: 10 // number of data that you want to show them per page
})
console.log(pagination.getNumberOfPages(209)) // => 21, it means that you have 21 pages
console.log(pagination.getNumberOfPages(20.9)) // => 3, it means that you have 3 pages, it support the float numbers.
if you want to map number of your page's in your react component you can go on like this:
import paginationHelper from "pagination-helper";
let pagination = new paginationHelper({
data_per_page: 10, // number of data that you want to show them per page
exportDataAsarray: true // export number of pages as array
})
console.log(pagination.getNumberOfPages(20.9)) // => [ 1, 2, 3 ]
console.log(pagination.getPageNumberByOffsetAndLimit(30, 5)) // => 7, it means you are in page 7
this method 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
console.log(pagination.getTakeAndSkip(7)) // => { take: 10, skip: 60 }, it means, if you want to go to page 7 set this data
// to your query
- alireza kargar - alireza kargar (author)