mirror of
https://github.com/ThatGuySam/doesitarm.git
synced 2026-05-18 06:44:46 -07:00
Add PaginatedList helper
This commit is contained in:
parent
fef9002c8f
commit
dea22f7650
1 changed files with 44 additions and 0 deletions
44
helpers/api/pagination.js
Normal file
44
helpers/api/pagination.js
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
export class PaginatedList {
|
||||||
|
constructor({ list, perPage }) {
|
||||||
|
this.list = list
|
||||||
|
this.perPage = perPage
|
||||||
|
}
|
||||||
|
|
||||||
|
get total () {
|
||||||
|
return this.list.length
|
||||||
|
}
|
||||||
|
|
||||||
|
get pageCount () {
|
||||||
|
return Math.ceil( this.total / this.perPage )
|
||||||
|
}
|
||||||
|
|
||||||
|
makePageItems ( pageNumber ) {
|
||||||
|
const start = (pageNumber - 1) * this.perPage
|
||||||
|
const end = start + this.perPage
|
||||||
|
|
||||||
|
return this.list.slice(start, end)
|
||||||
|
}
|
||||||
|
|
||||||
|
makePage ( pageNumber ) {
|
||||||
|
const items = this.makePageItems( pageNumber )
|
||||||
|
|
||||||
|
return {
|
||||||
|
number: pageNumber,
|
||||||
|
items,
|
||||||
|
json: JSON.stringify( items )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get pages () {
|
||||||
|
// Create an empty array of pages
|
||||||
|
const pages = Array( this.pageCount ).fill({})
|
||||||
|
|
||||||
|
return pages.map( ( _, index ) => {
|
||||||
|
const pageNumber = index + 1
|
||||||
|
|
||||||
|
return this.makePage( pageNumber )
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue