-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagination.js
More file actions
44 lines (37 loc) · 986 Bytes
/
Copy pathpagination.js
File metadata and controls
44 lines (37 loc) · 986 Bytes
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
40
41
42
43
44
class Pagination {
constructor(items, itemsPerPage) {
this.items = items;
this.itemsPerPage = itemsPerPage;
this.currentPage = 1;
}
getTotalPages() {
return Math.ceil(this.items.length / this.itemsPerPage);
}
getCurrentPage() {
return this.currentPage;
}
goToPage(pageNumber) {
if (pageNumber > 0 && pageNumber <= this.getTotalPages()) {
this.currentPage = pageNumber;
return true;
} else {
return false;
}
}
nextPage() {
if (this.currentPage < this.getTotalPages()) {
this.currentPage++;
}
}
prevPage() {
if (this.currentPage > 1) {
this.currentPage--;
}
}
getPageItems() {
const startIndex = (this.currentPage - 1) * this.itemsPerPage;
const endIndex = startIndex + this.itemsPerPage;
return this.items.slice(startIndex, endIndex);
}
}
// export default Pagination;