https://github.com/betaweb/paginatedjs
A simple JS class to paginate arrays
https://github.com/betaweb/paginatedjs
array arrays browser javascript javascript-library node node-js node-module nodejs paginable paginated pagination simple
Last synced: 10 months ago
JSON representation
A simple JS class to paginate arrays
- Host: GitHub
- URL: https://github.com/betaweb/paginatedjs
- Owner: betaWeb
- Created: 2018-12-13T19:17:32.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T07:57:04.000Z (about 3 years ago)
- Last Synced: 2025-04-02T15:01:57.879Z (10 months ago)
- Topics: array, arrays, browser, javascript, javascript-library, node, node-js, node-module, nodejs, paginable, paginated, pagination, simple
- Language: JavaScript
- Size: 1.21 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# PaginatedJS
[](https://travis-ci.org/betaWeb/paginatedjs)
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 :
```html
```
#### NodeJS
Install from NPM or Yarn
```BASH
$ npm i -S paginatedjs
# OR
$ yarn add paginatedjs
```
And import it into your NodeJS project
```javascript
const Pagination = require("paginatedjs").Pagination
// OR
import { Pagination } from 'paginatedjs'
```
And... that's it ! :)
### Usage
```javascript
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
```javascript
{Number} Pagination.pageNumber
```
Get the number of pages
```javascript
{Number} Pagination.nbPages
```
Get the list
```javascript
{Array} Pagination.list
```
Get the number of entries per page
```javascript
{Number} Pagination.perPage
```
#### Methods
Nomenclature : `{Return type} Class.method(<(optional) arg | type>, [<(optional) arg | type>])`
Get list length
```javascript
{Number} Pagination.count()
```
Get last page chunk length
```javascript
{Number} Pagination.countLastPage()
```
Returns true if pagination not ended, false otherwise
```javascript
{Boolean} Pagination.hasMore()
```
Set pagination to the previous page
```javascript
{Pagination} Pagination.prevPage()
```
Set pagination to the next page
```javascript
{Pagination} Pagination.nextPage()
```
Set pagination to the first page
```javascript
{Pagination} Pagination.firstPage()
```
Set pagination to the last page
```javascript
{Pagination} Pagination.lastPage()
```
Returns chunked list compared to pagination position
Returns Chunk class instance if to_array argument is false, array otherwise
```javascript
{Chunk|Array} Pagination.getPaginated(<(optional) to_array | Boolean>)
```
Set pagination to the page number passed as argument
```javascript
{Pagination} Pagination.goToPage()
```
Reset pagination
```javascript
{Pagination} Pagination.reset()
```
Returns a chunked array or page indexed object of the list
```javascript
{Array|Object} Pagination.chunkList(
<(optional) indexed_by_page | Boolean>,
<(optional) to_array | Boolean>
)
```
### Chunk class
#### Methods
Nomenclature : `{Return type} Class.method(, [])`
Get count length
```javascript
{Number} Chunk.count()
```
```javascript
{Boolean} Chunk.empty()
```
```javascript
{Boolean} Chunk.notEmpty()
```
Get the first element
```javascript
{Mixed} Chunk.first()
```
Get the last element
```javascript
{Mixed} Chunk.last()
```
Get nth element (begins to 1)
```javascript
{Mixed} Chunk.nth()
```
Returns true if list contains value passed as argument, false otherwise
```javascript
{Boolean} Chunk.contains()
```
Get chunked list only with keys passed as arguments
/!\ Works only with array of objects
```javascript
{Chunk} Chunk.only()
```
Returns chunked list as an array
```javascript
{Array} Chunk.toArray()
```
Paginate the chunked list
```javascript
{Pagination} Chunk.paginate()
```