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),
// resort table using the current sort; set to false to prevent resort, otherwise
// 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')
.find('tbody').append($row)
.trigger('addRows', [$row, resort]);</pre></div>
.trigger('addRows', [$row, resort, callback]);</pre></div>
</td>
<td><a href="example-add-rows.html">Example</a></td>
</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.
<div class="collapsible">
<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)
$("table").trigger("sorton", [sort]);</pre></div>
// A callback method was added in 2.3.9.
$("table").trigger("sorton", [sort, callback]);</pre></div>
</td>
<td><a href="example-trigger-sort.html">Example</a></td>
</tr>
@ -1611,8 +1619,12 @@ $("table tbody").append(html);
// let the plugin know that we made a update
// the resort flag set to anything BUT false (no quotes) will trigger an automatic
// table resort using the current sort
var resort = true;
$("table").trigger("update", [resort]);
// A callback method was added in 2.3.9.
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)
// after the update, so include the following if you want to specify a different sort

View File

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