From 4f43c31b7c949396334951222af82cfb307e452a Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Sun, 2 Apr 2017 03:40:10 -0500 Subject: [PATCH] Pager: use "all" only when option exists. Fixes #1364 & #1366 --- addons/pager/jquery.tablesorter.pager.js | 49 +++++++++++++----------- js/widgets/widget-pager.js | 46 +++++++++++++--------- 2 files changed, 54 insertions(+), 41 deletions(-) diff --git a/addons/pager/jquery.tablesorter.pager.js b/addons/pager/jquery.tablesorter.pager.js index fbc7d060..0d0e69ca 100644 --- a/addons/pager/jquery.tablesorter.pager.js +++ b/addons/pager/jquery.tablesorter.pager.js @@ -393,10 +393,8 @@ }, hideRowsSetup = function(table, p){ - var $el = p.$container.find(p.cssPageSize); - p.size = parsePageSize( p, $el.val(), 'get' ); - $el.val( p.size ); - $.data(table, 'pagerLastSize', p.size); + p.size = parsePageSize( p, p.$container.find(p.cssPageSize).val(), 'get' ); + setPageSize( table, p.size, p ); pagerArrows( table, p ); if ( !p.removeRows ) { hideRows(table, p); @@ -689,7 +687,7 @@ $.data(table, 'pagerLastPage', p.page); $.data(table, 'pagerLastSize', p.size); p.page = 0; - p.size = 'all'; + p.size = p.totalPages; p.totalPages = 1; $(table) .addClass('pagerDisabled') @@ -799,15 +797,9 @@ }, getTotalPages = function( table, p ) { - return ts.hasWidget( table, 'filter' ) ? Math.min( p.totalPages, p.filteredPages ) : p.totalPages; - }, - - // set to either set or get value - parsePageSize = function( p, size, mode ) { - var s = parseInt( size, 10 ) || p.size || p.settings.size || 10; - return p.initialized && (/all/i.test( size ) || s === p.totalRows) ? - // "get" to get `p.size` or "set" to set `pageSize.val()` - 'all' : ( mode === 'get' ? s : p.size ); + return ts.hasWidget( table, 'filter' ) ? + Math.min( p.totalPages, p.filteredPages ) : + p.totalPages; }, parsePageNumber = function( table, p ) { @@ -818,16 +810,26 @@ return p.page; }, + // set to either set or get value + parsePageSize = function( p, size, mode ) { + var s = parseInt( size, 10 ) || p.size || p.settings.size || 10; + if (p.initialized && (/all/i.test( s + ' ' + size ) || s === p.totalRows)) { + // Fixing #1364 & #1366 + return p.$container.find(p.cssPageSize + ' option[value="all"]').length ? + 'all' : p.totalRows; + } + // "get" to get `p.size` or "set" to set `pageSize.val()` + return mode === 'get' ? s : p.size; + }, + setPageSize = function(table, size, p) { + // "all" size is only returned if an "all" option exists - fixes #1366 p.size = parsePageSize( p, size, 'get' ); - var updatedPageSize = parsePageSize( p, p.size, 'set' ); - p.$container.find(p.cssPageSize).val( updatedPageSize !== 'all' ? updatedPageSize : - (p.$container.find(p.cssPageSize.concat(' option[value="').concat(size).concat('"]')).length !== 0 ? size : updatedPageSize) ); + p.$container.find( p.cssPageSize ).val( p.size ); $.data(table, 'pagerLastPage', parsePageNumber( table, p ) ); $.data(table, 'pagerLastSize', p.size); p.totalPages = p.size === 'all' ? 1 : Math.ceil( p.totalRows / p.size ); p.filteredPages = p.size === 'all' ? 1 : Math.ceil( p.filteredRows / p.size ); - moveToPage(table, p); }, moveToFirstPage = function(table, p) { @@ -908,8 +910,8 @@ $el = p.$container.find(p.cssPageSize); size = $el.find('option[selected]').val(); p.size = $.data(table, 'pagerLastSize') || parsePageSize( p, size, 'get' ); - $el.val( p.size ); // set page size p.totalPages = p.size === 'all' ? 1 : Math.ceil( getTotalPages( table, p ) / p.size ); + setPageSize(table, p.size, p); // set page size // if table id exists, include page display with aria info if ( table.id && !c.$table.attr( 'aria-describedby' ) ) { $el = p.$container.find( p.cssPageDisplay ); @@ -926,6 +928,7 @@ // tablesorter core update table ts.update( c ); setPageSize(table, p.size, p); + moveToPage(table, p); hideRowsSetup(table, p); if (c.debug) { console.log('Pager: Enabled'); @@ -954,8 +957,7 @@ t = ts.storage(table, p.storageKey) || {}; // fixes #387 p.page = isNaN(t.page) ? p.page : t.page; p.size = t.size === 'all' ? t.size : ( isNaN( t.size ) ? p.size : t.size ) || p.setSize || 10; - $.data(table, 'pagerLastSize', p.size); - pager.find(p.cssPageSize).val(p.size); + setPageSize(table, p.size, p); } // skipped rows p.regexRows = new RegExp('(' + (wo.filter_filteredRow || 'filtered') + '|' + c.selectorRemove.slice(1) + '|' + c.cssChildRow + ')'); @@ -1025,6 +1027,7 @@ .bind('pageSize refreshComplete '.split(' ').join(namespace + ' '), function(e, size){ e.stopPropagation(); setPageSize(table, parsePageSize( p, size, 'get' ), p); + moveToPage(table, p); hideRows(table, p); updatePageDisplay(table, p, false); }) @@ -1092,8 +1095,8 @@ if ( !$(this).hasClass(p.cssDisabled) ) { var size = $(this).val(); // in case there are more than one pager - p.$container.find(p.cssGoto).val( size ); setPageSize(table, size, p); + moveToPage(table, p); changeHeight(table, p); } return false; @@ -1127,6 +1130,8 @@ if (!p.ajax && !p.initialized) { p.initializing = false; p.initialized = true; + // update page size on init + setPageSize(table, p.size, p); moveToPage(table, p); if (c.debug) { console.log('Pager: Triggering pagerInitialized'); diff --git a/js/widgets/widget-pager.js b/js/widgets/widget-pager.js index 2f92d404..637f97c9 100644 --- a/js/widgets/widget-pager.js +++ b/js/widgets/widget-pager.js @@ -180,8 +180,7 @@ t = ts.storage( table, wo.pager_storageKey ) || {}; // fixes #387 p.page = ( isNaN( t.page ) ? p.page : t.page ) || p.setPage || 0; p.size = t.size === 'all' ? t.size : ( isNaN( t.size ) ? p.size : t.size ) || p.setSize || 10; - $.data( table, 'pagerLastSize', p.size ); - p.$container.find( s.pageSize ).val( p.size ); + tsp.setPageSize( c, p.size ); } // skipped rows @@ -219,7 +218,6 @@ initComplete: function( c ) { var p = c.pager; tsp.bindEvents( c ); - tsp.setPageSize( c, 0 ); // page size 0 is ignored if ( !p.ajax ) { tsp.hideRowsSetup( c ); } @@ -228,6 +226,7 @@ p.initialized = true; p.initializing = false; p.isInitializing = false; + tsp.setPageSize( c, p.size ); // page size 0 is ignored if ( c.debug ) { console.log( 'Pager: Triggering pagerInitialized' ); } @@ -310,6 +309,7 @@ .on( 'pageSize refreshComplete '.split( ' ' ).join( namespace + ' ' ), function( e, size ) { e.stopPropagation(); tsp.setPageSize( c, tsp.parsePageSize( c, size, 'get' ) ); + tsp.moveToPage( c, p, true ); tsp.hideRows( c ); tsp.updatePageDisplay( c, false ); }) @@ -382,6 +382,7 @@ // in case there are more than one pager p.$container.find( wo.pager_selectors.pageSize ).val( size ); tsp.setPageSize( c, size ); + tsp.moveToPage( c, p, true ); tsp.changeHeight( c ); } return false; @@ -694,8 +695,7 @@ $el = p.$container.find( c.widgetOptions.pager_selectors.pageSize ), size = $el.val(); p.size = tsp.parsePageSize( c, size, 'get' ); - $el.val( p.size ); - $.data( c.table, 'pagerLastSize', p.size ); + tsp.setPageSize( c, p.size ); tsp.pagerArrows( c ); if ( !c.widgetOptions.pager_removeRows ) { tsp.hideRows( c ); @@ -1003,7 +1003,7 @@ $.data( table, 'pagerLastPage', p.page ); $.data( table, 'pagerLastSize', p.size ); p.page = 0; - p.size = 'all'; + p.size = p.totalPages; p.totalPages = 1; c.$table .addClass( 'pagerDisabled' ) @@ -1128,16 +1128,9 @@ }, getTotalPages: function( c, p ) { - return ts.hasWidget( c.table, 'filter' ) ? Math.min( p.totalPages, p.filteredPages ) : p.totalPages; - }, - - // set to either set or get value - parsePageSize: function( c, size, mode ) { - var p = c.pager, - s = parseInt( size, 10 ) || p.size || c.widgetOptions.pager_size || 10; - return p.initialized && (/all/i.test( size ) || s === p.totalRows) ? - // "get" to set `p.size` or "set" to set `pageSize.val()` - 'all' : ( mode === 'get' ? s : p.size ); + return ts.hasWidget( c.table, 'filter' ) ? + Math.min( p.totalPages, p.filteredPages ) : + p.totalPages; }, parsePageNumber: function( c, p ) { @@ -1148,18 +1141,32 @@ return p.page; }, + // set to either set or get value + parsePageSize: function( c, size, mode ) { + var p = c.pager, + wo = c.widgetOptions, + s = parseInt( size, 10 ) || p.size || wo.pager_size || 10; + if (p.initialized && (/all/i.test( s + ' ' + size ) || s === p.totalRows)) { + // Fixing #1364 & #1366 + return p.$container.find( wo.pager_selectors.pageSize + ' option[value="all"]').length ? + 'all' : p.totalRows; + } + // "get" to set `p.size` or "set" to set `pageSize.val()` + return mode === 'get' ? s : p.size; + }, + setPageSize: function( c, size ) { var p = c.pager, table = c.table; + // "all" size is only returned if an "all" option exists - fixes #1366 p.size = tsp.parsePageSize( c, size, 'get' ); p.$container .find( c.widgetOptions.pager_selectors.pageSize ) - .val( tsp.parsePageSize( c, p.size, 'set' ) ); + .val( p.size ); $.data( table, 'pagerLastPage', tsp.parsePageNumber( c, p ) ); $.data( table, 'pagerLastSize', p.size ); p.totalPages = p.size === 'all' ? 1 : Math.ceil( p.totalRows / p.size ); p.filteredPages = p.size === 'all' ? 1 : Math.ceil( p.filteredRows / p.size ); - tsp.moveToPage( c, p, true ); }, moveToFirstPage: function( c, p ) { @@ -1228,7 +1235,7 @@ p.page = $.data( table, 'pagerLastPage' ) || p.page || 0; size = $el.find('option[selected]' ).val(); p.size = $.data( table, 'pagerLastSize' ) || tsp.parsePageSize( c, size, 'get' ); - $el.val( p.size ); // set page size + tsp.setPageSize( c, p.size ); // set page size p.totalPages = p.size === 'all' ? 1 : Math.ceil( tsp.getTotalPages( c, p ) / p.size ); c.$table.removeClass( 'pagerDisabled' ); // if table id exists, include page display with aria info @@ -1247,6 +1254,7 @@ // tablesorter core update table ts.update( c ); tsp.setPageSize( c, p.size ); + tsp.moveToPage( c, p, true ); tsp.hideRowsSetup( c ); if ( c.debug ) { console.log( 'Pager: Enabled' );