pagination-helper
TypeScript icon, indicating that this package has built-in type declarations

2.1.1 • Public • Published

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 - typescript

define the pagination helper class

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
})

get page numbers details and short them

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 }
    ]
*/

get page numbers details without shorting them

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.
*/

Get Page Number by Offset And Limit

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

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

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.
*/

Maintainers

Package Sidebar

Install

npm i pagination-helper

Weekly Downloads

1

Version

2.1.1

License

ISC

Unpacked Size

10.9 kB

Total Files

4

Last publish

Collaborators

  • alirezakargar