-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagination.js
More file actions
121 lines (104 loc) · 3.72 KB
/
pagination.js
File metadata and controls
121 lines (104 loc) · 3.72 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
function changePage(page) {
let start = (page - 1) * itemsPerPage;
let end = start + itemsPerPage;
let displayedGuns = guns.slice(start, end);
// Clear current gun cards
let gunCardsContainer = document.getElementById("gunCardsContainer");
gunCardsContainer.innerHTML = "";
// Populate gun cards
displayedGuns.forEach(gun => {
let card = document.createElement("div");
card.classList.add("col-md-4");
card.innerHTML = `
<div class="card">
<div class="card-body">
<h5 class="card-title">${gun.name}</h5>
<!-- Other gun details here -->
</div>
</div>
`;
gunCardsContainer.appendChild(card);
});
}
function populatePagination() {
let pagination = document.getElementById("pagination");
pagination.innerHTML = "";
for (let i = 1; i <= totalPages; i++) {
let li = document.createElement("li");
li.classList.add("page-item");
let a = document.createElement("a");
a.classList.add("page-link");
a.href = "#";
a.innerText = i;
// Add event listener to change page
a.addEventListener("click", function(event) {
event.preventDefault();
changePage(i);
});
li.appendChild(a);
pagination.appendChild(li);
}
}
// Highlight the current page
function highlightCurrentPage(page) {
let paginationLinks = document.querySelectorAll(".page-link");
paginationLinks.forEach(link => {
link.classList.remove("active");
if (parseInt(link.innerText) === page) {
link.classList.add("active");
}
});
}
async function fetchFilteredGuns() {
const { size, type, availability, ammo, colors } = getSelectedValues();
try {
const response = await fetch(`/api/guns?size=${size}&availability=${availability}&type=${type}&ammo=${ammo}&colors=${colors}`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
guns = data; // Update guns array with fetched data
totalPages = Math.ceil(guns.length / itemsPerPage); // Recalculate total pages
populatePagination(); // Populate pagination with updated total pages
changePage(1); // Display first page of gun cards
} catch (error) {
console.error('There was a problem fetching the filtered guns data:', error);
}
}
async function fetchFilteredGuns() {
const { size, type, availability, ammo, colors } = getSelectedValues();
try {
const response = await fetch(`/api/guns?size=${size}&availability=${availability}&type=${type}&ammo=${ammo}&colors=${colors}`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
guns = data; // Update guns array with fetched data
totalPages = Math.ceil(guns.length / itemsPerPage); // Recalculate total pages
populatePagination(); // Populate pagination with updated total pages
changePage(1); // Display first page of gun cards
} catch (error) {
console.error('There was a problem fetching the filtered guns data:', error);
}
}
// Event listener for dropdown changes
document.getElementById("sizeDropdown").addEventListener("change", function() {
fetchFilteredGuns();
highlightCurrentPage(1);
});
document.getElementById("typeDropdown").addEventListener("change", function() {
fetchFilteredGuns();
highlightCurrentPage(1);
});
document.getElementById("availabilityDropdown").addEventListener("change", function() {
fetchFilteredGuns();
highlightCurrentPage(1);
});
document.getElementById("ammoDropdown").addEventListener("change", function() {
fetchFilteredGuns();
highlightCurrentPage(1);
});
document.getElementById("colorDropdown").addEventListener("change", function() {
fetchFilteredGuns();
highlightCurrentPage(1);
});