paginatedjs

A simple JS class to paginate arrays

Usage no npm install needed!

<script type="module">
  import paginatedjs from 'https://cdn.skypack.dev/paginatedjs';
</script>

README

PaginatedJS

Build Status

A simple JS class to paginate arrays.



Getting started

Installation

Browser

You just have to download the minify js file paginate-js.min.js, and import it into your HTML :

<script src="/path/to/paginate-js.min.js"></script>

NodeJS

Install from NPM or Yarn

npm i -S paginatedjs

# OR

yarn add paginatedjs

And import it into your NodeJS project

const Pagination = require("paginatedjs").Pagination

// OR

import { Pagination } from 'paginatedjs'

And... that's it ! :)

Usage

let list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
let perPage = 3

let page = []
const pagination = new Pagination(list, perPage)

// gives you [1,2,3]
pagination.firstPage()
page = pagination.getPaginated(true)

// gives you [13]
pagination.lastPage()
page = pagination.getPaginated(true)

// gives you [10,11,12]
pagination.prevPage()
page = pagination.getPaginated(true)

// gives you [13]
pagination.nextPage() // next page does not exists
page = pagination.getPaginated(true)

// gives you [4,5,6]
pagination.goToPage(2)
page = pagination.getPaginated(true)

// gives you [13]
pagination.goToPage(20) // page 20 does not exists
page = pagination.getPaginated(true)

// gives you [7,8,9]
pagination.firstPage().nextPage().nextPage()
page = pagination.getPaginated(true)

// gives you [1,2,3]
pagination.reset()
page = pagination.getPaginated(true)

// gives you [[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13]]
let chunked_list = pagination.chunkList()

// gives you {1: [1,2,3], 2: [4,5,6], 3: [7,8,9], 4: [10,11,12], 5: [13]}
let chunked_list = pagination.chunkList(true)




API

Pagination class

Properties

Nomenclature : {Return type} Class.property


Get the current page number

{Number} Pagination.pageNumber

Get the number of pages

{Number} Pagination.nbPages

Get the list

{Array} Pagination.list

Get the number of entries per page

{Number} Pagination.perPage



Methods

Nomenclature : {Return type} Class.method(<(optional) arg | type>, [<(optional) arg | type>])


Get list length

{Number} Pagination.count()

Returns true if pagination not ended, false otherwise

{Boolean} Pagination.hasMore()

Set pagination to the previous page

{Pagination} Pagination.prevPage()

Set pagination to the next page

{Pagination} Pagination.nextPage()

Set pagination to the first page

{Pagination} Pagination.firstPage()

Set pagination to the last page

{Pagination} Pagination.lastPage()

Returns chunked list compared to pagination position Returns Chunk class instance if to_array argument is false, array otherwise

{Chunk|Array} Pagination.getPaginated(<(optional) to_array | Boolean>)

Set pagination to the page number passed as argument

{Pagination} Pagination.goToPage(<page_number | Number>)

Reset pagination

{Pagination} Pagination.reset()

Returns a chunked array or page indexed object of the list

{Array|Object} Pagination.chunkList(
    <(optional) indexed_by_page | Boolean>, 
    <(optional) to_array | Boolean>
)



Chunk class

Methods

Nomenclature : {Return type} Class.method(<arg | type>, [<arg | type>])


Get count length

{Number} Chunk.count()

{Boolean} Chunk.empty()

{Boolean} Chunk.notEmpty()

Get the first element

{Mixed} Chunk.first()

Get the last element

{Mixed} Chunk.last()

Get nth element (begins to 1)

{Mixed} Chunk.nth(<n | Number>)

Returns true if list contains value passed as argument, false otherwise

{Boolean} Chunk.contains(<value | mixed>)

Returns chunked list as an array

{Array} Chunk.toArray()

Paginate the chunked list

{Pagination} Chunk.paginate(<perPage | Number>)