64 lines
2.5 KiB
JavaScript

// Updates the pagination text for a given pageInfoId
function updateTablePaginationInfo(pageInfoId, showing, total) {
$(`${pageInfoId} .pageinfo-showing`).text(showing);
$(`${pageInfoId} .pageinfo-total`).text(total);
}
// Updates the pagination buttons for a given pageControlsId
function updateTablePagination(pageControlsId, currentPage, pageSize, totalItems, nextExists, prevExists) {
$(pageControlsId).attr("data-page", currentPage);
// Remove existing page specific buttons
$(`${pageControlsId} .page-pick`).remove();
// Determine states of 'previous page' 'next page' buttons
$(`${pageControlsId} .page-prev`).toggleClass("disabled", !prevExists).attr("tabindex", prevExists ? "" : "-1");
$(`${pageControlsId} .page-next`).toggleClass("disabled", !nextExists).attr("tabindex", nextExists ? "" : "-1");
// Calculate amount of pages to account for
const pages = Math.max(Math.ceil(totalItems / pageSize), 1);
// alert(pages + " " + totalItems + " " + pageSize + " ")
for (let i = 1; i <= pages; i++) {
let pageItem = $("<li>").addClass("page-item");
let pageLink = $("<button>")
.attr("type", "button")
.attr("data-page", i)
.addClass("page-link page-pick")
.text(i);
// Insert the new page button before the 'next page' button
pageItem.append(pageLink)
$(`${pageControlsId} .pagination .page-next`).parent().before(pageItem);
$(`${pageControlsId} .pagination .page-pick[data-page="${currentPage}"]`).addClass("disabled").attr("tabindex", -1);
}
}
// Bind the table pagination buttons to control the table pagination
async function bindTablePagination(pageControlsId, dataLoadFunc) {
$(`${pageControlsId} .pagination`).on("click", ".page-link", async function() {
let wantedPage;
let currentPage = parseInt($(pageControlsId).attr("data-page"));
if ($(this).hasClass("page-prev"))
wantedPage = currentPage - 1;
else if ($(this).hasClass("page-next"))
wantedPage = currentPage + 1;
else
wantedPage = $(this).attr("data-page");
await dataLoadFunc(getCurrentlyActiveServer().guild_id, wantedPage)
});
}
// Bind the table pagination page resizer control
async function bindTablePaginationResizer(resizerControlId, dataLoadFunc) {
$(resizerControlId).on("change", async function() {
const page = 1;
const pageSize = $(this).val();
await dataLoadFunc(getCurrentlyActiveServer().guild_id, page, pageSize);
});
}