Skip to content

alirezakargar1380/pagination-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pagination-helper

CI

a simple pagination for calculating offset, limit and number of pages

Installation and Usage

Server-side usage

Install the library with npm install pagination-helper

yarn

yarn add pagination-helper

No ES6

var paginationHelper = require('pagination-helper');

ES6

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.

React

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 ]

Get Page Number by Offset And Limit

console.log(pagination.getPageNumberByOffsetAndLimit(30, 5)) // => 7, it means you are in page 7

Get Offset(Limit) And Limit(Take) by Page Number

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

Maintainers