Skip to content

Commit

Permalink
6cd883a17dd4689ee47a9073d76bc28066ff641d New: `-feature paging.bounda…
Browse files Browse the repository at this point in the history
…ryNumbers` which can be used to disable the first and last numbers on eitherside of the ellipsis for the paging control. These could be considered irrelevant now that the first / last buttons are enabled by default.

Update: `paging.numbers` renamed to be `-feature paging.buttons` which makes a lot more sense!

Sync to source repo @6cd883a17dd4689ee47a9073d76bc28066ff641d
  • Loading branch information
dtbuild committed Apr 15, 2024
1 parent 6566c44 commit 5d803fd
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 31 deletions.
2 changes: 1 addition & 1 deletion datatables.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
],
"src-repo": "http://github.com/DataTables/DataTablesSrc",
"last-tag": "2.0.3",
"last-sync": "b8076da3b5692bfb8862d259737245a90e8d97a1"
"last-sync": "6cd883a17dd4689ee47a9073d76bc28066ff641d"
}
50 changes: 36 additions & 14 deletions js/dataTables.js
Original file line number Diff line number Diff line change
Expand Up @@ -12718,17 +12718,23 @@

// opts
// - type - button configuration
// - numbers - number of buttons to show - must be odd
// - buttons - number of buttons to show - must be odd
DataTable.feature.register( 'paging', function ( settings, opts ) {
// Don't show the paging input if the table doesn't have paging enabled
if (! settings.oFeatures.bPaginate) {
return null;
}

opts = $.extend({
numbers: DataTable.ext.pager.numbers_length,
type: settings.sPaginationType
}, opts)
buttons: DataTable.ext.pager.numbers_length,
type: settings.sPaginationType,
boundaryNumbers: true
}, opts);

// To be removed in 2.1
if (opts.numbers) {
opts.buttons = opts.numbers;
}

var host = $('<div/>').addClass( settings.oClasses.paging.container + ' paging_' + opts.type );
var draw = function () {
Expand Down Expand Up @@ -12760,7 +12766,7 @@
buttons = plugin()
.map(function (val) {
return val === 'numbers'
? _pagingNumbers(page, pages, opts.numbers)
? _pagingNumbers(page, pages, opts.buttons, opts.boundaryNumbers)
: val;
})
.flat();
Expand Down Expand Up @@ -12902,12 +12908,15 @@
* @param {*} page Current page
* @param {*} pages Total number of pages
* @param {*} buttons Target number of number buttons
* @param {boolean} addFirstLast Indicate if page 1 and end should be included
* @returns Buttons to show
*/
function _pagingNumbers ( page, pages, buttons ) {
function _pagingNumbers ( page, pages, buttons, addFirstLast ) {
var
numbers = [],
half = Math.floor(buttons / 2);
half = Math.floor(buttons / 2),
before = addFirstLast ? 2 : 1,
after = addFirstLast ? 1 : 0;

if ( pages <= buttons ) {
numbers = _range(0, pages);
Expand All @@ -12930,17 +12939,30 @@
}
}
else if ( page <= half ) {
numbers = _range(0, buttons-2);
numbers.push('ellipsis', pages-1);
numbers = _range(0, buttons-before);
numbers.push('ellipsis');

if (addFirstLast) {
numbers.push(pages-1);
}
}
else if ( page >= pages - 1 - half ) {
numbers = _range(pages-(buttons-2), pages);
numbers.unshift(0, 'ellipsis');
numbers = _range(pages-(buttons-before), pages);
numbers.unshift('ellipsis');

if (addFirstLast) {
numbers.unshift(0);
}
}
else {
numbers = _range(page-half+2, page+half-1);
numbers.push('ellipsis', pages-1);
numbers.unshift(0, 'ellipsis');
numbers = _range(page-half+before, page+half-after);
numbers.push('ellipsis');
numbers.unshift('ellipsis');

if (addFirstLast) {
numbers.push(pages-1);
numbers.unshift(0);
}
}

return numbers;
Expand Down
2 changes: 1 addition & 1 deletion js/dataTables.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dataTables.min.mjs

Large diffs are not rendered by default.

50 changes: 36 additions & 14 deletions js/dataTables.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12665,17 +12665,23 @@ DataTable.feature.register( 'search', function ( settings, opts ) {

// opts
// - type - button configuration
// - numbers - number of buttons to show - must be odd
// - buttons - number of buttons to show - must be odd
DataTable.feature.register( 'paging', function ( settings, opts ) {
// Don't show the paging input if the table doesn't have paging enabled
if (! settings.oFeatures.bPaginate) {
return null;
}

opts = $.extend({
numbers: DataTable.ext.pager.numbers_length,
type: settings.sPaginationType
}, opts)
buttons: DataTable.ext.pager.numbers_length,
type: settings.sPaginationType,
boundaryNumbers: true
}, opts);

// To be removed in 2.1
if (opts.numbers) {
opts.buttons = opts.numbers;
}

var host = $('<div/>').addClass( settings.oClasses.paging.container + ' paging_' + opts.type );
var draw = function () {
Expand Down Expand Up @@ -12707,7 +12713,7 @@ function _pagingDraw(settings, host, opts) {
buttons = plugin()
.map(function (val) {
return val === 'numbers'
? _pagingNumbers(page, pages, opts.numbers)
? _pagingNumbers(page, pages, opts.buttons, opts.boundaryNumbers)
: val;
})
.flat();
Expand Down Expand Up @@ -12849,12 +12855,15 @@ function _pagingButtonInfo(settings, button, page, pages) {
* @param {*} page Current page
* @param {*} pages Total number of pages
* @param {*} buttons Target number of number buttons
* @param {boolean} addFirstLast Indicate if page 1 and end should be included
* @returns Buttons to show
*/
function _pagingNumbers ( page, pages, buttons ) {
function _pagingNumbers ( page, pages, buttons, addFirstLast ) {
var
numbers = [],
half = Math.floor(buttons / 2);
half = Math.floor(buttons / 2),
before = addFirstLast ? 2 : 1,
after = addFirstLast ? 1 : 0;

if ( pages <= buttons ) {
numbers = _range(0, pages);
Expand All @@ -12877,17 +12886,30 @@ function _pagingNumbers ( page, pages, buttons ) {
}
}
else if ( page <= half ) {
numbers = _range(0, buttons-2);
numbers.push('ellipsis', pages-1);
numbers = _range(0, buttons-before);
numbers.push('ellipsis');

if (addFirstLast) {
numbers.push(pages-1);
}
}
else if ( page >= pages - 1 - half ) {
numbers = _range(pages-(buttons-2), pages);
numbers.unshift(0, 'ellipsis');
numbers = _range(pages-(buttons-before), pages);
numbers.unshift('ellipsis');

if (addFirstLast) {
numbers.unshift(0);
}
}
else {
numbers = _range(page-half+2, page+half-1);
numbers.push('ellipsis', pages-1);
numbers.unshift(0, 'ellipsis');
numbers = _range(page-half+before, page+half-after);
numbers.push('ellipsis');
numbers.unshift('ellipsis');

if (addFirstLast) {
numbers.push(pages-1);
numbers.unshift(0);
}
}

return numbers;
Expand Down

0 comments on commit 5d803fd

Please sign in to comment.