-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaginationHelper.js
38 lines (30 loc) · 1.08 KB
/
PaginationHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class PaginationHelper {
collection;
itemsPerPage;
constructor(collection, itemsPerPage) {
this.collection = collection;
this.itemsPerPage = itemsPerPage;
}
itemCount() {
return this.collection.length;
}
pageCount() {
let division = Math.floor(this.collection.length / this.itemsPerPage);
let resto = this.collection.length % this.itemsPerPage;
return resto === 0 ? division : division + 1;
}
pageItemCount(pageIndex) {
let comienzoPagIndex = this.itemsPerPage * pageIndex;
let finalPagIndex = comienzoPagIndex + this.itemsPerPage;
if (comienzoPagIndex >= this.collection.length || comienzoPagIndex < 0) {
return -1;
}else if (finalPagIndex >= this.collection.length) {
return this.collection.length - comienzoPagIndex;
} else {
return this.itemsPerPage;
}
}
pageIndex(itemIndex) {
return itemIndex >= this.collection.length || itemIndex < 0 ? -1 : Math.floor(itemIndex / this.itemsPerPage);
}
}