I am following Allan's this plugins for integrating DataTables with bootstrap. Have make a good progress so far and only thing I am missing is first and last buttons in the pagination (currently I have Next/Previous buttons, but I need Fist/Previous/[rest of pages]/Next/Last buttons). At the very bottom of this thread Allan has mentioned that
I recently committed a change for DataTables 1.10 which will make this much much easier in future
I can get it to working using the answer by @kiarash in this post. Also noticed this Github issue. Does anyone know what is the correct approach to get First/last buttons with bootstrap paginantion?
Oh, wow. All of these examples are buggy and full of syntax errors.
Took me a while to get this running (too many changes to describe them individually):
/* Bootstrap style pagination control */
$.extend($.fn.dataTableExt.oPagination, {
"bootstrap": {
"fnInit": function(oSettings, nPaging, fnDraw) {
var oLang = oSettings.oLanguage.oPaginate;
var fnClickHandler = function(e) {
e.preventDefault();
if (oSettings.oApi._fnPageChange(oSettings, e.data.action)) {
fnDraw(oSettings);
}
};
$(nPaging).append(
'<ul class="pagination">' +
'<li class="first disabled"><a href="#">' + oLang.sFirst + '</a></li>' +
'<li class="prev disabled"><a href="#">' + oLang.sPrevious + '</a></li>' +
'<li class="next disabled"><a href="#">' + oLang.sNext + '</a></li>' +
'<li class="last disabled"><a href="#">' + oLang.sLast + '</a></li>' +
'</ul>'
);
var els = $('a', nPaging);
$(els[0]).bind('click.DT', {
action: "first"
}, fnClickHandler);
$(els[1]).bind('click.DT', {
action: "previous"
}, fnClickHandler);
$(els[2]).bind('click.DT', {
action: "next"
}, fnClickHandler);
$(els[3]).bind('click.DT', {
action: "last"
}, fnClickHandler);
},
"fnUpdate": function(oSettings, fnDraw) {
var iListLength = 5;
var oPaging = oSettings.oInstance.fnPagingInfo();
var an = oSettings.aanFeatures.p;
var i, j, sClass, iStart, iEnd, iHalf = Math.floor(iListLength / 2);
if (oPaging.iTotalPages < iListLength) {
iStart = 1;
iEnd = oPaging.iTotalPages;
} else if (oPaging.iPage <= iHalf) {
iStart = 1;
iEnd = iListLength;
} else if (oPaging.iPage >= (oPaging.iTotalPages - iHalf)) {
iStart = oPaging.iTotalPages - iListLength + 1;
iEnd = oPaging.iTotalPages;
} else {
iStart = oPaging.iPage - iHalf + 1;
iEnd = iStart + iListLength - 1;
}
for (i = 0, iLen = an.length; i < iLen; i++) {
// Remove the middle elements
$('li:gt(1)', an[i]).filter(':not(.next,.last)').remove();
// Add the new list items and their event handlers
for (j = iStart; j <= iEnd; j++) {
var act = "";
if (j == oPaging.iPage + 1) {
act = 'class="active"';
}
$('<li ' + act + '<a href="/ref#sClass"></a>' + '<a href="#">' + j + '</a></li>')
.insertBefore($('.next,.last', an[i])[0])
.bind('click', function(e) {
e.preventDefault();
oSettings._iDisplayStart = (parseInt($('a', this).text(), 10) - 1) * oPaging.iLength;
fnDraw(oSettings);
});
}
// Add / remove disabled classes from the static elements
if (oPaging.iPage === 0) {
$('li:first', an[i]).addClass('disabled');
$('li.prev').addClass('disabled');
} else {
$('li:first', an[i]).removeClass('disabled');
$('li.prev').removeClass('disabled');
}
if (oPaging.iPage === oPaging.iTotalPages - 1 || oPaging.iTotalPages === 0) {
$('li:last', an[i]).addClass('disabled');
$('li.next').addClass('disabled');
} else {
$('li:last', an[i]).removeClass('disabled');
$('li.next').removeClass('disabled');
}
}
}
}
});
Find a working plunker here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With