| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<title>jQuery tablesorter 2.0 - Writing custom widgets</title> |
| 6 |
|
| 7 |
|
| 8 |
<script src="js/jquery-latest.min.js"></script> |
| 9 |
|
| 10 |
|
| 11 |
<link rel="stylesheet" href="css/jq.css"> |
| 12 |
<link href="css/prettify.css" rel="stylesheet"> |
| 13 |
<script src="js/prettify.js"></script> |
| 14 |
<script src="js/docs.js"></script> |
| 15 |
|
| 16 |
|
| 17 |
<link rel="stylesheet" href="../css/theme.blue.css"> |
| 18 |
<script src="../js/jquery.tablesorter.js"></script> |
| 19 |
|
| 20 |
|
| 21 |
<script src="../addons/pager/jquery.tablesorter.pager.js"></script> |
| 22 |
|
| 23 |
<script id="js">$(function() { |
| 24 |
|
| 25 |
// add new widget called repeatHeaders |
| 26 |
// updated v2.18.0 (works in nested tables) |
| 27 |
// ***************************************** |
| 28 |
$.tablesorter.addWidget({ |
| 29 |
|
| 30 |
id: "repeatHeaders", |
| 31 |
priority: 10, |
| 32 |
options: { |
| 33 |
rowsToSkip : 4 |
| 34 |
}, |
| 35 |
// format is called on init and when a sorting has finished |
| 36 |
format: function(table, c, wo) { |
| 37 |
var h = '', i, $tr, l, skip; |
| 38 |
// cache and collect all TH headers |
| 39 |
if (!wo.repeatHeaders) { |
| 40 |
// "remove-me" class was added in case the table needs to be updated, the "remove-me" rows will be |
| 41 |
// removed prior to the update to prevent including the rows in the update - see "selectorRemove" option |
| 42 |
h = '<tr class="repeated-header remove-me">'; |
| 43 |
$.each(c.headerContent, function(i,t) { |
| 44 |
// table.config.headerContent stores the original table HTML (as text), but it is the HTML before |
| 45 |
// the headerTemplate option is applied to each header cell; and before the `onRender` callbacks are |
| 46 |
// executed |
| 47 |
h += '<th>' + t + '</th>'; |
| 48 |
}); |
| 49 |
wo.repeatHeaders = h + '</tr>'; |
| 50 |
} |
| 51 |
|
| 52 |
// number of rows to skip |
| 53 |
skip = wo && wo.rowsToSkip || 4; |
| 54 |
|
| 55 |
// remove appended headers by classname |
| 56 |
c.$tbodies.children("tr.repeated-header").remove(); |
| 57 |
// only find visible rows (may be filtered) |
| 58 |
$tr = c.$tbodies.children('tr:visible'); |
| 59 |
l = $tr.length; |
| 60 |
// loop all tr elements and insert a copy of the "headers" |
| 61 |
for (i = skip; i < l; i += skip) { |
| 62 |
// insert a copy of the table head every X rows |
| 63 |
$tr.eq(i).before(wo.repeatHeaders); |
| 64 |
} |
| 65 |
}, |
| 66 |
// this remove function is called when using the refreshWidgets method or when destroying the tablesorter plugin |
| 67 |
// this function only applies to tablesorter v2.4+ |
| 68 |
remove: function(table, c) { |
| 69 |
c.$tbodies.children("tr.repeated-header").remove(); |
| 70 |
} |
| 71 |
|
| 72 |
}); |
| 73 |
|
| 74 |
// call the tablesorter plugin and assign widgets with id "zebra" (Default widget in the core) and the newly created "repeatHeaders" |
| 75 |
$("table").tablesorter({ |
| 76 |
theme: 'blue', |
| 77 |
|
| 78 |
// apply both widgets |
| 79 |
widgets: ['zebra', 'repeatHeaders'], |
| 80 |
widgetOptions : { |
| 81 |
rowsToSkip : 4 |
| 82 |
} |
| 83 |
}); |
| 84 |
|
| 85 |
});</script> |
| 86 |
</head> |
| 87 |
<body> |
| 88 |
|
| 89 |
<div id="banner"> |
| 90 |
<h1>table<em>sorter</em></h1> |
| 91 |
<h2>Writing custom widgets</h2> |
| 92 |
<h3>Flexible client-side table sorting</h3> |
| 93 |
<a href="index.html">Back to documentation</a> |
| 94 |
</div> |
| 95 |
|
| 96 |
<div id="main"> |
| 97 |
|
| 98 |
<div class="tip"> |
| 99 |
Notes about the <code>addWidget</code> template: |
| 100 |
<ul> |
| 101 |
<li>The <code>id</code> block: |
| 102 |
<ul> |
| 103 |
<li>The widget id, or name, must be unique!</li> |
| 104 |
<li>The id, or name, can contain special characters and/or spaces.</li> |
| 105 |
<li>This setting is required.</li> |
| 106 |
</ul> |
| 107 |
</li> |
| 108 |
<li>The <code>priority</code> block (added <span class="version">v2.9</span>): |
| 109 |
<ul> |
| 110 |
<li>Set the widget priority using any number; think of it like setting the css z-index.</li> |
| 111 |
<li>This tells the plugin the order in which to run the widgets, lowest number priority first.</li> |
| 112 |
<li>The default widgets have priorities set in intervals of 10 (see the <a href="index.html#Widget-options"> widget priority table</a>), so to run your custom widget before a specific widget, set your widget priority to less than that number.</li> |
| 113 |
<li>This block is <strong>not supported in older versions</strong>!</li> |
| 114 |
<li>This setting is optional, but if no priority is specified, it defaults to <code>10</code>.</li> |
| 115 |
</ul> |
| 116 |
</li> |
| 117 |
<li>The <code>options</code> block (added <span class="version">v2.8</span>): |
| 118 |
<ul> |
| 119 |
<li>Include any widget options to be automatically be extended with any set widgetOptions (from <code>table.config.widgetOptions</code>).</li> |
| 120 |
<li>As of v2.8, no included widgets will be using this to maintain backwards compatibility with older versions. This changed when v2.9 was released.</li> |
| 121 |
<li>This block is <strong>not supported in older versions</strong>!</li> |
| 122 |
<li>This block is optional.</li> |
| 123 |
</ul> |
| 124 |
</li> |
| 125 |
<li>The <code>init</code> block (added v2.0.28): |
| 126 |
<ul> |
| 127 |
<li>This code is called only after tablesorter has initialized, but before initial sort and before <strong>any</strong> of the widgets are applied (via the <code>format</code> block); it is only run once.</li> |
| 128 |
<li>Since, v2.0.28, only the saveSort widget uses this block to ensure that the stored sort is applied before; but some of the newer widgets (post v2.9) are using this code block.</li> |
| 129 |
<li>This block is <strong>not supported in older versions</strong>.</li> |
| 130 |
<li>This block is optional.</li> |
| 131 |
</ul> |
| 132 |
</li> |
| 133 |
<li>The <code>format</code> block (modified v2.0.23): |
| 134 |
<ul> |
| 135 |
<li>This block is part of the original <code>addWidget</code> template and is required.</li> |
| 136 |
<li>In the original template, only the <code>table</code> parameter is provided. After v2.0.28, <code>config</code> and <code>widgetOptions</code> were provided as additional parameters (sorry the previous docs were incorrect).</li> |
| 137 |
<li>The <code>initFlag</code> is correctly set in v2.8+.</li> |
| 138 |
</ul> |
| 139 |
</li> |
| 140 |
<li>The <code>remove</code> block (added v2.4): |
| 141 |
<ul> |
| 142 |
<li>In <span class="version">v2.19.0</span> the <code>refreshing</code> parameter was added: |
| 143 |
<ul> |
| 144 |
<li>It is a parameter used to indicate that the <a href="index.html#refreshwidgets"><code>refreshWidgets</code></a> method was triggered.</li> |
| 145 |
<li>When widgets are refreshed, the <code>remove</code> method is called, then the widget <code>init</code> function is immediately called to reapply the widget.</li> |
| 146 |
<li>In this update, this parameter is used by the filter widget to retain filtered rows and prevent a "flash" of showing all rows, then returning to its previous state after applying the filter again.</li> |
| 147 |
</ul> |
| 148 |
</li> |
| 149 |
<li>This code is called when either the <a href="index.html#refreshwidgets"><code>refreshWidgets</code></a> or <a href="index.html#destroy"><code>destroy</code></a> methods are called.</li> |
| 150 |
<li>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.</li> |
| 151 |
<li>If additional rows are added to the table, make sure to include the class name within the <a href="index.html#selectorremove"><code>selectorRemove</code></a> option.</li> |
| 152 |
<li>This block was added in v2.4, and <strong>not supported in older versions</strong>.</li> |
| 153 |
<li>This block is optional.</li> |
| 154 |
</ul> |
| 155 |
</li> |
| 156 |
</ul> |
| 157 |
</div> |
| 158 |
|
| 159 |
<h3>addWidget Template</h3> |
| 160 |
<div> |
| 161 |
<pre class="prettyprint lang-javascript">// addWidget Template |
| 162 |
// ******************* |
| 163 |
// parameters: |
| 164 |
// table = table object (DOM) |
| 165 |
// config = config object (from table.config) |
| 166 |
// widgetOptions = all widget options (from table.config.widgetOptions) |
| 167 |
$.tablesorter.addWidget({ |
| 168 |
id: 'myWidget', |
| 169 |
// set the priority of the widget (optional) |
| 170 |
priority: 10, |
| 171 |
// widget options (added v2.8) - added to table.config.widgetOptions |
| 172 |
options: { |
| 173 |
myWidget_option1 : 'setting1', |
| 174 |
myWidget_option2 : 'setting2' |
| 175 |
}, |
| 176 |
// The init function (added v2.0.28) is called only after tablesorter has |
| 177 |
// initialized, but before initial sort & before any of the widgets are applied. |
| 178 |
init: function(table, thisWidget, config, widgetOptions) { |
| 179 |
// widget initialization code - this is only *RUN ONCE* |
| 180 |
// but in this example, only the format function is called to from here |
| 181 |
// to keep the widget backwards compatible with the original tablesorter |
| 182 |
thisWidget.format(table, config, widgetOptions, true); |
| 183 |
}, |
| 184 |
format: function(table, config, widgetOptions, initFlag) { |
| 185 |
// widget code to apply to the table *AFTER EACH SORT* |
| 186 |
// set the initFlag to true when this format is called from the init |
| 187 |
// function above otherwise initFlag is undefined |
| 188 |
// * see the saveSort widget for a full example * |
| 189 |
}, |
| 190 |
remove: function(table, config, widgetOptions, refreshing) { |
| 191 |
// do what ever needs to be done to remove stuff added by your widget |
| 192 |
// unbind events, restore hidden content, etc. |
| 193 |
// refreshing flag is true when the refreshWidgets method is triggered, meaning |
| 194 |
// the widget will be removed, then immediately reapplied |
| 195 |
} |
| 196 |
});</pre> |
| 197 |
</div> |
| 198 |
|
| 199 |
<h1>Demo</h1> |
| 200 |
|
| 201 |
<table class="tablesorter"> |
| 202 |
<thead> |
| 203 |
<tr><th>Name</th><th>Major</th><th>Sex</th><th>English</th><th>Japanese</th><th>Calculus</th><th>Geometry</th></tr> |
| 204 |
</thead> |
| 205 |
<tfoot> |
| 206 |
<tr><th>Name</th><th>Major</th><th>Sex</th><th>English</th><th>Japanese</th><th>Calculus</th><th>Geometry</th></tr> |
| 207 |
</tfoot> |
| 208 |
<tbody> |
| 209 |
<tr><td>Student12</td><td>Mathematics</td><td>female</td><td>100</td><td>75</td><td>70</td><td>85</td></tr> |
| 210 |
<tr><td>Student13</td><td>Languages</td><td>female</td><td>100</td><td>80</td><td>100</td><td>90</td></tr> |
| 211 |
<tr><td>Student14</td><td>Languages</td><td>female</td><td>50</td><td>45</td><td>55</td><td>90</td></tr> |
| 212 |
<tr><td>Student15</td><td>Languages</td><td>male</td><td>95</td><td>35</td><td>100</td><td>90</td></tr> |
| 213 |
<tr><td>Student16</td><td>Languages</td><td>female</td><td>100</td><td>50</td><td>30</td><td>70</td></tr> |
| 214 |
<tr><td>Student17</td><td>Languages</td><td>female</td><td>80</td><td>100</td><td>55</td><td>65</td></tr> |
| 215 |
<tr><td>Student18</td><td>Mathematics</td><td>male</td><td>30</td><td>49</td><td>55</td><td>75</td></tr> |
| 216 |
<tr><td>Student19</td><td>Languages</td><td>male</td><td>68</td><td>90</td><td>88</td><td>70</td></tr> |
| 217 |
<tr><td>Student20</td><td>Mathematics</td><td>male</td><td>40</td><td>45</td><td>40</td><td>80</td></tr> |
| 218 |
<tr><td>Student21</td><td>Languages</td><td>male</td><td>50</td><td>45</td><td>100</td><td>100</td></tr> |
| 219 |
<tr><td>Student22</td><td>Mathematics</td><td>male</td><td>100</td><td>99</td><td>100</td><td>90</td></tr> |
| 220 |
<tr><td>Student23</td><td>Languages</td><td>female</td><td>85</td><td>80</td><td>80</td><td>80</td></tr> |
| 221 |
<tr><td>Student01</td><td>Languages</td><td>male</td><td>80</td><td>70</td><td>75</td><td>80</td></tr> |
| 222 |
<tr><td>Student02</td><td>Mathematics</td><td>male</td><td>90</td><td>88</td><td>100</td><td>90</td></tr> |
| 223 |
<tr><td>Student03</td><td>Languages</td><td>female</td><td>85</td><td>95</td><td>80</td><td>85</td></tr> |
| 224 |
<tr><td>Student04</td><td>Languages</td><td>male</td><td>60</td><td>55</td><td>100</td><td>100</td></tr> |
| 225 |
<tr><td>Student05</td><td>Languages</td><td>female</td><td>68</td><td>80</td><td>95</td><td>80</td></tr> |
| 226 |
<tr><td>Student06</td><td>Mathematics</td><td>male</td><td>100</td><td>99</td><td>100</td><td>90</td></tr> |
| 227 |
<tr><td>Student07</td><td>Mathematics</td><td>male</td><td>85</td><td>68</td><td>90</td><td>90</td></tr> |
| 228 |
<tr><td>Student08</td><td>Languages</td><td>male</td><td>100</td><td>90</td><td>90</td><td>85</td></tr> |
| 229 |
<tr><td>Student09</td><td>Mathematics</td><td>male</td><td>80</td><td>50</td><td>65</td><td>75</td></tr> |
| 230 |
<tr><td>Student10</td><td>Languages</td><td>male</td><td>85</td><td>100</td><td>100</td><td>90</td></tr> |
| 231 |
<tr><td>Student11</td><td>Languages</td><td>male</td><td>86</td><td>85</td><td>100</td><td>100</td></tr> |
| 232 |
<tr><td>Student24</td><td>Languages</td><td>female</td><td>100</td><td>91</td><td>13</td><td>82</td></tr> |
| 233 |
</tbody> |
| 234 |
</table> |
| 235 |
|
| 236 |
<h1>Javascript</h1> |
| 237 |
<h3>Repeat Headers Widget</h3> |
| 238 |
|
| 239 |
<div id="javascript"> |
| 240 |
<pre class="prettyprint lang-javascript"></pre> |
| 241 |
</div> |
| 242 |
|
| 243 |
<div class="next-up"> |
| 244 |
<hr /> |
| 245 |
Next up: <a href="example-pager.html">Pager plugin ››</a> |
| 246 |
</div> |
| 247 |
|
| 248 |
</div> |
| 249 |
|
| 250 |
</body> |
| 251 |
</html> |