Added first/last jumps.  Tried to figure out a way to have the same
amount of page numbers displayed at all times, but that was getting
complicated quickly.

Anyway, here it is. For some reason if I set the step to 1 or 2, I end
up with an empty page on the end.  Hope someone can have a look and
take this further.

// John Resig's Pager - http://jquery.com/api/js/pager.js //
// modified July 2007 - Adam Davis (agent2026) //

$.fn.pager = function(step,spread) {
  var types = {
    UL: "li",
    OL: "li",
    DL: "dt",
    TABLE: "tr"
  };

  return this.each(function(){
    var type = types[this.nodeName];
    var pagedUI = type == "tr" ? $("tbody",this) : $(this);
    var rows = $(type, pagedUI);
    var curPage = 0;
    var names = [], num = [];

    if ( !step || step.constructor != Function ) {
      step = step || 10;

      if (rows.length > step)
        for ( var i = 0; i <= rows.length; i += step ) {
          names.push( names.length + 1 );
          num.push( [ i, step ] );
        }
    } else {
      var last;
      rows.each(function(){
        var l = step( this );
        if ( l != last ) {
          names.push( l );
          var pre = num.length ? num[ num.length - 1 ][0] +
num[ num.length - 1 ][1] : 0;

          num.push( [ pre, 0 ] );
          last = l;
        }

        num[ num.length - 1 ][1]++;
      });
    }

    if ( names.length > 1 ) {
      var pager = $(this).prev("ul.nav-page").empty();

      if ( !pager.length )
        pager = $("<ul class='nav-page'></ul>");

      for ( var i = 0; i < names.length; i++ )
        $("<a href=''></a>").attr({
          rel: i, innerHTML: names[i]
        }).click(function() {
          return handleCrop( this.rel );
        }).wrap("<li></li>").parent().appendTo(pager);

      pager.insertBefore( this );

      var prev = $("<a href=''>&laquo; Prev</a>").click(function(){
        return handleCrop( --curPage );
      }).wrap("<li class='prev, noTrim'></
li>").parent().prependTo(pager);

      var next = $("<a href=''>Next &raquo;</a>").click(function(){
        return handleCrop( ++curPage );
      }).wrap("<li class='next, noTrim'></
li>").parent().appendTo(pager);

// add first/last jumps - agent2026 //

      var fJump = $("<a href=''>&lsaquo; First</a>").click(function(){
        return handleCrop( 0 );
      }).wrap("<li class='first, noTrim'></
li>").parent().prependTo(pager);

      var lJump = $("<a href=''>Last &rsaquo;</a>").click(function(){
        return handleCrop( names.length-1 );
      }).wrap("<li class='last, noTrim'></
li>").parent().appendTo(pager);

// end first/last //

      handleCrop( 0 );
    }

    function handleCrop( page ) {
      curPage = page - 0;
      var s = num[ curPage ][0];
      var e = num[ curPage ][1];

      if ( !curPage ) prev.hide();
      else prev.show();

      if ( curPage == names.length - 1 ) next.hide();
      else next.show();

// show first/last only when needed - agent2026 //

      if ( curPage < 3 ) fJump.hide();
      else fJump.show();

      if ( curPage > names.length - 4 ) lJump.hide();
      else lJump.show();

// end show //

      $("li",pager)
        .removeClass("cur")
        .eq( curPage + 2 ) // increase +1 to +2 to account for new <li
class="first"> - agent2026 //
          .addClass("cur");

      pagedUI.empty().append(
        jQuery.makeArray( rows ).slice( s, s + e )
      );
      pageTrim();
      return false;
    }

// trim pages when page count is high using spread - agent2026 //

    function pageTrim(){
      $("li:not(.noTrim)",pager).show();
      $("li:not(.noTrim)",pager).lt(curPage-spread-1).hide();
      $("li:not(.noTrim)",pager).gt(curPage+spread+1).hide();
    }

  });
};

Reply via email to