Update input.js

Redid the logic for disabling the buttons, again, since it would display incorrectly when there were only 2 pages total.
Also added some logic to hide the paging controls when you only have 0 or 1 pages, since you can't page anyway.
pull/104/head
Anderezekial 10 years ago
parent fa5734b290
commit 2dd7559d41

@ -33,8 +33,8 @@ $.fn.dataTableExt.oPagination.input = {
nNext.innerHTML = oSettings.oLanguage.oPaginate.sNext; nNext.innerHTML = oSettings.oLanguage.oPaginate.sNext;
nLast.innerHTML = oSettings.oLanguage.oPaginate.sLast; nLast.innerHTML = oSettings.oLanguage.oPaginate.sLast;
nFirst.className = "paginate_button first"; nFirst.className = "paginate_button first disabled";
nPrevious.className = "paginate_button previous"; nPrevious.className = "paginate_button previous disabled";
nNext.className="paginate_button next"; nNext.className="paginate_button next";
nLast.className = "paginate_button last"; nLast.className = "paginate_button last";
nOf.className = "paginate_of"; nOf.className = "paginate_of";
@ -61,54 +61,66 @@ $.fn.dataTableExt.oPagination.input = {
nPaging.appendChild( nNext ); nPaging.appendChild( nNext );
nPaging.appendChild( nLast ); nPaging.appendChild( nLast );
$(nFirst).click( function () { $(nFirst).click( function ()
{
var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
if (iCurrentPage != 1)
{
oSettings.oApi._fnPageChange( oSettings, "first" ); oSettings.oApi._fnPageChange( oSettings, "first" );
fnCallbackDraw( oSettings ); fnCallbackDraw( oSettings );
$(nFirst).addClass('disabled'); $(nFirst).addClass('disabled');
$(nPrevious).addClass('disabled'); $(nPrevious).addClass('disabled');
$(nNext).removeClass('disabled'); $(nNext).removeClass('disabled');
$(nLast).removeClass('disabled'); $(nLast).removeClass('disabled');
}
} ); } );
$(nPrevious).click( function() { $(nPrevious).click( function()
oSettings.oApi._fnPageChange( oSettings, "previous" ); {
fnCallbackDraw( oSettings );
var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1; var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
if (iCurrentPage == 1) if (iCurrentPage != 1)
{
oSettings.oApi._fnPageChange(oSettings, "previous");
fnCallbackDraw(oSettings);
if (iCurrentPage == 2)
{ {
$(nFirst).addClass('disabled'); $(nFirst).addClass('disabled');
$(nPrevious).addClass('disabled'); $(nPrevious).addClass('disabled');
} }
else
{
$(nNext).removeClass('disabled'); $(nNext).removeClass('disabled');
$(nLast).removeClass('disabled'); $(nLast).removeClass('disabled');
} }
} ); } );
$(nNext).click( function() { $(nNext).click( function()
oSettings.oApi._fnPageChange( oSettings, "next" ); {
fnCallbackDraw( oSettings );
var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1; var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
if (iCurrentPage == (Math.ceil((oSettings.fnRecordsDisplay() - 1) / oSettings._iDisplayLength))) if (iCurrentPage != (Math.ceil((oSettings.fnRecordsDisplay() - 1) / oSettings._iDisplayLength)))
{
oSettings.oApi._fnPageChange(oSettings, "next");
fnCallbackDraw(oSettings);
if (iCurrentPage == (Math.ceil((oSettings.fnRecordsDisplay() - 1) / oSettings._iDisplayLength) - 1))
{ {
$(nNext).addClass('disabled'); $(nNext).addClass('disabled');
$(nLast).addClass('disabled'); $(nLast).addClass('disabled');
} }
else
{
$(nFirst).removeClass('disabled'); $(nFirst).removeClass('disabled');
$(nPrevious).removeClass('disabled'); $(nPrevious).removeClass('disabled');
} }
} ); } );
$(nLast).click( function() { $(nLast).click( function()
oSettings.oApi._fnPageChange( oSettings, "last" ); {
fnCallbackDraw( oSettings ); var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
if (iCurrentPage != (Math.ceil((oSettings.fnRecordsDisplay() - 1) / oSettings._iDisplayLength)))
{
oSettings.oApi._fnPageChange(oSettings, "last");
fnCallbackDraw(oSettings);
$(nFirst).removeClass('disabled'); $(nFirst).removeClass('disabled');
$(nPrevious).removeClass('disabled'); $(nPrevious).removeClass('disabled');
$(nNext).addClass('disabled'); $(nNext).addClass('disabled');
$(nLast).addClass('disabled'); $(nLast).addClass('disabled');
}
} ); } );
$(nInput).keyup( function (e) { $(nInput).keyup( function (e) {
@ -126,29 +138,33 @@ $.fn.dataTableExt.oPagination.input = {
if ( this.value === "" || this.value.match(/[^0-9]/) ) if ( this.value === "" || this.value.match(/[^0-9]/) )
{ {
/* Nothing entered or non-numeric character */ /* Nothing entered or non-numeric character */
this.value = this.value.replace(/[^\d]/g, ''); // don't even allow anything but digits
return; return;
} }
var iNewStart = oSettings._iDisplayLength * (this.value - 1); var iNewStart = oSettings._iDisplayLength * (this.value - 1);
var iEndPosition = (Math.ceil((oSettings.fnRecordsDisplay() - 1) / oSettings._iDisplayLength) - 1) * oSettings._iDisplayLength;
if (iNewStart < 0) if (iNewStart < 0)
{ {
iNewStart = 0; iNewStart = 0;
} }
if (iNewStart > oSettings.fnRecordsDisplay()) if (iNewStart > oSettings.fnRecordsDisplay())
{ {
iNewStart = iEndPosition; iNewStart = (Math.ceil((oSettings.fnRecordsDisplay() - 1) / oSettings._iDisplayLength) - 1) * oSettings._iDisplayLength;
} }
if (iNewStart == 0) if (iNewStart == 0)
{ {
$(nFirst).addClass('disabled'); $(nFirst).addClass('disabled');
$(nPrevious).addClass('disabled'); $(nPrevious).addClass('disabled');
$(nNext).removeClass('disabled');
$(nLast).removeClass('disabled');
} }
else if (iNewStart == iEndPosition) else if (iNewStart == ((Math.ceil((oSettings.fnRecordsDisplay() - 1) / oSettings._iDisplayLength) - 1) * oSettings._iDisplayLength))
{ {
$(nNext).addClass('disabled'); $(nNext).addClass('disabled');
$(nLast).addClass('disabled'); $(nLast).addClass('disabled');
$(nFirst).removeClass('disabled');
$(nPrevious).removeClass('disabled');
} }
else else
{ {
@ -165,6 +181,13 @@ $.fn.dataTableExt.oPagination.input = {
/* Take the brutal approach to cancelling text selection */ /* Take the brutal approach to cancelling text selection */
$('span', nPaging).bind( 'mousedown', function () { return false; } ); $('span', nPaging).bind( 'mousedown', function () { return false; } );
$('span', nPaging).bind( 'selectstart', function () { return false; } ); $('span', nPaging).bind( 'selectstart', function () { return false; } );
// If we can't page anyway, might as well not show it
var iPages = Math.ceil((oSettings.fnRecordsDisplay()) / oSettings._iDisplayLength);
if(iPages <= 1)
{
$(nPaging).hide();
}
}, },
@ -177,14 +200,21 @@ $.fn.dataTableExt.oPagination.input = {
var iPages = Math.ceil((oSettings.fnRecordsDisplay()) / oSettings._iDisplayLength); var iPages = Math.ceil((oSettings.fnRecordsDisplay()) / oSettings._iDisplayLength);
var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1; var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
/* Loop over each instance of the pager */
var an = oSettings.aanFeatures.p; var an = oSettings.aanFeatures.p;
for ( var i=0, iLen=an.length ; i<iLen ; i++ ) if (iPages <= 1) // hide paging when we can't page
{
$(an).hide();
}
else
{
/* Loop over each instance of the pager */
for (var i = 0, iLen = an.length ; i < iLen ; i++)
{ {
var spans = an[i].getElementsByTagName('span'); var spans = an[i].getElementsByTagName('span');
var inputs = an[i].getElementsByTagName('input'); var inputs = an[i].getElementsByTagName('input');
spans[3].innerHTML = " of "+iPages; spans[3].innerHTML = " of " + iPages;
inputs[0].value = iCurrentPage; inputs[0].value = iCurrentPage;
} }
} }
}
}; };

Loading…
Cancel
Save