Core: update config.totalRows variable before initialization. Fixes #670

This commit is contained in:
Mottie 2014-07-15 11:41:16 -05:00
parent 26493ec8bc
commit ed5a86ffad
2 changed files with 12 additions and 21 deletions

View File

@ -4996,24 +4996,17 @@ $('.tablesorter')[0].config.cache[0].normalized[0];
<tr id="variable-totalrows"> <tr id="variable-totalrows">
<td><a href="#" class="permalink">config.totalRows</a></td> <td><a href="#" class="permalink">config.totalRows</a></td>
<td>Numeric</td> <td>Numeric</td>
<td>This variable contains the total number of rows within the table, not including rows within info-only tbodies (<span class="version">v2.17.4</span>) <td>This variable contains the total number of rows within the table, not including rows within info-only tbodies (<span class="version">v2.17.4</span>; <span class="version updated">v2.17.5</span>)
<div class="collapsible"> <div class="collapsible">
<br> <br>
Access this internal value at after tablesorter has initialized. It is also included as a variable in the <code>filterEnd</code> event so you can update an external count like this: Access this internal value after tablesorter has initialized. It is also included as a variable in the <code>filterEnd</code> event so you can update an external count like this:
<pre class="prettyprint lang-js">$('table').bind('filterEnd', function(event, data){ <pre class="prettyprint lang-js">$('table').bind('filterInit filterEnd', function(event, data){
// use data.filteredRows or this.config.filteredRows // use data.filteredRows or this.config.filteredRows
$('.filter-rows').html( data.filteredRows ); $('.filter-rows').html( data.filteredRows );
$('.total-rows').html( data.totalRows ); $('.total-rows').html( data.totalRows );
});</pre>The <code>filterEnd</code> event does not fire during filter widget initialization, so if you need to force a search, add this code to the table initialziation to get a <code>filterEnd</code> even to fire (for now):
<pre class="prettyprint lang-js">$(function(){
$('table').tablesorter({
initialized: function(table){
$.tablesorter.setFilters( table, false, true );
}
});
});</pre><hr> });</pre><hr>
If using the pager plugin or widget, the value returned from the <code>filterEnd</code> event will not be accurate, so you'll need to bind to the <code>pagerComplete</code> event instead: If using the pager plugin or widget, the value returned from the <code>filterEnd</code> event will not be accurate, so you'll need to bind to the <code>pagerComplete</code> event instead:
<pre class="prettyprint lang-js">$('table').bind('filterEnd pagerComplete', function(event, data){ <pre class="prettyprint lang-js">$('table').bind('filterInit filterEnd pagerComplete', function(event, data){
// Note: data = table.config (filterEnd event); and data = table.config.pager (pagerComplete event) // Note: data = table.config (filterEnd event); and data = table.config.pager (pagerComplete event)
// both objects contain data.filteredRows & data.totalRows // both objects contain data.filteredRows & data.totalRows
$('.filter-rows').html( data.filteredRows ); $('.filter-rows').html( data.filteredRows );
@ -5096,26 +5089,19 @@ var wo = $('#mytable').data('tablesorter').widgetOptions;
<tr id="variable-filteredrows"> <tr id="variable-filteredrows">
<td><a href="#" class="permalink">config.filteredRows</a></td> <td><a href="#" class="permalink">config.filteredRows</a></td>
<td>Numeric</td> <td>Numeric</td>
<td>Only available when the filter widget is active. This variable contains the current number of filtered rows (<span class="version">v2.17.4</span>) <td>Only available when the filter widget is active. This variable contains the current number of filtered rows (<span class="version">v2.17.4</span>; <span class="version updated">v2.17.5</span>)
<div class="collapsible"> <div class="collapsible">
<br> <br>
This internal value will show an accurate number of filtered rows; which means the count won't include any rows from "information-only" tbodies.<br> This internal value will show an accurate number of filtered rows; which means the count won't include any rows from "information-only" tbodies.<br>
<br> <br>
Access this internal value at any time after the filter widget has initialized, or as a shortcut it is included as a variable in the <code>filterEnd</code> event so you can update an external count like this: Access this internal value at any time after the filter widget has initialized, or as a shortcut it is included as a variable in the <code>filterEnd</code> event so you can update an external count like this:
<pre class="prettyprint lang-js">$('table').bind('filterEnd', function(event, data){ <pre class="prettyprint lang-js">$('table').bind('filterInit filterEnd', function(event, data){
// use data.filteredRows or this.config.filteredRows // use data.filteredRows or this.config.filteredRows
$('.filter-rows').html( data.filteredRows ); $('.filter-rows').html( data.filteredRows );
$('.total-rows').html( data.totalRows ); $('.total-rows').html( data.totalRows );
});</pre>The <code>filterEnd</code> event does not fire during filter widget initialization, so if you need to force a search, add this code to the table initialziation to get a <code>filterEnd</code> even to fire (for now):
<pre class="prettyprint lang-js">$(function(){
$('table').tablesorter({
initialized: function(table){
$.tablesorter.setFilters( table, false, true );
}
});
});</pre><hr> });</pre><hr>
If using the pager plugin or widget, the value returned from the <code>filterEnd</code> event will not be accurate, so you'll need to bind to the <code>pagerComplete</code> event instead: If using the pager plugin or widget, the value returned from the <code>filterEnd</code> event will not be accurate, so you'll need to bind to the <code>pagerComplete</code> event instead:
<pre class="prettyprint lang-js">$('table').bind('filterEnd pagerComplete', function(event, data){ <pre class="prettyprint lang-js">$('table').bind('filterInit filterEnd pagerComplete', function(event, data){
// Note: data = table.config (filterEnd event); and data = table.config.pager (pagerComplete event) // Note: data = table.config (filterEnd event); and data = table.config.pager (pagerComplete event)
// both objects contain data.filteredRows & data.totalRows // both objects contain data.filteredRows & data.totalRows
$('.filter-rows').html( data.filteredRows ); $('.filter-rows').html( data.filteredRows );

View File

@ -274,6 +274,7 @@
$tb = c.$table.children('tbody'), $tb = c.$table.children('tbody'),
parsers = c.parsers; parsers = c.parsers;
c.cache = {}; c.cache = {};
c.totalRows = 0;
// if no parsers found, return - it's an empty table. // if no parsers found, return - it's an empty table.
if (!parsers) { if (!parsers) {
return c.debug ? log('Warning: *Empty table!* Not building a cache') : ''; return c.debug ? log('Warning: *Empty table!* Not building a cache') : '';
@ -343,6 +344,8 @@
cc.normalized.push(cols); cc.normalized.push(cols);
} }
cc.colMax = colMax; cc.colMax = colMax;
// total up rows, not including child rows
c.totalRows += cc.normalized.length;
} }
} }
if (c.showProcessing) { if (c.showProcessing) {
@ -1043,6 +1046,8 @@
fixColumnWidth(table); fixColumnWidth(table);
// try to auto detect column type, and store in tables config // try to auto detect column type, and store in tables config
buildParserCache(table); buildParserCache(table);
// start total row count at zero
c.totalRows = 0;
// build the cache for the tbody cells // build the cache for the tbody cells
// delayInit will delay building the cache until the user starts a sort // delayInit will delay building the cache until the user starts a sort
if (!c.delayInit) { buildCache(table); } if (!c.delayInit) { buildCache(table); }