added method callbacks & updateComplete event

This commit is contained in:
Rob Garrison 2012-06-20 09:38:07 -05:00
parent 8f020a5dba
commit 408faa1cad
2 changed files with 48 additions and 16 deletions

View File

@ -1581,10 +1581,14 @@ $('input.tablesorter-filter:eq(0)').trigger('search', false);</pre></div>
$row = $(row), $row = $(row),
// resort table using the current sort; set to false to prevent resort, otherwise // resort table using the current sort; set to false to prevent resort, otherwise
// any other value in resort will automatically trigger the table resort. // any other value in resort will automatically trigger the table resort.
resort = true; // A callback method was added in 2.3.9.
resort = true,
callback = function(table){
alert('rows have been added!');
};
$('table') $('table')
.find('tbody').append($row) .find('tbody').append($row)
.trigger('addRows', [$row, resort]);</pre></div> .trigger('addRows', [$row, resort, callback]);</pre></div>
</td> </td>
<td><a href="example-add-rows.html">Example</a></td> <td><a href="example-add-rows.html">Example</a></td>
</tr> </tr>
@ -1594,9 +1598,13 @@ $('input.tablesorter-filter:eq(0)').trigger('search', false);</pre></div>
<td>Use this method to sort an initialized table in the desired order. <td>Use this method to sort an initialized table in the desired order.
<div class="collapsible"> <div class="collapsible">
<pre class="js">// Choose a new sort order <pre class="js">// Choose a new sort order
var sort = [[0,0],[2,0]]; var sort = [[0,0],[2,0]],
callback = function(table){
alert('new sort applied to ' + table.id);
};
// Note that the sort value below is inside of another array (inside another set of square brackets) // Note that the sort value below is inside of another array (inside another set of square brackets)
$("table").trigger("sorton", [sort]);</pre></div> // A callback method was added in 2.3.9.
$("table").trigger("sorton", [sort, callback]);</pre></div>
</td> </td>
<td><a href="example-trigger-sort.html">Example</a></td> <td><a href="example-trigger-sort.html">Example</a></td>
</tr> </tr>
@ -1611,8 +1619,12 @@ $("table tbody").append(html);
// let the plugin know that we made a update // let the plugin know that we made a update
// the resort flag set to anything BUT false (no quotes) will trigger an automatic // the resort flag set to anything BUT false (no quotes) will trigger an automatic
// table resort using the current sort // table resort using the current sort
var resort = true; // A callback method was added in 2.3.9.
$("table").trigger("update", [resort]); var resort = true,
callback = function(table){
alert('new sort applied');
};
$("table").trigger("update", [resort, callback]);
// As of version 2.0.14, the table will automatically resort (using the current sort selection) // As of version 2.0.14, the table will automatically resort (using the current sort selection)
// after the update, so include the following if you want to specify a different sort // after the update, so include the following if you want to specify a different sort

View File

@ -604,6 +604,23 @@
return b - a; return b - a;
} }
function checkResort($table, flag, callback) {
var t = $table[0];
if (flag !== false) {
$table.trigger("sorton", [t.config.sortList, function(){
$table.trigger('updateComplete');
if (typeof callback === "function") {
callback(t);
}
}]);
} else {
$table.trigger('updateComplete');
if (typeof callback === "function") {
callback(t);
}
}
}
/* public methods */ /* public methods */
this.construct = function(settings) { this.construct = function(settings) {
return this.each(function() { return this.each(function() {
@ -611,12 +628,12 @@
if (!this.tHead || this.tBodies.length === 0) { return; } if (!this.tHead || this.tBodies.length === 0) { return; }
// declare // declare
var $headers, $cell, $this, var $headers, $cell, $this,
config, c, i, j, k, a, s, o, downTime, c, i, j, k, a, s, o, downTime,
m = $.metadata; m = $.metadata;
// new blank config object // new blank config object
this.config = {}; this.config = {};
// merge and extend. // merge and extend.
c = config = $.extend(true, this.config, $.tablesorter.defaults, settings); c = $.extend(true, this.config, $.tablesorter.defaults, settings);
if (c.debug) { $.data( this, 'startoveralltimer', new Date()); } if (c.debug) { $.data( this, 'startoveralltimer', new Date()); }
// store common expression for speed // store common expression for speed
@ -746,16 +763,16 @@
} }
// apply easy methods that trigger binded events // apply easy methods that trigger binded events
$this $this
.bind("update", function(e, resort) { .bind("update", function(e, resort, callback) {
// remove rows/elements before update // remove rows/elements before update
$(c.selectorRemove, this).remove(); $(c.selectorRemove, this).remove();
// rebuild parsers. // rebuild parsers.
c.parsers = buildParserCache(this, $headers); c.parsers = buildParserCache(this, $headers);
// rebuild the cache map // rebuild the cache map
buildCache(this); buildCache(this);
if (resort !== false) { $(this).trigger("sorton", [c.sortList]); } checkResort($this, resort, callback);
}) })
.bind("updateCell", function(e, cell, resort) { .bind("updateCell", function(e, cell, resort, callback) {
// get position from the dom. // get position from the dom.
var t = this, $tb = $(this).find('tbody'), row, pos, var t = this, $tb = $(this).find('tbody'), row, pos,
// update cache - format: function(s, table, cell, cellIndex) // update cache - format: function(s, table, cell, cellIndex)
@ -763,9 +780,9 @@
row = $tb.eq(tbdy).find('tr').index( $(cell).closest('tr') ); row = $tb.eq(tbdy).find('tr').index( $(cell).closest('tr') );
pos = [ row, cell.cellIndex]; pos = [ row, cell.cellIndex];
t.config.cache[tbdy].normalized[pos[0]][pos[1]] = c.parsers[pos[1]].format( getElementText(t, cell, pos[1]), t, cell, pos[1] ); t.config.cache[tbdy].normalized[pos[0]][pos[1]] = c.parsers[pos[1]].format( getElementText(t, cell, pos[1]), t, cell, pos[1] );
if (resort !== false) { $(this).trigger("sorton", [c.sortList]); } checkResort($this, resort, callback);
}) })
.bind("addRows", function(e, $row, resort) { .bind("addRows", function(e, $row, resort, callback) {
var i, rows = $row.filter('tr').length, var i, rows = $row.filter('tr').length,
dat = [], l = $row[0].cells.length, t = this, dat = [], l = $row[0].cells.length, t = this,
tbdy = $(this).find('tbody').index( $row.closest('tbody') ); tbdy = $(this).find('tbody').index( $row.closest('tbody') );
@ -783,9 +800,9 @@
dat = []; dat = [];
} }
// resort using current settings // resort using current settings
if (resort !== false) { $(this).trigger("sorton", [c.sortList]); } checkResort($this, resort, callback);
}) })
.bind("sorton", function(e, list, init) { .bind("sorton", function(e, list, callback, init) {
$(this).trigger("sortStart", this); $(this).trigger("sortStart", this);
var l = c.headerList.length; var l = c.headerList.length;
c.sortList = []; c.sortList = [];
@ -800,6 +817,9 @@
// sort the table and append it to the dom // sort the table and append it to the dom
multisort(this, c.sortList); multisort(this, c.sortList);
appendToTable(this, init); appendToTable(this, init);
if (typeof callback === "function") {
callback(this);
}
}) })
.bind("appendCache", function(e, init) { .bind("appendCache", function(e, init) {
appendToTable(this, init); appendToTable(this, init);
@ -825,7 +845,7 @@
applyWidget(this, true); applyWidget(this, true);
// if user has supplied a sort list to constructor. // if user has supplied a sort list to constructor.
if (c.sortList.length > 0) { if (c.sortList.length > 0) {
$this.trigger("sorton", [c.sortList, !c.initWidgets]); $this.trigger("sorton", [c.sortList, {}, !c.initWidgets]);
} else if (c.initWidgets) { } else if (c.initWidgets) {
// apply widget format // apply widget format
applyWidget(this); applyWidget(this);