Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  100] [ 1]  / answers: 1 / hits: 34659  / 6 Years ago, mon, march 12, 2018, 12:00:00

I want to customize my pagination view for DataTalble for one of my client requirement. I want pagination view same shown in the image.



Custom



For this purpose i have modified some code of datables.js.



DataTable.js:



$.extend(extPagination, {
simple: function (page, pages) {
return ['previous', 'next'];
},

full: function (page, pages) {
return ['first', 'previous', 'next', 'last'];
},

numbers: function (page, pages) {
return [_numbers(page, pages)];
},

simple_numbers: function (page, pages) {
return ['previous', _numbers(page, pages), 'next'];
},

full_numbers: function (page, pages) {
return ['first', 'previous', _numbers(page, pages), 'next', 'last'];
},

first_last_numbers: function (page, pages) {
return ['first', _numbers(page, pages), 'last'];
},
custom: function (page, pages) {
return ['previous', _numbers(page, pages), 'next'];
},

// For testing and plug-ins to use
_numbers: _numbers,

// Number of number buttons (including ellipsis) to show. _Must be odd!_
numbers_length: 7
});


In My datatable view i have added sPaginationType as custom.



$('#tblSessionList').DataTable({
bPaginate: true,
sPaginationType: custom,
bLengthChange: false,
columnDefs: [
{ targets: 0, orderable: true },
{ targets: 1, orderable: false },
{ targets: 2, orderable: false }

],
bFilter: true,
bInfo: false,
bAutoWidth: false,
searching: false,
scrollX: true
});


By Creating new function i am not able to hide page numbers. Now i am not able to find out how to modify view. Please Help


More From » jquery

 Answers
13

After did some research on datatable.js. I found a solution.



HTML:



$('#tblSessionList').DataTable({
language: {
paginate: {
next: '<span class=glyphicon glyphicon-menu-right></span>',
previous: '<span class=glyphicon glyphicon-menu-left></span>'
}
},
bPaginate: true,
sPaginationType: custom,
bLengthChange: false,
columnDefs: [
{ targets: 0, orderable: true },
{ targets: 1, orderable: false },
{ targets: 2, orderable: false }

],
bFilter: true,
bInfo: false,
bAutoWidth: false,
searching: false,
scrollX: true
});


Here is all you need to change in datatable.js:



$.extend(true, DataTable.ext.renderer, {
pageButton: {
_: function(settings, host, idx, buttons, page, pages) {
var classes = settings.oClasses;
var lang = settings.oLanguage.oPaginate;
var aria = settings.oLanguage.oAria.paginate || {};
var btnDisplay,
btnClass,
counter = 0;

var attach = function(container, buttons) {
var i, ien, node, button;
var clickHandler = function(e) {
_fnPageChange(settings, e.data.action, true);
};

for (i = 0, ien = buttons.length; i < ien; i++) {
button = buttons[i];

if ($.isArray(button)) {
var inner;
if (settings.sPaginationType == custom) {

inner = $(
<span class='custom-pagination'> Page + parseInt(page + 1) + of + pages + </span>
).appendTo(container);
} else {
inner = $(< + (button.DT_el || div) + />).appendTo(
container
);
attach(inner, button);
}
} else {
btnDisplay = null;
btnClass = ;

switch (button) {
case ellipsis:
container.append('<span class=ellipsis>&#x2026;</span>');
break;

case first:
btnDisplay = lang.sFirst;
btnClass =
button +
(page > 0 ? : + classes.sPageButtonDisabled);
break;

case previous:
btnDisplay = lang.sPrevious;
btnClass =
button +
(page > 0 ? : + classes.sPageButtonDisabled);
break;

case next:
btnDisplay = lang.sNext;
btnClass =
button +
(page < pages - 1 ? : + classes.sPageButtonDisabled);
console.log(btnClass)
break;

case last:
btnDisplay = lang.sLast;
btnClass =
button +
(page < pages - 1 ? : + classes.sPageButtonDisabled);
break;

default:
// To Button
btnDisplay = button + 1;
btnClass = page === button ? classes.sPageButtonActive : ;
console.log(btnDisplay);
break;
}

if (btnDisplay !== null) {
//
node = $(<a>, {
class: classes.sPageButton + + btnClass,
aria-controls: settings.sTableId,
aria-label: aria[button],
data-dt-idx: counter,
tabindex: settings.iTabIndex,
id:
idx === 0 && typeof button === string
? settings.sTableId + _ + button
: null
})
.html(btnDisplay)
.appendTo(container);
// }

_fnBindAction(node, { action: button }, clickHandler);

counter++;
}
}
}
};

// IE9 throws an 'unknown error' if document.activeElement is used
// inside an iframe or frame. Try / catch the error. Not good for
// accessibility, but neither are frames.
var activeEl;

try {
// Because this approach is destroying and recreating the paging
// elements, focus is lost on the select button which is bad for
// accessibility. So we want to restore focus once the draw has
// completed
activeEl = $(host)
.find(document.activeElement)
.data(dt-idx);
} catch (e) {}

attach($(host).empty(), buttons);

if (activeEl !== undefined) {
$(host)
.find([data-dt-idx= + activeEl + ])
.focus();
}
}
}
});

[#54969] Wednesday, March 7, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristinsonjab

Total Points: 364
Total Questions: 98
Total Answers: 98

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
kristinsonjab questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Fri, Jan 22, 21, 00:00, 3 Years ago
Fri, Aug 14, 20, 00:00, 4 Years ago
;