DataTables.Net Bootstrap 3 Pagination Missing First Last按钮

时间:2014-03-04 06:37:21

标签: twitter-bootstrap-3 datatables jquery-datatables

我正在关注Allan的this plugins,以便将DataTables与bootstrap集成。到目前为止取得了很好的进展,我唯一缺少的是分页中的第一个和最后一个按钮(目前我有下一个/上一个按钮,但我需要Fist / Previous / [其余页面] / Next / Last按钮)。在this thread的最底层,艾伦提到了

  

我最近对DataTables 1.10进行了更改,这将使以后更加轻松

我可以使用this post中@kiarash的答案来使用它。还注意到this Github问题。有谁知道使用bootstrap paginantion获取First / last按钮的正确方法是什么?

1 个答案:

答案 0 :(得分:9)

哦,哇。所有这些示例都是错误的,并且充满了语法错误。

花了一些时间让这个运行(太多的变化来单独描述它们):

/* 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');
        }
      }
    }
  }
});

找一个工作的plunker here