Skip to content

Commit

Permalink
Pager: fix pager initialization with filter widget & display update. F…
Browse files Browse the repository at this point in the history
…ixes #755 & #757
  • Loading branch information
Mottie committed Oct 31, 2014
1 parent 2dd35ed commit e8452da
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
22 changes: 14 additions & 8 deletions addons/pager/jquery.tablesorter.pager.js
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
},

Expand Down Expand Up @@ -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 ]);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) || [];
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions js/jquery.tablesorter.widgets.js
Expand Up @@ -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*=","]';

Expand Down Expand Up @@ -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) || [];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 11 additions & 2 deletions js/widgets/widget-pager.js
Expand Up @@ -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') || [];
Expand Down Expand Up @@ -209,6 +210,7 @@ tsp = ts.pager = {

// pager initialized
p.initialized = true;
p.initializing = false;
p.isInitializing = false;
c.$table
.trigger('pagerInitialized', c)
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e8452da

Please sign in to comment.