diff --git a/docs/example-widgets.html b/docs/example-widgets.html
index 50796dbe..288187e6 100644
--- a/docs/example-widgets.html
+++ b/docs/example-widgets.html
@@ -28,9 +28,11 @@
// give the widget an id
id: "repeatHeaders",
-
- // format is called when the on init and when a sorting has finished
- format: function(table) {
+ options: {
+ rowsToSkip : 4
+ },
+ // format is called on init and when a sorting has finished
+ format: function(table, c, wo) {
var h, i, skip;
// cache and collect all TH headers
if (!this.headers) {
@@ -44,7 +46,7 @@
$(table).find("tr.repeated-header").remove();
// number of rows to skip
- skip = 4;
+ skip = wo && wo.rowsToSkip || 4;
// loop all tr elements and insert a copy of the "headers"
for (i = skip; i < table.tBodies[0].rows.length; i += (skip + 1)) {
@@ -84,277 +86,120 @@
+
+
+ Notes about the
addWidget
template below:
+
+ - The
id
block is required and must be unique.
+ - The
options
block (New! in v2.8):
+
+ - Include any widget options to be automatically be extended with any set widgetOptions (from
table.config.widgetOptions
).
+ - This block was added in tablesorter v2.8 and is not supported in older versions!
+ - As of v2.8, no included widgets will be using this to maintain backwards compatibility with older versions. This will change when v3.0 is released.
+ - This block is optional.
+
+
+ - The
init
block:
+
+ - This code is called only after tablesorter has initialized, but before initial sort and before any of the widgets are applied; it is only run once.
+ - This block was added in v2.0.28 and it not supported in older versions.
+ - Since, v2.0.28, only the saveSort widget uses this block to ensure that the stored sort is applied before .
+ - This block is optional.
+
+
+ - The
format
block:
+
+ - This block is part of the original
addWidget
template and is required.
+ - In the original template, only the
table
parameter is provided. After v2.0.28, config
and widgetOptions
were provided as additional parameters (sorry the previous docs were incorrect).
+ - The
initFlag
is correctly set in v2.8+.
+
+
+ - The
remove
block:
+
+ - This code is called when either the
refreshWidgets
or destroy
methods are called.
+ - The code contained within this block must remove all additional content/elements added by the widget. Also, any rows or content that is hidden must be restored.
+ - If additional rows are added to the table, make sure to include the class name within the
selectorRemove
option.
+ - This block was added in v2.4 and is optional.
+
+
+
+
+
+
addWidget Template
+
+
// addWidget Template
+// *******************
+// parameters:
+// table = table object (DOM)
+// config = config object (from table.config)
+// widgetOptions = all widget options (from table.config.widgetOptions)
+$.tablesorter.addWidget({
+ id: 'myWidget',
+ // widget options (added v2.8) - added to table.config.widgetOptions
+ options: {
+ myWidget_option1 : 'setting1',
+ myWidget_option2 : 'setting2'
+ },
+ // The init function (added v2.0.28) is called only after tablesorter has
+ // initialized, but before initial sort & before any of the widgets are applied.
+ init: function(table, thisWidget, config, widgetOptions){
+ // widget initialization code - this is only *RUN ONCE*
+ // but in this example, only the format function is called to from here
+ // to keep the widget backwards compatible with the original tablesorter
+ thisWidget.format(table, config, widgetOptions, true);
+ },
+ format: function(table, config, widgetOptions, initFlag) {
+ // widget code to apply to the table *AFTER EACH SORT*
+ // the initFlag is true when this format is called from the init
+ // function above otherwise initFlag is undefined
+ // * see the saveSort widget for a full example *
+ },
+ remove: function(table, config, widgetOptions){
+ // do what ever needs to be done to remove stuff added by your widget
+ // unbind events, restore hidden content, etc.
+ }
+});
+
+
Demo
-
- Name |
- Major |
- Sex |
- English |
- Japanese |
- Calculus |
- Geometry |
-
+ Name | Major | Sex | English | Japanese | Calculus | Geometry |
-
- Name |
- Major |
- Sex |
- English |
- Japanese |
- Calculus |
- Geometry |
-
+ Name | Major | Sex | English | Japanese | Calculus | Geometry |
-
- Student01 |
- Languages |
- male |
- 80 |
- 70 |
- 75 |
- 80 |
-
-
- Student02 |
- Mathematics |
- male |
- 90 |
- 88 |
- 100 |
- 90 |
-
-
- Student03 |
- Languages |
- female |
- 85 |
- 95 |
- 80 |
- 85 |
-
-
- Student04 |
- Languages |
- male |
- 60 |
- 55 |
- 100 |
- 100 |
-
-
- Student05 |
- Languages |
- female |
- 68 |
- 80 |
- 95 |
- 80 |
-
-
- Student06 |
- Mathematics |
- male |
- 100 |
- 99 |
- 100 |
- 90 |
-
-
- Student07 |
- Mathematics |
- male |
- 85 |
- 68 |
- 90 |
- 90 |
-
-
- Student08 |
- Languages |
- male |
- 100 |
- 90 |
- 90 |
- 85 |
-
-
- Student09 |
- Mathematics |
- male |
- 80 |
- 50 |
- 65 |
- 75 |
-
-
- Student10 |
- Languages |
- male |
- 85 |
- 100 |
- 100 |
- 90 |
-
-
- Student11 |
- Languages |
- male |
- 86 |
- 85 |
- 100 |
- 100 |
-
-
- Student12 |
- Mathematics |
- female |
- 100 |
- 75 |
- 70 |
- 85 |
-
-
- Student13 |
- Languages |
- female |
- 100 |
- 80 |
- 100 |
- 90 |
-
-
- Student14 |
- Languages |
- female |
- 50 |
- 45 |
- 55 |
- 90 |
-
-
- Student15 |
- Languages |
- male |
- 95 |
- 35 |
- 100 |
- 90 |
-
-
- Student16 |
- Languages |
- female |
- 100 |
- 50 |
- 30 |
- 70 |
-
-
- Student17 |
- Languages |
- female |
- 80 |
- 100 |
- 55 |
- 65 |
-
-
- Student18 |
- Mathematics |
- male |
- 30 |
- 49 |
- 55 |
- 75 |
-
-
- Student19 |
- Languages |
- male |
- 68 |
- 90 |
- 88 |
- 70 |
-
-
- Student20 |
- Mathematics |
- male |
- 40 |
- 45 |
- 40 |
- 80 |
-
-
- Student21 |
- Languages |
- male |
- 50 |
- 45 |
- 100 |
- 100 |
-
-
- Student22 |
- Mathematics |
- male |
- 100 |
- 99 |
- 100 |
- 90 |
-
-
- Student23 |
- Languages |
- female |
- 85 |
- 80 |
- 80 |
- 80 |
-
-
- Student24 |
- Languages |
- female |
- 100 |
- 91 |
- 13 |
- 82 |
-
+ Student12 | Mathematics | female | 100 | 75 | 70 | 85 |
+ Student13 | Languages | female | 100 | 80 | 100 | 90 |
+ Student14 | Languages | female | 50 | 45 | 55 | 90 |
+ Student15 | Languages | male | 95 | 35 | 100 | 90 |
+ Student16 | Languages | female | 100 | 50 | 30 | 70 |
+ Student17 | Languages | female | 80 | 100 | 55 | 65 |
+ Student18 | Mathematics | male | 30 | 49 | 55 | 75 |
+ Student19 | Languages | male | 68 | 90 | 88 | 70 |
+ Student20 | Mathematics | male | 40 | 45 | 40 | 80 |
+ Student21 | Languages | male | 50 | 45 | 100 | 100 |
+ Student22 | Mathematics | male | 100 | 99 | 100 | 90 |
+ Student23 | Languages | female | 85 | 80 | 80 | 80 |
+ Student01 | Languages | male | 80 | 70 | 75 | 80 |
+ Student02 | Mathematics | male | 90 | 88 | 100 | 90 |
+ Student03 | Languages | female | 85 | 95 | 80 | 85 |
+ Student04 | Languages | male | 60 | 55 | 100 | 100 |
+ Student05 | Languages | female | 68 | 80 | 95 | 80 |
+ Student06 | Mathematics | male | 100 | 99 | 100 | 90 |
+ Student07 | Mathematics | male | 85 | 68 | 90 | 90 |
+ Student08 | Languages | male | 100 | 90 | 90 | 85 |
+ Student09 | Mathematics | male | 80 | 50 | 65 | 75 |
+ Student10 | Languages | male | 85 | 100 | 100 | 90 |
+ Student11 | Languages | male | 86 | 85 | 100 | 100 |
+ Student24 | Languages | female | 100 | 91 | 13 | 82 |
-
Javascript
-
Add Widget Template
-
-
// addWidget Template
-// *******************
-$.tablesorter.addWidget({
- id: 'myWidget',
- // The init function (added in v2.0.28) is called only after tablesorter has
- // initialized, but before initial sort & before any of the widgets are applied.
- init: function(table, allWidgets, thisWidget){
- // widget initialization code - this is only *RUN ONCE*
- // but in this example, only the format function is called to from here
- // to keep the widget 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 this format is called from the init
- // function above otherwise initFlag is undefined
- // * see the saveSort widget for a full example *
- }
-});
-
-
-
Repeat Headers Widget Code
+
Repeat Headers Widget
diff --git a/js/jquery.tablesorter.js b/js/jquery.tablesorter.js
index aaf7a717..18c24cef 100644
--- a/js/jquery.tablesorter.js
+++ b/js/jquery.tablesorter.js
@@ -1097,10 +1097,11 @@
for (i = 0; i < l; i++) {
w = ts.getWidgetById(ws[i]);
if ( w ) {
- if (init === true && w.hasOwnProperty('init')) {
- w.init(table, w, c, wo);
+ if (init) {
+ if (w.hasOwnProperty('options')) { $.extend( true, wo, w.options ); }
+ if (w.hasOwnProperty('init')) { w.init(table, w, c, wo); }
} else if (!init && w.hasOwnProperty('format')) {
- w.format(table, c, wo);
+ w.format(table, c, wo, false);
}
}
}
diff --git a/js/jquery.tablesorter.widgets.js b/js/jquery.tablesorter.widgets.js
index 2546b296..c70e18f5 100644
--- a/js/jquery.tablesorter.widgets.js
+++ b/js/jquery.tablesorter.widgets.js
@@ -977,14 +977,15 @@ $.tablesorter.resizableReset = function(table){
// **************************
$.tablesorter.addWidget({
id: 'saveSort',
- init: function(table, thisWidget){
+ init: function(table, thisWidget, c, wo){
// run widget format before all other widgets are applied to the table
- thisWidget.format(table, true);
+ thisWidget.format(table, c, wo, true);
},
- format: function(table, init){
+ format: function(table, c, wo, init){
+ // redefining c & wo for backwards compatibility
+ c = table.config;
+ wo = c.widgetOptions;
var sl, time, $t = $(table),
- c = table.config,
- wo = c.widgetOptions,
ss = wo.saveSort !== false, // make saveSort active/inactive; default to true
sortList = { "sortList" : c.sortList };
if (c.debug){