DataTables.Net Bootstrap 3 Pagination Missing First Last Buttons

I follow Allan these plugins for integrating DataTables with downloads. They made good progress, and only what I miss is the first and last buttons paginated (currently I have Next / Previous buttons, but I need Fist / Previous / [other pages] / Next / Last buttons ) At the very bottom of this stream, Allan mentioned that

I recently made a change for DataTables 1.10, which will make it a lot easier in the future.

I can get it working using @kiarash's answer in this post . Github also noted this question. Does anyone know which is the right approach to get first / last buttons with bootstrapping?

+4
source share
1 answer

Oh wow. All of these examples are erroneous and full of syntax errors.

It took a while to start this process (too many changes to describe them separately):

/* 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 plunger here

+9
source

Source: https://habr.com/ru/post/1529941/


All Articles