-
Notifications
You must be signed in to change notification settings - Fork 425
/
pagination.js
81 lines (68 loc) · 2.81 KB
/
pagination.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function getPageList(totalPages, page, maxLength){
function range(start, end){
return Array.from(Array(end-start+1), (_, i)=> i+start);
}
var sideWidth = maxLength < 9 ? 1: 2;
var leftWidth = (maxLength - sideWidth*2 -3)>>1;
var rightWidth = (maxLength - sideWidth*2 -3)>>1;
if (totalPages <= maxLength){
return range(1, totalPages);
}
if (page<= maxLength-sideWidth-1-rightWidth){
return range(1, maxLength-sideWidth-1).concat(0, range(totalPages-sideWidth+1, totalPages));
}
if (page >= totalPages - sideWidth-1-rightWidth){
return range(1, sideWidth).concat(0, range(totalPages-sideWidth-1-rightWidth-leftWidth, totalPages));
}
return range(1, sideWidth).concat(0, range(page-leftWidth, page+rightWidth), 0, range(totalPages-sideWidth+1, totalPages));
}
var itemsArr = [];
var items = document.querySelectorAll(".container .card");
items.forEach(item => itemsArr.push(item));
var numberOfItems = items.length;
var limitPerPage = 10;
var totalPages = Math.ceil(numberOfItems/limitPerPage);
var paginationSize = 7;
var currentPage;
function showPage(whichPage){
console.log("hey")
console.log(whichPage)
if (whichPage<1 || whichPage>totalPages)return false;
currentPage= whichPage;
console.log(currentPage);
items.forEach(item => {
item.classList.add("hidden");
})
// console.log(items);
itemsArr.slice((currentPage-1)*limitPerPage, currentPage*limitPerPage).forEach(item => {
item.classList.remove("hidden");
});
document.querySelectorAll(".pagination li").forEach(item => {
if (!item.classList.contains("previous-page") && !item.classList.contains("next-page"))item.remove();
})
getPageList(totalPages, currentPage, paginationSize).forEach(item => {
var newListItem = document.createElement("li");
var newAnchor = document.createElement("a");
newListItem.classList.add("page-item");
newListItem.classList.add(item ? "current-page":"dots");
if (item===currentPage)newListItem.classList.toggle("active")
newAnchor.classList.add("page-link")
newAnchor.setAttribute("href", "#");
newAnchor.innerHTML = (item || "...");
newListItem.append(newAnchor);
document.querySelector(".pagination").insertBefore(newListItem, document.querySelector(".pagination .next-page"));
})
$(".previous-page").toggleClass("disable", currentPage===1);
$(".next-page").toggleClass("disable", currentPage===totalPages);
return true;
}
showPage(1);
$(document).on("click", ".pagination li.current-page:not(.active)", function(){
return showPage(+$(this).text());
})
$(".next-page").on("click", function(){
return showPage(currentPage+1);
})
$(".previous-page").on("click", function(){
return showPage(currentPage-1);
})