-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaginationHelper.js
More file actions
39 lines (39 loc) · 1.5 KB
/
paginationHelper.js
File metadata and controls
39 lines (39 loc) · 1.5 KB
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
39
class PaginationHelper {
constructor(collection, itemsPerPage) {
this.collection = collection;
this.itemsPerPage = itemsPerPage;
}
itemCount() {
return this.collection.length;
}
pageCount() {
if (this.collection.length <= 0) return 0;
return (String(this.collection.length / this.itemsPerPage).includes(".") ? Math.floor(this.collection.length / this.itemsPerPage) + 1 : (this.collection.length / this.itemsPerPage));
}
pageItemCount(pageIndex) {
if (this.collection.length <= 0 || pageIndex <= -1) return -1;
const newArr = this.collection.slice(pageIndex * this.itemsPerPage, (pageIndex + 1) * this.itemsPerPage);
return newArr.length <= 0 ? -1 : newArr.length;
}
pageIndex(itemIndex) {
if (this.collection.length <= 0 || itemIndex > this.collection.length || itemIndex <= -1) return -1;
if (this.itemsPerPage > this.collection.length) return 0;
let newArr = [];
for (let i = 0; i <= this.pageCount(); i++) {
newArr.push(
this.collection.slice(
i * this.itemsPerPage,
(i + 1) * this.itemsPerPage
)
);
}
const itemAtIndex = this.collection[itemIndex];
let arrIndex;
for (let i = 0; i < newArr.length; i++) {
if (newArr[i].indexOf(itemAtIndex) !== -1) {
arrIndex = i;
};
}
return arrIndex === undefined ? -1 : arrIndex;
}
}