From 2993069e04d72afcb8da883827ad52ab14427057 Mon Sep 17 00:00:00 2001 From: Mottie Date: Thu, 3 Oct 2013 07:27:23 -0500 Subject: [PATCH] added pager savePages option. Fixes #345 --- addons/pager/jquery.tablesorter.pager.js | 41 ++++++++++++++++++------ docs/example-pager.html | 5 +++ docs/index.html | 8 +++++ 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/addons/pager/jquery.tablesorter.pager.js b/addons/pager/jquery.tablesorter.pager.js index a62a6fba..3922024f 100644 --- a/addons/pager/jquery.tablesorter.pager.js +++ b/addons/pager/jquery.tablesorter.pager.js @@ -6,6 +6,8 @@ ;(function($) { "use strict"; /*jshint supernew:true */ + var ts = $.tablesorter; + $.extend({ tablesorterPager: new function() { this.defaults = { @@ -56,6 +58,9 @@ // Number of visible rows size: 10, + // Save pager page & size if the storage script is loaded (requires $.tablesorter.storage in jquery.tablesorter.widgets.js) + savePages: true, + // if true, the table will remain the same height no matter how many records are displayed. The space is made up by an empty // table row set to a height to compensate; default is false fixedHeight: false, @@ -139,7 +144,16 @@ } } pagerArrows(p); - if (p.initialized && flag !== false) { c.$table.trigger('pagerComplete', p); } + if (p.initialized && flag !== false) { + c.$table.trigger('pagerComplete', p); + // save pager info to storage + if (p.savePages && ts.storage) { + ts.storage(table, 'tablesorter-pager', { + page : p.page, + size : p.size + }); + } + } }, fixHeight = function(table, p) { @@ -279,13 +293,15 @@ } } if (c.showProcessing) { - $.tablesorter.isProcessing(table); // remove loading icon + ts.isProcessing(table); // remove loading icon } $t.trigger('update'); p.totalPages = Math.ceil( p.totalRows / p.size ); updatePageDisplay(table, p); fixHeight(table, p); - if (p.initialized) { $t.trigger('pagerChange', p); } + if (p.initialized) { + $t.trigger('pagerChange', p); + } } if (!p.initialized) { p.initialized = true; @@ -299,7 +315,7 @@ c = table.config; if ( url !== '' ) { if (c.showProcessing) { - $.tablesorter.isProcessing(table, true); // show loading icon + ts.isProcessing(table, true); // show loading icon } $doc.bind('ajaxError.pager', function(e, xhr, settings, exception) { renderAjax(null, table, p, xhr, exception); @@ -366,12 +382,12 @@ if ( e > rows.length ) { e = rows.length; } - $.tablesorter.clearTableBody(table); - $tb = $.tablesorter.processTbody(table, table.config.$tbodies.eq(0), true); + ts.clearTableBody(table); + $tb = ts.processTbody(table, table.config.$tbodies.eq(0), true); for ( i = s; i < e; i++ ) { $tb.append(rows[i]); } - $.tablesorter.processTbody(table, $tb, false); + ts.processTbody(table, $tb, false); } if ( p.page >= p.totalPages ) { moveToLastPage(table, p); @@ -497,8 +513,14 @@ $t = c.$table, // added in case the pager is reinitialized after being destroyed. pager = p.$container = $(p.container).addClass('tablesorter-pager').show(); - p.oldAjaxSuccess = p.oldAjaxSuccess || p.ajaxObject.success; - c.appender = $this.appender; + p.oldAjaxSuccess = p.oldAjaxSuccess || p.ajaxObject.success; + c.appender = $this.appender; + + if (p.savePages && ts.storage) { + t = ts.storage(table, 'tablesorter-pager'); + p.page = isNaN(t.page) ? p.page : t.page; + p.size = isNaN(t.size) ? p.size : t.size; + } $t .unbind('filterStart.pager filterEnd.pager sortEnd.pager disable.pager enable.pager destroy.pager update.pager pageSize.pager') @@ -578,7 +600,6 @@ p.page = $(this).val() - 1; moveToPage(table, p); }); - updatePageDisplay(table, p, false); } // page size selector diff --git a/docs/example-pager.html b/docs/example-pager.html index 3713f234..f15afb7f 100644 --- a/docs/example-pager.html +++ b/docs/example-pager.html @@ -16,6 +16,7 @@ + @@ -59,6 +60,9 @@ // Number of visible rows - default is 10 size: 10, + // Save pager page & size if the storage script is loaded (requires $.tablesorter.storage in jquery.tablesorter.widgets.js) + savePages : true, + // if true, the table will remain the same height no matter how many records are displayed. The space is made up by an empty // table row set to a height to compensate; default is false fixedHeight: true, @@ -192,6 +196,7 @@

diff --git a/docs/index.html b/docs/index.html index 196d597d..b892add7 100644 --- a/docs/index.html +++ b/docs/index.html @@ -2640,6 +2640,14 @@ $.extend($.tablesorter.themes.jui, { Example + + savePages + Boolean + true + Saves the current pager page size and number. This option requires the $.tablesorter.storage script in the jquery.tablesorter.widgets.js file (v2.11). + Example + + fixedHeight Boolean