/* * Copyright 2016-2017 ZTE, Inc. and others. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ $.fn.dataTableExt.oApi.fnPagingInfo = function (oSettings) { return { "iEnd": oSettings.fnDisplayEnd(), "iLength": oSettings._iDisplayLength, "iTotal": oSettings.fnRecordsTotal(), "iFilteredTotal": oSettings.fnRecordsDisplay(), "iPage": Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength), "iTotalPages": Math.ceil(oSettings.fnRecordsDisplay() / oSettings._iDisplayLength) }; }; $.extend($.fn.dataTableExt.oPagination, { "bootstrap_extended": { "fnInit": function (oSettings, nPaging, fnDraw) { var oLang = oSettings.oLanguage.oPaginate; var oPaging = oSettings.oInstance.fnPagingInfo(); var fnClickHandler = function (e) { e.preventDefault(); if (oSettings.oApi._fnPageChange(oSettings, e.data.action)) { fnDraw(oSettings); } }; $(nPaging).append( '
' + oLang.sPage + ' ' + '' + '' + ' ' + oLang.sPageOf + ' ' + '
' ); var els = $('a', nPaging); $(els[0]).bind('click.DT', {action: "previous"}, fnClickHandler); $(els[1]).bind('click.DT', {action: "next"}, fnClickHandler); $('.pagination-panel-input', nPaging).bind('change.DT', function (e) { var oPaging = oSettings.oInstance.fnPagingInfo(); e.preventDefault(); var page = parseInt($(this).val()); if (page > 0 && page < oPaging.iTotalPages) { if (oSettings.oApi._fnPageChange(oSettings, page - 1)) { fnDraw(oSettings); } } else { $(this).val(oPaging.iPage + 1); } }); $('.pagination-panel-input', nPaging).bind('keypress.DT', function (e) { var oPaging = oSettings.oInstance.fnPagingInfo(); if (e.which == 13) { var page = parseInt($(this).val()); if (page > 0 && page < oSettings.oInstance.fnPagingInfo().iTotalPages) { if (oSettings.oApi._fnPageChange(oSettings, page - 1)) { fnDraw(oSettings); } } else { $(this).val(oPaging.iPage + 1); } e.preventDefault(); } }); }, "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++) { var wrapper = $(an[i]).parents(".dataTables_wrapper"); if (oPaging.iTotalPages <= 0) { $('.pagination-panel, .dataTables_length', wrapper).hide(); } else { $('.pagination-panel, .dataTables_length', wrapper).show(); } $('.pagination-panel-total', an[i]).html(oPaging.iTotalPages); $('.pagination-panel-input', an[i]).val(oPaging.iPage + 1); // Remove the middle elements $('li:gt(1)', an[i]).filter(':not(.next)').remove(); // Add the new list items and their event handlers for (j = iStart; j <= iEnd; j++) { sClass = (j == oPaging.iPage + 1) ? 'class="active"' : ''; $('
  • ' + j + '
  • ') .insertBefore($('li.next:first', 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) { $('a.prev', an[i]).addClass('disabled'); } else { $('a.prev', an[i]).removeClass('disabled'); } if (oPaging.iPage === oPaging.iTotalPages - 1 || oPaging.iTotalPages === 0) { $('a.next', an[i]).addClass('disabled'); } else { $('a.next', an[i]).removeClass('disabled'); } } } } });