");
},
});
}
function updatePagination(pagination) {
if (!pagination) {
$("#paged").hide();
return;
}
const currentPage = pagination.current_page;
const lastPage = pagination.last_page;
const maxVisiblePages = 5; // Number of visible pages before showing "..."
$("#paged").empty();
// Add "Previous" button
if (currentPage > 1) {
$("#paged").append(createPageItem("«", currentPage - 1, false));
}
let startPage = Math.max(1, currentPage - Math.floor(maxVisiblePages / 2));
let endPage = Math.min(lastPage, currentPage + Math.floor(maxVisiblePages / 2));
// Ensure first page is always shown, but not duplicated
if (startPage > 1) {
if (startPage > 2) {
$("#paged").append(createPageItem(1, 1, currentPage === 1));
$("#paged").append('
...
'); // Show "..." if there's a gap
} else {
startPage = 1; // Fix to make sure page 1 is included
}
}
// Add middle pages (now includes `1` properly)
for (let page = startPage; page <= endPage; page++) {
$("#paged").append(createPageItem(page, page, currentPage === page));
}
// Ensure last page is always shown, but not duplicated
if (endPage < lastPage) {
if (endPage < lastPage - 1) {
$("#paged").append('
...
'); // Show "..." if there's a gap
}
$("#paged").append(createPageItem(lastPage, lastPage, currentPage === lastPage));
}
// Add "Next" button
if (currentPage < lastPage) {
$("#paged").append(createPageItem("»", currentPage + 1, false));
}
$("#paged").show();
function createPageItem(text, page, isActive) {
const pageItem = $("