Skip to content
Snippets Groups Projects
Commit fb18d766 authored by Angela Livia Arumsari's avatar Angela Livia Arumsari
Browse files

feat: add truncate for pagination

parent d7925266
Branches
No related merge requests found
...@@ -33,27 +33,67 @@ async function searchFilterHandler() { ...@@ -33,27 +33,67 @@ async function searchFilterHandler() {
} }
function generatePaginationLinks() { function generatePaginationLinks() {
console.log(totalPageCount);
const paginationContainer = document.getElementById("pagination-container"); const paginationContainer = document.getElementById("pagination-container");
paginationContainer.innerHTML = ""; paginationContainer.innerHTML = "";
console.log(totalPageCount);
for (let i = 1; i <= totalPageCount; i++) { const maxVisiblePages = 8; // Set the maximum number of visible pages
const pageLink = document.createElement("a"); const halfVisiblePages = Math.floor(maxVisiblePages / 2);
pageLink.setAttribute("class", "page-number");
pageLink.textContent = i; let startPage = Math.max(currentPage - halfVisiblePages, 1);
pageLink.href = `?page=${i}`; let endPage = Math.min(startPage + maxVisiblePages - 1, totalPageCount);
pageLink.classList.add("page-number");
if (i === currentPage) { if (endPage - startPage + 1 < maxVisiblePages) {
pageLink.setAttribute("class", "page-number active"); startPage = Math.max(endPage - maxVisiblePages + 1, 1);
} }
pageLink.addEventListener("click", (e) => {
e.preventDefault(); if (startPage > 1) {
currentPage = i; const firstPageLink = createPageLink(1, "First");
searchFilterHandler(); paginationContainer.appendChild(firstPageLink);
});
const ellipsis = createEllipsis();
paginationContainer.appendChild(ellipsis);
}
for (let i = startPage; i <= endPage; i++) {
const pageLink = createPageLink(i);
paginationContainer.appendChild(pageLink); paginationContainer.appendChild(pageLink);
} }
if (endPage < totalPageCount) {
const ellipsis = createEllipsis();
paginationContainer.appendChild(ellipsis);
const lastPageLink = createPageLink(totalPageCount, "Last");
paginationContainer.appendChild(lastPageLink);
}
}
function createPageLink(pageNumber, text = "") {
const pageLink = document.createElement("a");
pageLink.setAttribute("class", "page-number");
pageLink.textContent = text || pageNumber;
pageLink.href = `?page=${pageNumber}`;
pageLink.classList.add("page-number");
if (pageNumber === currentPage) {
pageLink.setAttribute("class", "page-number active");
}
pageLink.addEventListener("click", (e) => {
e.preventDefault();
currentPage = pageNumber;
searchFilterHandler();
});
return pageLink;
}
function createEllipsis() {
const ellipsis = document.createElement("span");
ellipsis.textContent = "...";
ellipsis.classList.add("ellipsis");
return ellipsis;
} }
function updateFilmCards(films) { function updateFilmCards(films) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment