Docunext


jQuery Tablesorter Pager Cookie Widget Bug Fix

June 26th, 2009

Wow that's a mouthful!

The cookie widget for the awesome jQuery tablesorter pager extension wouldn't go back to page 1 for me. I debugged it endlessly until I finally figured it out - the zero page confused it. I hacked a fix that works for me and sent it to the author:

Nice plugin. When I use it w/ pager, move to first page breaks due to wacky numbering. This fix works for me:
if ( pageNum >= 1 ) {
    tablesorterCookieJar.set($(table).attr('id')+'-page', pageNum);
} else if ( pageNum == 0 ) {
} else {
   var pageNum = tablesorterCookieJar.get($(table).attr('id')+'-page');
   if (pageNum && pageNum > 1) {
       table.config.page = pageNum;
        jQuery(table).trigger('sorton',[0,0]);
   }
}

Woops, actually that doesn't work, but I think this does:

var pageNum = table.config.page;
var totalRows = table.config.totalRows;
if ( totalRows > 0 ) {
    tablesorterCookieJar.set($(table).attr('id')+'-page', pageNum);
} else {
   table.config.totalRows = 1;
   var pageNum = tablesorterCookieJar.get($(table).attr('id')+'-page');
   if (pageNum && pageNum > 1) {
       table.config.page = pageNum;
       jQuery(table).trigger('sorton',[0,0]);
   }
}

I'm using totalRows as an initialization marker of sorts.

HA! Actually that change does nothing. Turns out I was just avoiding calling the update from the cookie - which is what was preventing the page from getting reset to 0. Instead, I've removed that part:

            var pageNum = table.config.page;
            if ( pageNum > 0 ) {
                tablesorterCookieJar.set($(table).attr('id')+'-page', pageNum);
            }

and instead using this to initialize the tablesorter:

        var tablesorterCookieJar = $.cookieJar('tablesorter', {
            cookie: {
                path: '/'
            }
        });
        var mypage = tablesorterCookieJar.get('table-page');
        $("#table").tablesorter(
            {
              widthFixed: true
              widgets:['zebra','cookie']
            }
          ).tablesorterPager(
              {
                  container: $("#table-pager"),
                  positionFixed: false,
                  size: 20,
                  page: mypage
              }
          );
Yearly Indexes: 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2015 2019 2020 2022