From 328bba34a7d18330a9462d692cfc646401772437 Mon Sep 17 00:00:00 2001 From: Mottie Date: Tue, 3 Dec 2013 17:55:25 -0600 Subject: [PATCH] addRows method now works on an empty table. Fixes #450 --- addons/pager/jquery.tablesorter.pager.js | 4 +-- js/jquery.tablesorter.js | 46 +++++++++++++----------- js/widgets/widget-pager.js | 4 +-- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/addons/pager/jquery.tablesorter.pager.js b/addons/pager/jquery.tablesorter.pager.js index 1f2ef4bb..06d716fa 100644 --- a/addons/pager/jquery.tablesorter.pager.js +++ b/addons/pager/jquery.tablesorter.pager.js @@ -612,7 +612,7 @@ } $t - .unbind('filterStart filterEnd sortEnd disable enable destroy update pageSize '.split(' ').join('.pager ')) + .unbind('filterStart filterEnd sortEnd disable enable destroy update updateRows updateAll addRows pageSize '.split(' ').join('.pager ')) .bind('filterStart.pager', function(e, filters) { p.currentFilters = filters; }) @@ -636,7 +636,7 @@ e.stopPropagation(); destroyPager(table, p); }) - .bind('update.pager', function(e){ + .bind('update updateRows updateAll addRows '.split(' ').join('.pager '), function(e){ e.stopPropagation(); hideRows(table, p); }) diff --git a/js/jquery.tablesorter.js b/js/jquery.tablesorter.js index 899630c7..f755a936 100644 --- a/js/jquery.tablesorter.js +++ b/js/jquery.tablesorter.js @@ -814,28 +814,34 @@ }) .bind("addRows.tablesorter", function(e, $row, resort, callback) { e.stopPropagation(); - var i, rows = $row.filter('tr').length, - dat = [], l = $row[0].cells.length, - tbdy = $this.find('tbody').index( $row.parents('tbody').filter(':first') ); - // fixes adding rows to an empty table - see issue #179 - if (!c.parsers) { - buildParserCache(table); - } - // add each row - for (i = 0; i < rows; i++) { - // add each cell - for (j = 0; j < l; j++) { - dat[j] = c.parsers[j].format( getElementText(table, $row[i].cells[j], j), table, $row[i].cells[j], j ); + if (isEmptyObject(c.cache)) { + // empty table, do an update instead - fixes #450 + updateHeader(table); + commonUpdate(table, resort, callback); + } else { + var i, rows = $row.filter('tr').length, + dat = [], l = $row[0].cells.length, + tbdy = $this.find('tbody').index( $row.parents('tbody').filter(':first') ); + // fixes adding rows to an empty table - see issue #179 + if (!c.parsers) { + buildParserCache(table); } - // add the row index to the end - dat.push(c.cache[tbdy].row.length); - // update cache - c.cache[tbdy].row.push([$row[i]]); - c.cache[tbdy].normalized.push(dat); - dat = []; + // add each row + for (i = 0; i < rows; i++) { + // add each cell + for (j = 0; j < l; j++) { + dat[j] = c.parsers[j].format( getElementText(table, $row[i].cells[j], j), table, $row[i].cells[j], j ); + } + // add the row index to the end + dat.push(c.cache[tbdy].row.length); + // update cache + c.cache[tbdy].row.push([$row[i]]); + c.cache[tbdy].normalized.push(dat); + dat = []; + } + // resort using current settings + checkResort($this, resort, callback); } - // resort using current settings - checkResort($this, resort, callback); }) .bind("sorton.tablesorter", function(e, list, callback, init) { var c = table.config; diff --git a/js/widgets/widget-pager.js b/js/widgets/widget-pager.js index 41593ab3..a1ead962 100644 --- a/js/widgets/widget-pager.js +++ b/js/widgets/widget-pager.js @@ -200,7 +200,7 @@ tsp = ts.pager = { s = wo.pager_selectors; c.$table - .unbind('filterStart filterEnd sortEnd disable enable destroy update pageSize '.split(' ').join('.pager ')) + .unbind('filterStart filterEnd sortEnd disable enable destroy update updateRows updateAll addRows pageSize '.split(' ').join('.pager ')) .bind('filterStart.pager', function(e, filters) { p.currentFilters = filters; }) @@ -224,7 +224,7 @@ tsp = ts.pager = { e.stopPropagation(); tsp.destroyPager(table, c); }) - .on('update.pager', function(e){ + .on('update updateRows updateAll addRows '.split(' ').join('.pager '), function(e){ e.stopPropagation(); tsp.hideRows(table, c); })