From e8452da206544f863766c13224729552f735579b Mon Sep 17 00:00:00 2001 From: Mottie Date: Fri, 31 Oct 2014 11:52:25 -0500 Subject: [PATCH] Pager: fix pager initialization with filter widget & display update. Fixes #755 & #757 --- addons/pager/jquery.tablesorter.pager.js | 22 ++++++++++++++-------- js/jquery.tablesorter.widgets.js | 9 +++------ js/widgets/widget-pager.js | 13 +++++++++++-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/addons/pager/jquery.tablesorter.pager.js b/addons/pager/jquery.tablesorter.pager.js index c0451359..539f98f7 100644 --- a/addons/pager/jquery.tablesorter.pager.js +++ b/addons/pager/jquery.tablesorter.pager.js @@ -150,7 +150,7 @@ }, updatePageDisplay = function(table, p, completed) { - if ( !p.initialized ) { return; } + if ( p.initializing ) { return; } var s, t, $out, c = table.config, sz = p.size || 10; // don't allow dividing by zero @@ -477,10 +477,11 @@ } if (!p.initialized) { p.initialized = true; + p.initializing = false; $(table) .trigger('applyWidgets') - .trigger('pagerInitialized', p) - .trigger('pagerComplete', p); + .trigger('pagerInitialized', p); + updatePageDisplay(table, p); } }, @@ -603,7 +604,7 @@ } ts.processTbody(table, $tb, false); } - updatePageDisplay(table, p, true); + updatePageDisplay(table, p, false); if (table.isUpdating) { $t.trigger('updateComplete', [ table, true ]); } @@ -660,10 +661,13 @@ if ( pageMoved !== false && p.initialized && $.isEmptyObject(c.cache)) { return updateCache(table); } + // abort page move if the table has filters and has not been initialized + if (p.ajax && ts.hasWidget(table, 'filter') && !c.widgetOptions.filter_initialized) { return; } calcFilters(table, p); pg = Math.min( p.totalPages, p.filteredPages ); if ( p.page < 0 ) { p.page = 0; } if ( p.page > ( pg - 1 ) && pg !== 0 ) { p.page = pg - 1; } + // fixes issue where one currentFilter is [] and the other is ['','',''], // making the next if comparison think the filters are different (joined by commas). Fixes #202. l.currentFilters = (l.currentFilters || []).join('') === '' ? [] : l.currentFilters; @@ -813,6 +817,7 @@ } p.oldAjaxSuccess = p.oldAjaxSuccess || p.ajaxObject.success; c.appender = $this.appender; + p.initializing = true; if (ts.filter && $.inArray('filter', c.widgets) >= 0) { // get any default filter settings (data-value attribute) fixes #388 p.currentFilters = c.$table.data('lastSearch') || ts.filter.setDefaults(table, c, c.widgetOptions) || []; @@ -839,8 +844,8 @@ } }) // update pager after filter widget completes - .bind('filterEnd.pager sortEnd.pager', function() { - if (p.initialized) { + .bind('filterEnd.pager sortEnd.pager', function(e) { + if (p.initialized || p.initializing) { if (c.delayInit && c.rowsCopy && c.rowsCopy.length === 0) { // make sure we have a copy of all table rows once the cache has been built updateCache(table); @@ -962,10 +967,11 @@ } // pager initialized - if (!p.ajax) { + if (!p.ajax && !p.initialized) { + p.initializing = false; p.initialized = true; moveToPage(table, p); - updatePageDisplay(table, p, true); + updatePageDisplay(table, p, false); $(table) .trigger('pagerInitialized', p) .trigger('pagerComplete', p); diff --git a/js/jquery.tablesorter.widgets.js b/js/jquery.tablesorter.widgets.js index 62f23e4f..3454a5ef 100644 --- a/js/jquery.tablesorter.widgets.js +++ b/js/jquery.tablesorter.widgets.js @@ -596,7 +596,6 @@ ts.filter = { wo.filter_initTimer = null; wo.filter_formatterCount = 0; wo.filter_formatterInit = []; - wo.filter_initializing = true; wo.filter_anyColumnSelector = '[data-column="all"],[data-column="any"]'; wo.filter_multipleColumnSelector = '[data-column*="-"],[data-column*=","]'; @@ -723,7 +722,7 @@ ts.filter = { c.filteredRows = c.totalRows; // add default values - c.$table.bind('tablesorter-initialized pagerInitialized', function() { + c.$table.bind('tablesorter-initialized pagerBeforeInitialized', function() { // redefine "wo" as it does not update properly inside this callback var wo = this.config.widgetOptions; filters = ts.filter.setDefaults(table, c, wo) || []; @@ -763,11 +762,9 @@ ts.filter = { var wo = c.widgetOptions, count = 0, completed = function(){ - // set initializing false first so findRows will process - wo.filter_initializing = false; - ts.filter.findRows(c.table, c.$table.data('lastSearch'), null); wo.filter_initialized = true; c.$table.trigger('filterInit', c); + ts.filter.findRows(c.table, c.$table.data('lastSearch'), null); }; $.each( wo.filter_formatterInit, function(i, val) { if (val === 1) { @@ -1096,7 +1093,7 @@ ts.filter = { return columns; }, findRows: function(table, filters, combinedFilters) { - if (table.config.lastCombinedFilter === combinedFilters || table.config.widgetOptions.filter_initializing) { return; } + if (table.config.lastCombinedFilter === combinedFilters || !table.config.widgetOptions.filter_initialized) { return; } var len, $rows, rowIndex, tbodyIndex, $tbody, $cells, $cell, columnIndex, childRow, lastSearch, hasSelect, matches, result, showRow, time, val, indx, notFiltered, searchFiltered, filterMatched, excludeMatch, fxn, ffxn, diff --git a/js/widgets/widget-pager.js b/js/widgets/widget-pager.js index 6c9bc4a9..8bc17873 100644 --- a/js/widgets/widget-pager.js +++ b/js/widgets/widget-pager.js @@ -159,6 +159,7 @@ tsp = ts.pager = { p.totalRows = c.$tbodies.eq(0).children('tr').not( wo.pager_countChildRows ? '' : '.' + c.cssChildRow ).length; p.oldAjaxSuccess = p.oldAjaxSuccess || wo.pager_ajaxObject.success; c.appender = tsp.appender; + p.initializing = true; if (ts.filter && $.inArray('filter', c.widgets) >= 0) { // get any default filter settings (data-value attribute) fixes #388 p.currentFilters = c.$table.data('lastSearch') || []; @@ -209,6 +210,7 @@ tsp = ts.pager = { // pager initialized p.initialized = true; + p.initializing = false; p.isInitializing = false; c.$table .trigger('pagerInitialized', c) @@ -233,7 +235,7 @@ tsp = ts.pager = { }) // update pager after filter widget completes .on('filterEnd.pager sortEnd.pager', function() { - if (p.initialized) { + if (p.initialized || p.initializing) { if (c.delayInit && c.rowsCopy && c.rowsCopy.length === 0) { // make sure we have a copy of all table rows once the cache has been built tsp.updateCache(table); @@ -374,6 +376,7 @@ tsp = ts.pager = { }, updatePageDisplay: function(table, c, completed) { + if ( c.pager.initializing ) { return; } var s, t, $out, wo = c.widgetOptions, p = c.pager, @@ -684,6 +687,7 @@ tsp = ts.pager = { p.last.totalRows = p.totalRows; p.last.currentFilters = p.currentFilters; p.last.sortList = (c.sortList || []).join(','); + p.initializing = false; tsp.updatePageDisplay(table, c); $t.trigger('updateCache', [function(){ if (p.initialized) { @@ -821,7 +825,7 @@ tsp = ts.pager = { ts.processTbody(table, $tb, false); } - tsp.updatePageDisplay(table, c); + tsp.updatePageDisplay(table, c, false); wo.pager_startPage = p.page; wo.pager_size = p.size; @@ -889,10 +893,15 @@ tsp = ts.pager = { var pg, c = table.config, wo = c.widgetOptions, l = p.last; + + // abort page move if the table has filters and has not been initialized + if (p.ajax && !wo.filter_initialized && ts.hasWidget(table, 'filter')) { return; } + tsp.calcFilters(table, c); pg = Math.min( p.totalPages, p.filteredPages ); if ( p.page < 0 ) { p.page = 0; } if ( p.page > ( pg - 1 ) && pg !== 0 ) { p.page = pg - 1; } + // fixes issue where one current filter is [] and the other is ['','',''], // making the next if comparison think the filters as different. Fixes #202. l.currentFilters = (l.currentFilters || []).join('') === '' ? [] : l.currentFilters;