Permalink
Browse files

added widget init function & updated saveSort widget

  • Loading branch information...
Mottie committed Feb 2, 2012
1 parent 3a84867 commit 12cf4675b75fa8c7cc8527dfc51b2bd5f6a166a7
View
@@ -34,6 +34,30 @@ Included all original [document pages](http://mottie.github.com/tablesorter/docs
View the [complete listing here](http://mottie.github.com/tablesorter/changelog.txt).
+####Version 2.0.28 (2012-2-1)
+
+* Added a new function to widgets called "init" which is called upon initialization, before any of the widgets are applied.
+ * I added it to allow the "saveSort" widget to get the saved sort data (localStorage or cookie) before the initial sort was applied.
+ * The "saveSort" widget is still compatible with the original tablesorter, but the original version will call all of the widgets twice on initialization, if using the "saveSort" widget.
+ * New add widget format is as follows:
+
+ ```javascript
+ $.tablesorter.addWidget({
+ id: 'myWidget',
+ init: function(table, allWidgets, thisWidget){
+ // widget initialization code - this is only run ONCE
+ // but in this example I call the format function because
+ // I want to keep it backwards compatible with the original tablesorter
+ thisWidget.format(table, true);
+ },
+ format: function(table, initFlag) {
+ // widget code to apply to the table AFTER EACH SORT
+ // the initFlag is true when format is called for the first time, but
+ // only if it is called from the init function
+ }
+ });
+ ```
+
####Version 2.0.27 (2012-1-31)
* Added `sortReset` option
View
@@ -1,5 +1,30 @@
TableSorter Change Log
+Version 2.0.28 (2012-2-1)
+============================
+
+* Added a new function to widgets called "init" which is called upon initialization, before any of the widgets are applied.
+ * I added it to allow the "saveSort" widget to get the saved sort data (localStorage or cookie) before the initial sort was applied.
+ * The "saveSort" widget is still compatible with the original tablesorter, but the original version will call all of the widgets twice on initialization, if using the "saveSort" widget.
+ * New add widget format is as follows:
+
+ ```javascript
+ $.tablesorter.addWidget({
+ id: 'myWidget',
+ init: function(table, allWidgets, thisWidget){
+ // widget initialization code - this is only run ONCE
+ // but in this example I call the format function because
+ // I want to keep it backwards compatible with the original tablesorter
+ thisWidget.format(table, true);
+ },
+ format: function(table, initFlag) {
+ // widget code to apply to the table AFTER EACH SORT
+ // the initFlag is true when format is called for the first time, but
+ // only if it is called from the init function
+ }
+ });
+ ```
+
Version 2.0.27 (2012-1-31)
============================
@@ -24,7 +24,7 @@
$("table").tablesorter({
// use save sort widget
- widgets: ["saveSort"]
+ widgets: ["saveSort", "zebra"]
});
View
@@ -1,6 +1,6 @@
/*
* TableSorter 2.0 - Client-side table sorting with ease!
-* Version 2.0.27
+* Version 2.0.28
* @requires jQuery v1.2.3
*
* Copyright (c) 2007 Christian Bach
@@ -76,7 +76,7 @@
* @author Christian Bach/christian.bach@polyester.se
* @contributor Rob Garrison/https://github.com/Mottie/tablesorter
*/
-(function($){
+!(function($){
$.extend({
tablesorter: new function(){
@@ -109,7 +109,7 @@
/* debuging utils */
function log(s) {
- if (typeof console !== "undefined" && typeof console.debug !== "undefined") {
+ if (typeof console !== "undefined" && typeof console.log !== "undefined") {
console.log(s);
} else {
alert(s);
@@ -266,6 +266,16 @@
return cache;
}
+ function initWidgets(table){
+ var i, w, l = widgets.length;
+ for (i = 0; i < l; i++) {
+ w = widgets[i];
+ if (w && w.hasOwnProperty('init')) {
+ w.init(table, widgets, w);
+ }
+ }
+ }
+
function getWidgetById(name) {
var i, w, l = widgets.length;
for (i = 0; i < l; i++) {
@@ -774,6 +784,8 @@
if ($.metadata && ($(this).metadata() && $(this).metadata().sortlist)) {
config.sortList = $(this).metadata().sortlist;
}
+ // initialize widgets
+ initWidgets(this);
// if user has supplied a sort list to constructor.
if (config.sortList.length > 0) {
$this.trigger("sorton", [config.sortList]);

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1,4 +1,4 @@
-/* TableSorter 2.0 Widgets - updated 1/31/2012
+/* TableSorter 2.0 Widgets - updated 2/1/2012
*
* jQuery UI Theme
* Column Styles
@@ -288,7 +288,11 @@ $.tablesorter.addWidget({
// **************************
$.tablesorter.addWidget({
id: 'saveSort',
- format: function(table) {
+ init: function(table, allWidgets, thisWidget){
+ // run widget format before all other widgets are applied to the table
+ thisWidget.format(table, true);
+ },
+ format: function(table, init) {
var n, d, k, ls, time, c = table.config,
// older browsers don't support JSON.stringify (http://caniuse.com/#search=json)
// if you need it, then include https://github.com/douglascrockford/JSON-js
@@ -329,13 +333,17 @@ $.tablesorter.addWidget({
}
// parse data
try { ls = $.parseJSON(ls); } catch(e) { ls = ''; }
- sortList = (ls && ls.hasOwnProperty('sortList') && $.isArray(ls.sortList)) ? [ls.sortList] : '';
+ sortList = (ls && ls.hasOwnProperty('sortList') && $.isArray(ls.sortList)) ? ls.sortList : '';
if (c.debug) {
$.tablesorter.benchmark('saveSort: Last sort for "' + n + '" obtained from ' + (c.hasLocalStorage ? 'local storage' : 'a cookie'), time);
}
- // update sort change
- if (sortList && sortList.length > 0) {
- $(table).trigger('sorton', sortList);
+ // init is true when widget init is run, this will run this widget before all other widgets have initialized
+ // this method allows using this widget in the original tablesorter plugin; but then it will run all widgets twice.
+ if (init && sortList && sortList.length > 0) {
+ c.sortList = sortList;
+ } else if (sortList && sortList.length > 0) {
+ // update sort change
+ $(table).trigger('sorton', [sortList]);
}
}
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,6 +1,6 @@
{
"name": "tablesorter",
- "version": "2.0.27",
+ "version": "2.0.28",
"title": "tablesorter",
"author": {
"name": "Christian Bach",

0 comments on commit 12cf467

Please sign in to comment.