Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTables.Net Bootstrap 3 Pagination Missing First Last buttons

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?

like image 515
JenonD Avatar asked Jan 12 '23 02:01

JenonD


1 Answers

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

like image 84
mainguy Avatar answered Jan 30 '23 21:01

mainguy