prevent dividing by zero (pager size)

This commit is contained in:
Mottie 2013-10-18 07:49:57 -05:00
parent 6f35c682a1
commit 6e93d71f9f
3 changed files with 22 additions and 29 deletions

View File

@ -1,6 +1,6 @@
/*! /*!
* tablesorter pager plugin * tablesorter pager plugin
* updated 10/17/2013 * updated 10/18/2013
*/ */
/*jshint browser:true, jquery:true, unused:false */ /*jshint browser:true, jquery:true, unused:false */
/*global toString:true */ /*global toString:true */
@ -115,10 +115,11 @@
var i, pg, s, out, var i, pg, s, out,
c = table.config, c = table.config,
f = c.$table.hasClass('hasFilters') && !p.ajaxUrl, f = c.$table.hasClass('hasFilters') && !p.ajaxUrl,
t = (c.widgetOptions && c.widgetOptions.filter_filteredRow || 'filtered') + ',' + c.selectorRemove; t = (c.widgetOptions && c.widgetOptions.filter_filteredRow || 'filtered') + ',' + c.selectorRemove,
p.totalPages = Math.ceil( p.totalRows / p.size ); // needed for "pageSize" method sz = p.size || 10; // don't allow dividing by zero
p.totalPages = Math.ceil( p.totalRows / sz ); // needed for "pageSize" method
p.filteredRows = (f) ? c.$tbodies.eq(0).children('tr:not(.' + t + ')').length : p.totalRows; p.filteredRows = (f) ? c.$tbodies.eq(0).children('tr:not(.' + t + ')').length : p.totalRows;
p.filteredPages = (f) ? Math.ceil( p.filteredRows / p.size ) || 1 : p.totalPages; p.filteredPages = (f) ? Math.ceil( p.filteredRows / sz ) || 1 : p.totalPages;
if ( Math.min( p.totalPages, p.filteredPages ) >= 0 ) { if ( Math.min( p.totalPages, p.filteredPages ) >= 0 ) {
t = (p.size * p.page > p.filteredRows); t = (p.size * p.page > p.filteredRows);
p.startRow = (t) ? 1 : (p.filteredRows === 0 ? 0 : p.size * p.page + 1); p.startRow = (t) ? 1 : (p.filteredRows === 0 ? 0 : p.size * p.page + 1);
@ -309,7 +310,7 @@
if (c.showProcessing) { if (c.showProcessing) {
ts.isProcessing(table); // remove loading icon ts.isProcessing(table); // remove loading icon
} }
p.totalPages = Math.ceil( p.totalRows / p.size ); p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
updatePageDisplay(table, p); updatePageDisplay(table, p);
fixHeight(table, p); fixHeight(table, p);
if (p.initialized) { if (p.initialized) {
@ -466,7 +467,7 @@
p.$size.val(size); p.$size.val(size);
$.data(table, 'pagerLastPage', p.page); $.data(table, 'pagerLastPage', p.page);
$.data(table, 'pagerLastSize', p.size); $.data(table, 'pagerLastSize', p.size);
p.totalPages = Math.ceil( p.totalRows / p.size ); p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
moveToPage(table, p); moveToPage(table, p);
}, },
@ -514,7 +515,7 @@
p.page = $.data(table, 'pagerLastPage') || p.page || 0; p.page = $.data(table, 'pagerLastPage') || p.page || 0;
p.size = $.data(table, 'pagerLastSize') || parseInt(pg.find('option[selected]').val(), 10) || p.size; p.size = $.data(table, 'pagerLastSize') || parseInt(pg.find('option[selected]').val(), 10) || p.size;
pg.val(p.size); // set page size pg.val(p.size); // set page size
p.totalPages = Math.ceil( Math.min( p.totalPages, p.filteredPages ) / p.size); p.totalPages = Math.ceil( Math.min( p.totalPages, p.filteredPages ) / ( p.size || 10 ) );
if ( triggered ) { if ( triggered ) {
$(table).trigger('update'); $(table).trigger('update');
setPageSize(table, p.size, p); setPageSize(table, p.size, p);
@ -529,7 +530,7 @@
table.config.rowsCopy = rows; table.config.rowsCopy = rows;
p.totalRows = rows.length; p.totalRows = rows.length;
p.size = $.data(table, 'pagerLastSize') || p.size; p.size = $.data(table, 'pagerLastSize') || p.size;
p.totalPages = Math.ceil(p.totalRows / p.size); p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
renderTable(table, rows, p); renderTable(table, rows, p);
} }
}; };
@ -551,7 +552,7 @@
if (p.savePages && ts.storage) { if (p.savePages && ts.storage) {
t = ts.storage(table, 'tablesorter-pager') || {}; // fixes #387 t = ts.storage(table, 'tablesorter-pager') || {}; // fixes #387
p.page = isNaN(t.page) ? p.page : t.page; p.page = isNaN(t.page) ? p.page : t.page;
p.size = isNaN(t.size) ? p.size : t.size; p.size = ( isNaN(t.size) ? p.size : t.size ) || 10;
} }
$t $t

View File

@ -25,22 +25,13 @@
<!-- Tablesorter: optional --> <!-- Tablesorter: optional -->
<script src="../js/jquery.tablesorter.widgets.js"></script> <script src="../js/jquery.tablesorter.widgets.js"></script>
<script>
$(function(){
$('.accordion').accordion({
autoHeight: false,
collapsible : true
});
});
</script>
<script id="js">$(function(){ <script id="js">$(function(){
// Initialize tablesorter // Initialize tablesorter
// *********************** // ***********************
$("table") $("table")
.tablesorter({ .tablesorter({
theme: 'blue', debug: true, theme: 'blue',
widthFixed: true, widthFixed: true,
sortLocaleCompare: true, // needed for accented characters in the data sortLocaleCompare: true, // needed for accented characters in the data
widgets: ['zebra', 'filter', 'pager'], widgets: ['zebra', 'filter', 'pager'],

View File

@ -1,4 +1,4 @@
/* Pager widget (beta) for TableSorter 10/17/2013 */ /* Pager widget (beta) for TableSorter 10/18/2013 */
/*jshint browser:true, jquery:true, unused:false */ /*jshint browser:true, jquery:true, unused:false */
;(function($){ ;(function($){
"use strict"; "use strict";
@ -7,7 +7,7 @@ var tsp,
ts.addWidget({ ts.addWidget({
id: "pager", id: "pager",
priority: 60, priority: 5,
options : { options : {
// output default: '{page}/{totalPages}' // output default: '{page}/{totalPages}'
// possible variables: {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows} // possible variables: {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
@ -138,7 +138,7 @@ tsp = ts.pager = {
if (wo.pager_savePages && ts.storage) { if (wo.pager_savePages && ts.storage) {
t = ts.storage(table, 'tablesorter-pager') || {}; // fixes #387 t = ts.storage(table, 'tablesorter-pager') || {}; // fixes #387
p.page = isNaN(t.page) ? p.page : t.page; p.page = isNaN(t.page) ? p.page : t.page;
p.size = isNaN(t.size) ? p.size : t.size; p.size = ( isNaN(t.size) ? p.size : t.size ) || 10;
} }
// clear initialized flag // clear initialized flag
@ -299,12 +299,13 @@ tsp = ts.pager = {
wo = c.widgetOptions, wo = c.widgetOptions,
p = c.pager, p = c.pager,
f = c.$table.hasClass('hasFilters') && !wo.pager_ajaxUrl, f = c.$table.hasClass('hasFilters') && !wo.pager_ajaxUrl,
t = (c.widgetOptions && c.widgetOptions.filter_filteredRow || 'filtered') + ',' + c.selectorRemove; t = (c.widgetOptions && c.widgetOptions.filter_filteredRow || 'filtered') + ',' + c.selectorRemove,
sz = p.size || 10; // don't allow dividing by zero
p.$size.removeClass(wo.pager_css.disabled).removeAttr('disabled'); p.$size.removeClass(wo.pager_css.disabled).removeAttr('disabled');
p.$goto.removeClass(wo.pager_css.disabled).removeAttr('disabled'); p.$goto.removeClass(wo.pager_css.disabled).removeAttr('disabled');
p.totalPages = Math.ceil( p.totalRows / p.size ); // needed for "pageSize" method p.totalPages = Math.ceil( p.totalRows / sz ); // needed for "pageSize" method
p.filteredRows = (f) ? c.$tbodies.eq(0).children('tr:not(.' + t + ')').length : p.totalRows; p.filteredRows = (f) ? c.$tbodies.eq(0).children('tr:not(.' + t + ')').length : p.totalRows;
p.filteredPages = (f) ? Math.ceil( p.filteredRows / p.size ) || 1 : p.totalPages; p.filteredPages = (f) ? Math.ceil( p.filteredRows / sz ) || 1 : p.totalPages;
if ( Math.min( p.totalPages, p.filteredPages ) >= 0 ) { if ( Math.min( p.totalPages, p.filteredPages ) >= 0 ) {
t = (p.size * p.page > p.filteredRows); t = (p.size * p.page > p.filteredRows);
p.startRow = (t) ? 1 : (p.filteredRows === 0 ? 0 : p.size * p.page + 1); p.startRow = (t) ? 1 : (p.filteredRows === 0 ? 0 : p.size * p.page + 1);
@ -493,7 +494,7 @@ tsp = ts.pager = {
if (c.showProcessing) { if (c.showProcessing) {
ts.isProcessing(table); // remove loading icon ts.isProcessing(table); // remove loading icon
} }
p.totalPages = Math.ceil( p.totalRows / p.size ); p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
tsp.updatePageDisplay(table, c); tsp.updatePageDisplay(table, c);
tsp.fixHeight(table, c); tsp.fixHeight(table, c);
if (p.initialized) { if (p.initialized) {
@ -665,7 +666,7 @@ tsp = ts.pager = {
p.$size.val(size); p.$size.val(size);
$.data(table, 'pagerLastPage', p.page); $.data(table, 'pagerLastPage', p.page);
$.data(table, 'pagerLastSize', p.size); $.data(table, 'pagerLastSize', p.size);
p.totalPages = Math.ceil( p.totalRows / p.size ); p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
tsp.moveToPage(table, p); tsp.moveToPage(table, p);
}, },
@ -714,7 +715,7 @@ tsp = ts.pager = {
p.page = $.data(table, 'pagerLastPage') || p.page || 0; p.page = $.data(table, 'pagerLastPage') || p.page || 0;
p.size = $.data(table, 'pagerLastSize') || parseInt(p.$size.find('option[selected]').val(), 10) || p.size; p.size = $.data(table, 'pagerLastSize') || parseInt(p.$size.find('option[selected]').val(), 10) || p.size;
p.$size.val(p.size); // set page size p.$size.val(p.size); // set page size
p.totalPages = Math.ceil( Math.min( p.totalPages, p.filteredPages ) / p.size); p.totalPages = Math.ceil( Math.min( p.totalPages, p.filteredPages ) / ( p.size || 10 ) );
c.$table.removeClass('pagerDisabled'); c.$table.removeClass('pagerDisabled');
if ( triggered ) { if ( triggered ) {
c.$table.trigger('update'); c.$table.trigger('update');
@ -730,7 +731,7 @@ tsp = ts.pager = {
table.config.rowsCopy = rows; table.config.rowsCopy = rows;
p.totalRows = rows.length; p.totalRows = rows.length;
p.size = $.data(table, 'pagerLastSize') || p.size; p.size = $.data(table, 'pagerLastSize') || p.size;
p.totalPages = Math.ceil(p.totalRows / p.size); p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
tsp.moveToPage(table, p); tsp.moveToPage(table, p);
// tsp.renderTable(table, rows); // tsp.renderTable(table, rows);
} }