Skip to content

Commit

Permalink
Fix(#10210): Hide pager when there is only one page (#10221)
Browse files Browse the repository at this point in the history
* Fix: Hide pager in legacy datatables when only one page
---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
SharkyBytes and pre-commit-ci[bot] authored Dec 30, 2024
1 parent b73fe2b commit ab74711
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions openlibrary/plugins/openlibrary/js/editions-table/index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const LS_RESULTS_LENGTH_KEY = 'editions-table.resultsLength';

export function initEditionsTable() {
var rowCount;
let currentLength;

$('#editions th.title').on('mouseover', function(){
if ($(this).hasClass('sorting_asc')) {
Expand Down Expand Up @@ -43,6 +42,7 @@ export function initEditionsTable() {
});

$('#editions').on('length.dt', function(e, settings, length) {
togglePaginationVisibility(length);
localStorage.setItem(LS_RESULTS_LENGTH_KEY, length);
});

Expand All @@ -53,29 +53,31 @@ export function initEditionsTable() {
})

rowCount = $('#editions tbody tr').length;
if (rowCount < 4) {
$('#editions').DataTable({
aoColumns: [{sType: 'html'},null],
order: [ [1,'asc'] ],
bPaginate: false,
bInfo: false,
bFilter: false,
bStateSave: false,
bAutoWidth: false
});
} else {
currentLength = Number(localStorage.getItem(LS_RESULTS_LENGTH_KEY));
$('#editions').DataTable({
aoColumns: [{sType: 'html'},null],
order: [ [1,'asc'] ],
lengthMenu: [ [3, 10, 25, 50, 100, -1], [3, 10, 25, 50, 100, 'All'] ],
bPaginate: true,
bInfo: true,
sPaginationType: 'full_numbers',
bFilter: true,
bStateSave: false,
bAutoWidth: false,
pageLength: currentLength ? currentLength : DEFAULT_LENGTH
});
const currentLength = Number(localStorage.getItem(LS_RESULTS_LENGTH_KEY)) || DEFAULT_LENGTH;

$('#editions').DataTable({
aoColumns: [{ sType: 'html' }, null],
order: [[1, 'asc']],
lengthMenu: [[3, 10, 25, 50, 100, -1], [3, 10, 25, 50, 100, 'All']],
bPaginate: true, // Always allow pagination initially
bInfo: true,
sPaginationType: 'full_numbers',
bFilter: true,
bStateSave: false,
bAutoWidth: false,
pageLength: currentLength,
});

// Toggle pagination visibility based on row count and selected length
function togglePaginationVisibility(selectedLength) {
const paginationElement = $('.dataTables_paginate.paging_full_numbers');
if (rowCount <= selectedLength || selectedLength === -1) {
paginationElement.hide();
} else {
paginationElement.show();
}
}

// Initial pagination visibility toggle
togglePaginationVisibility(currentLength);
}

0 comments on commit ab74711

Please sign in to comment.