Debian Codegraph @

docs/example-widget-build-table.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="utf-8" />
5 <title>jQuery tablesorter 2.0 - Build Table Widget</title>
6 <!-- jQuery -->
7 <script src="js/jquery-latest.min.js"></script>
8
9 <!-- Demo stuff -->
10 <link class="ui-theme" rel="stylesheet" href="css/jquery-ui.min.css">
11 <script src="js/jquery-ui.min.js"></script>
12 <link rel="stylesheet" href="css/jq.css">
13 <link href="css/prettify.css" rel="stylesheet">
14 <script src="js/prettify.js"></script>
15 <script src="js/docs.js"></script>
16 <script src="js/demo-build-table.js"></script>
17 <style>
18 .csv { display: none; }
19 .tablesorter-blue td.info { background: #eee; }
20 </style>
21
22 <!-- Tablesorter: required -->
23 <link rel="stylesheet" href="../css/theme.blue.css">
24 <script src="../js/jquery.tablesorter.js"></script>
25
26 <!-- build table widget -->
27 <script type="text/javascript" src="../js/widgets/widget-build-table.js"></script>
28
29 </head>
30 <body>
31 <div id="banner">
32 <h1>table<em>sorter</em></h1>
33 <h2>Build Table Widget</h2>
34 <h3>Flexible client-side table sorting</h3>
35 <a href="index.html">Back to documentation</a>
36 </div>
37 <div id="main">
38 <br>
39 <div class="accordion">
40
41 <h3><a href="#">Notes</a></h3>
42 <div>
43 <ul>
44 <li>To bind events to the newly created table, see the <strong>Adding event bindings</strong> section below.</li>
45 <li>Build a table starting with an assortment of data types ( array, text (CSV, HTML) or object (json) ).</li>
46 <li>This widget isn't really a widget because it is run and does it's processing before tablesorter has initialized; but the options for it are contained within the tablesorter <code>widgetOptions</code>.</li>
47 <li>Using the core build options:
48 <ul>
49 <li>The <code>build_type</code> tells the widget which type of data processing to use on the data.</li>
50 <li>The <code>build_source</code> points the widget to the data source.</li>
51 <li>Once the data is obtained from the <code>build_source</code>, you can do whatever processing that needs to be done on it using the <code>build_processing</code> option.
52 <ul>
53 <li>For example, if the table data is within a larger JSON, you can just return that portion of the JSON to the widget: <code>build_processing : function(data, wo) { return data.all_info_tables.table_of_contents; }</code></li>
54 <li>Anoter example is to use the processing option to split a string into an array (see the "Array (from string w/processing)" section below)</li>
55 </ul>
56 </li>
57 </ul>
58 </li>
59 <li>An extra option named <code>build_numbers</code> has been included:
60 <ul>
61 <li>This option only applies to array and csv source table builds.</li>
62 <li>Set the <code>build_numbers.addColumn</code> option to <code>true</code> (or a string with the column name <code>"#"</code>) to add a row number column on the far left of the table.</li>
63 <li>Set the <code>build_numbers.sortable</code> option to <code>true</code> to make the added number column sortable. This option only applies if the <code>addColumn</code> option is <code>true</code>.</li>
64 </ul>
65 </li>
66 </ul>
67 </div>
68
69 <h3><a href="#">Adding event bindings</a></h3>
70 <div>
71 <p>Internally, all triggered events use jQuery's <a href="http://api.jquery.com/triggerHandler/">triggerHandler</a> method to get around the issue of events triggered on a nested table bubbling up to the parent table; e.g. we don't want a "sortEnd" event to trigger on a parent table when a nested table has completed sorting.</p>
72 <strong>A binding added to the wrapper will not execute, and you can not target a table element that has not yet been created.</strong>
73 <p>To add an event listener to a table created by the build widget, you will need to use the <code>initialized</code> callback:</p>
74 HTML (before)
75 <pre class="prettyprint lang-html">&lt;!-- empty wrapper before the table has been built --&gt;
76 &lt;div id="wrapper"&gt;&lt;/div&gt;
77 </pre>
78 Javascript
79 <pre class="prettyprint lang-js">$(function() {
80 $('#wrapper').tablesorter({
81 theme: 'blue',
82 widgets: ['zebra'],
83 data : dataObject,
84 initialized: function(table) {
85 $(table).on('sortStart', function() {
86 alert('ok');
87 });
88 }
89 });
90 });</pre>
91 HTML (after)
92 <pre class="prettyprint lang-html">&lt;div id="wrapper"&gt;
93 &lt;table class="tablesorter-blue"&gt;
94 &lt;!-- ... --&gt;
95 &lt;/table&gt;
96 &lt;/div&gt;
97 </pre>
98 <p></p>
99 </div>
100
101 <h3><a href="#">Options</a></h3>
102 <div>
103 <table class="tablesorter-blue">
104 <colgroup>
105 <col style="width:10%">
106 <col style="width:10%">
107 <col style="width:15%">
108 <col style="width:65%">
109 </colgroup>
110 <thead>
111 <tr>
112 <th>widgetOption</th>
113 <th>Type</th>
114 <th>Default</th>
115 <th>Description</th>
116 </tr>
117 </thead>
118 <tbody>
119
120 <tr>
121 <td colspan="4" class="info">Core Options</td>
122 </tr>
123
124 <tr>
125 <td><a href="#" class="permalink"><code>build_type</code></a></td>
126 <td>String</td>
127 <td><code>""</code></td>
128 <td>
129 Indicate the data result type that needs to be processed
130 <div class="collapsible"><br>
131 Options include: <code>"array"</code>, <code>"csv"</code>, <code>"object"</code>, <code>"json"</code> or <code>"html"</code>.<br>
132 Default is <code>""</code> (an empty string).
133 </div>
134 </td>
135 </tr>
136 <tr>
137 <td><a href="#" class="permalink"><code>build_source</code></a></td>
138 <td>String</td>
139 <td><code>""</code></td>
140 <td>
141 Contains the data stored as an array, object, jQuery object or <a href="http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings">ajax settings</a> used to obtain ajax data (include any desired ajax options).
142 <div class="collapsible"><br>
143 Options include: an array, object, jQuery Object (<code>$('.csv')</code>) or ajax settings( <code>{ url: 'mysite.com/data.json', dataType: 'json' }</code> ).
144 </div>
145 </td>
146 </tr>
147 <tr>
148 <td><a href="#" class="permalink"><code>build_processing</code></a></td>
149 <td>Function</td>
150 <td><code>null</code></td>
151 <td>
152 Add a function that returns a useable build_type. (e.g. string to array)
153 <div class="collapsible"><br>
154 The function receives two parameters: <code>data</code> which contains the obtained data and <code>wo</code> which is the widget options (<code>table.config.widgetOptions</code>).<br>
155 Example: <code>build_processing: function(data, wo) { return data.split(';'); }</code>
156 </div>
157 </td>
158 </tr>
159 <tr>
160 <td><span class="permalink"><code>build_complete</code></span></td>
161 <td>String</td>
162 <td><code>"tablesorter-build-complete"</code></td>
163 <td>Contains the name of the event to trigger when a table build has completed.</td>
164 </tr>
165
166 <tr>
167 <td colspan="4" class="info">CSV & Array Options</td>
168 </tr>
169
170 <tr>
171 <td><span class="permalink"><code>build_headers.rows</code></span></td>
172 <td>Numeric</td>
173 <td><code>1</code></td>
174 <td>Number of header rows contained within the csv</td>
175 </tr>
176 <tr>
177 <td><span class="permalink"><code>build_headers.classes</code></span></td>
178 <td>Array</td>
179 <td><code>[]</code></td>
180 <td>Array of header class names to add while building the table header.</td>
181 </tr>
182 <tr>
183 <td><a href="#" class="permalink"><code>build_headers.text</code></a></td>
184 <td>Array</td>
185 <td><code>[]</code></td>
186 <td>Array of header cell names to add while building the table header.
187 <div class="collapsible"><br>
188 Any value contained within this option will override any header text obtained from the CSV data.
189 </div>
190 </td>
191 </tr>
192 <tr>
193 <td><span class="permalink"><code>build_headers.widths</code></span></td>
194 <td>Array</td>
195 <td><code>[]</code></td>
196 <td>Array of header cell widths which are added to a <code>&lt;col&gt;</code> within a <code>&lt;colgroup&gt;</code>.</td>
197 </tr>
198 <tr>
199 <td><span class="permalink"><code>build_footers.rows</code></span></td>
200 <td>Numeric</td>
201 <td><code>1</code></td>
202 <td>Number of footer rows contained within the csv</td>
203 </tr>
204 <tr>
205 <td><span class="permalink"><code>build_footers.classes</code></span></td>
206 <td>Array</td>
207 <td><code>[]</code></td>
208 <td>Array of footer class names to add while building the table footer.</td>
209 </tr>
210 <tr>
211 <td><a href="#" class="permalink"><code>build_footers.text</code></a></td>
212 <td>Array</td>
213 <td><code>[]</code></td>
214 <td>Array of footer cell names to add while building the table footer.
215 <div class="collapsible"><br>
216 Any value contained within this option will override any footer text obtained from the CSV data.
217 </div>
218 </td>
219 </tr>
220
221 <tr>
222 <td><a href="#" class="permalink"><code>build_numbers.addColumn</code></a></td>
223 <td>Boolean</td>
224 <td><code>false</code></td>
225 <td>Include a row numbering column.
226 <div class="collapsible"><br>
227 Only works with csv or array data.
228 </div>
229 </td>
230 </tr>
231 <tr>
232 <td><a href="#" class="permalink"><code>build_numbers.sortable</code></a></td>
233 <td>Boolean</td>
234 <td><code>false</code></td>
235 <td>Make the included row number column sortable.
236 <div class="collapsible"><br>
237 Only works when the <code>build_numbers.addColumn</code> is <code>true</code> and when csv or array data is used.
238 </div>
239 </td>
240 </tr>
241
242 <tr>
243 <td colspan="4" class="info">CSV Only Options</td>
244 </tr>
245
246 <tr>
247 <td><span class="permalink"><code>build_csvStartLine</code></span></td>
248 <td>Numeric</td>
249 <td><code>0</code></td>
250 <td>The line within the csv data to start building the table</td>
251 </tr>
252 <tr>
253 <td><span class="permalink"><code>build_csvSeparator</code></span></td>
254 <td>String</td>
255 <td><code>","</code> (comma)</td>
256 <td>The separator used within the text file (comma or tab)</td>
257 </tr>
258
259 <tr>
260 <td colspan="4" class="info">Object Only Options</td>
261 </tr>
262
263 <tr>
264 <td><span class="permalink"><code>build_objectRowKey</code></span></td>
265 <td>String</td>
266 <td><code>"rows"</code></td>
267 <td>Object key which contains row data</td>
268 </tr>
269 <tr>
270 <td><span class="permalink"><code>build_objectCellKey</code></span></td>
271 <td>String</td>
272 <td><code>"cells"</code></td>
273 <td>Object key which contains the table cells, within the row data</td>
274 </tr>
275 <tr>
276 <td><span class="permalink"><code>build_objectHeaderKey</code></span></td>
277 <td>String</td>
278 <td><code>"headers"</code></td>
279 <td>Object key which contains the table headers data</td>
280 </tr>
281 <tr>
282 <td><span class="permalink"><code>build_objectFooterKey</code></span></td>
283 <td>String</td>
284 <td><code>"footers"</code></td>
285 <td>Object key which contains the table footers data</td>
286 </tr>
287
288 </tbody>
289 </table>
290
291 </div> <!-- end options -->
292
293 <h3><a href="#">Setup - Common (document head)</a></h3>
294 <div>
295 Add the following required scripts and css files into the document <code>&lt;head&gt;</code>
296 <pre class="prettyprint lang-html">&lt;!-- jQuery --&gt;
297 &lt;script src=&quot;js/jquery-latest.min.js&quot;&gt;&lt;/script&gt;
298
299 &lt;!-- Tablesorter: required --&gt;
300 &lt;link rel=&quot;stylesheet&quot; href=&quot;../css/theme.blue.css&quot;&gt; &lt;!-- choose any theme --&gt;
301 &lt;script src=&quot;../js/jquery.tablesorter.js&quot;&gt;&lt;/script&gt;
302
303 &lt;!-- build table widget --&gt;
304 &lt;script type=&quot;text/javascript&quot; src=&quot;../js/widgets/widget-build-table.js&quot;&gt;&lt;/script&gt;</pre>
305 </div>
306
307 <h3><a href="#">Setup - Array (javascript variable)</a></h3>
308 <div>
309
310 <h3>Notes</h3>
311 <ul>
312 <li>The array is set up such that it is an array of arrays. The outer array contains each row array, and within each row array is the cell text.</li>
313 <li>Using an array is limiting, so addtional options have been added:
314 <ul>
315 <li>(<code>build_headers</code> &amp; <code>build_footers</code>) have been added allowing customizing the headers and/or footers.</li>
316 <li>The <code>rows</code> option sets the number of headers rows to include from the data source. <strong>Zero is not an option</strong> since tablesorter requires a thead in place before it will initialize.</li>
317 <li>The <code>classes</code> array sets each header cell (<code>th</code>) class name.</li>
318 <li>The <code>text</code> array within these options will override any text obtained from the data source.</li>
319 <li>The <code>widths</code> array, only in the <code>build_headers</code> option, sets column widths by building <code>&lt;col&gt;</code> elements within a <code>&lt;colgroup</code>.</li>
320 <li>Adding class names to the tbody rows or cells isn't easily accomplished, but you can bind to the build complete event (<code>'tablesorter-build-complete'</code>) and add them yourself.</li>
321 </ul>
322 </li>
323 </ul>
324
325 <h3>HTML</h3>
326 <pre class="prettyprint lang-html">&lt;div id=&quot;array2Table&quot;&gt;&lt;/div&gt;</pre>
327 <h3>Javascript</h3>
328 <pre class="prettyprint lang-js">$(function() {
329 var arry = [
330 [ 'ID', 'Name', 'Age', 'Date' ], // header
331 [ 'A42b', 'Parker', 28, 'Jul 6, 2006 8:14 AM' ], // row 1
332 [ 'A255', 'Hood', 33, 'Dec 10, 2002 5:14 AM' ], // row 2
333 [ 'A33', 'Kent', 18, 'Jan 12, 2003 11:14 AM' ], // row 3
334 [ 'A1', 'Franklin', 45, 'Jan 18, 2001 9:12 AM' ], // row 4
335 [ 'A102', 'Evans', 22, 'Jan 18, 2007 9:12 AM' ], // row 5
336 [ 'A42a', 'Everet', 22, 'Jan 18, 2007 9:12 AM' ], // row 6
337 [ 'ID', 'Name', 'Age', 'Date' ] // footer
338 ];
339
340 $('#array2Table').tablesorter({
341 theme: 'blue',
342 widgets: ['zebra'],
343 widgetOptions : {
344 // build_type : 'array', // can sometimes be detected if undefined
345 build_source : arry,
346 build_headers : {
347 rows : 1, // Number of header rows from the csv
348 classes : [], // Header classes to apply to cells
349 text : [], // Header cell text
350 widths : [ '15%', '30%', '15%', '40%' ] // set header cell widths
351 },
352 build_footers : {
353 rows : 1, // Number of header rows from the csv
354 classes : [], // Footer classes to apply to cells
355 text : [ 'ID (a###)', 'Last Name', 'Age (joined)', 'Date (joined)' ] // Footer cell text
356 }
357 }
358 });
359 });</pre>
360 <h3>Result</h3>
361 <div id="array2Table" class="container"></div>
362 </div>
363
364 <h3><a href="#">Setup - Array (from string w/processing)</a></h3>
365 <div>
366
367 <h3>Notes</h3>
368 <ul>
369 <li>We start out with a string and split it into a useable array of arrays.</li>
370 <li>The settings are similair to the Array (javascript variable) code above with the exception of using the <code>build_processing</code> function to create the array.
371 <ul>
372 <li>The string is set up to separate each row by a semi-colon, then each cell by a comma.</li>
373 <li>The first split is needed to create an array of rows <code>.split(';')</code></li>
374 <li>The second split can be accomplished using <code>.split(',')</code> on each row of the array, but instead we use the build table widget function <code>$.tablesorter.buildTable.splitCSV(cells, ',');</code> to ensure that the split doesn't occur if the separator is within double quotes (note the footer text).</li>
375 <li>Then just return this newly built array back to the build table widget (see the full code below).</li>
376 </ul>
377 </li>
378 </ul>
379
380 <h3>HTML</h3>
381 <pre class="prettyprint lang-html">&lt;div id=&quot;string2Table&quot;&gt;&lt;/div&gt;</pre>
382 <h3>Javascript</h3>
383 <pre class="prettyprint lang-js">$(function() {
384 $('#string2Table').tablesorter({
385 theme: 'blue',
386 widgets: ['zebra'],
387 widgetOptions: {
388 build_type : 'array',
389 build_source : 'header 1,header 2,header 3;r1c1,r1c2,r1c3;r2c1,r2c2,r2c3;r3c1,r3c2,r3c3;"footer, 1","footer, 2","footer, 3"',
390 build_processing : function(data, wo) {
391 var rows = data.split(';');
392 return $.each(rows, function(i,cells) {
393 // similar to using rows[i] = cells.split(',') but the splitCSV script
394 // doesn't split the cell if the separator (comma) is within double quotes
395 rows[i] = $.tablesorter.buildTable.splitCSV(cells, ',');
396 });
397 }
398 }
399 });
400 });</pre>
401 <h3>Result</h3>
402 <div id="string2Table" class="container"></div>
403 </div>
404
405 <h3><a href="#">Setup - CSV (txt within DOM element)</a></h3>
406 <div>
407
408 <h3>Notes</h3>
409 <ul>
410 <li>A jQuery object targeting the CSV text can be passed to the <code>build_source</code> option.</li>
411 <li>The widget detects that a jQuery object was passed to it, and grabs the HTML (not text) of the element.
412 <ul>
413 <li>Make sure not include HTML tags, or at least use the <code>build_processing</code> function to reformat the data.</li>
414 <li>The row of csv data is trimmed of extra tabs and spaces (only from the beginning &amp; end of each row).</li>
415 <li>Building a table from CSV or array use the same script, so the same addtional options are available:
416 <ul>
417 <li>(<code>build_headers</code> &amp; <code>build_footers</code>) have been added allowing customizing the headers and/or footers.</li>
418 <li>The <code>rows</code> option sets the number of headers rows to include from the data source. <strong>Zero is not an option</strong> since tablesorter requires a thead in place before it will initialize.</li>
419 <li>The <code>classes</code> array sets each header cell (<code>th</code>) class name.</li>
420 <li>The <code>text</code> array within these options will override any text obtained from the data source.</li>
421 <li>The <code>widths</code> array, only in the <code>build_headers</code> option, sets column widths by building <code>&lt;col&gt;</code> elements within a <code>&lt;colgroup</code>.</li>
422 <li>Adding class names to the tbody rows or cells isn't easily accomplished, but you can bind to the build complete event (<code>'tablesorter-build-complete'</code>) and add them yourself.</li>
423 </ul>
424 </li>
425 <li>CSV data has two additional options used during data processing:
426 <ul>
427 <li><code>build_csvStartLine</code> (default is <code>0</code>) tells the csv build script which line in the csv data to start using to build the table. This is added in case comments or other data is above the table data.</li>
428 <li><code>build_csvSeparator</code> (default is <code>","</code>)
429 <ul>
430 <li>This sets the separator used within the csv data to separate each cell.</li>
431 <li>The <a href="https://code.google.com/p/jquerycsvtotable/">original jquerycsvtotable plugin</a> shows an <a href="http://honestbleeps.com/csvtotable/demo.html#TSVSource">example to process tab separated values (TSV)</a> by setting this option to <code>"\t"</code>.</li>
432 <li>Rows are determined by placing the data on a new line.</li>
433 </ul>
434 </li>
435 </ul>
436 </li>
437 <li>In this demo, the <code>build_numbers</code> sub-options <code>addColumn</code> and <code>sortable</code> are set to add a numeric column on the left side.
438 <ul>
439 <li>The <code>addColumn</code> option contains the name of the numeric column to be added to the header. Otherwise, set this option to <code>false</code> to not build this numeric column.</li>
440 <li>The <code>sortable</code> option makes that numeric column sortable.</li>
441 </ul>
442 </li>
443 </ul>
444 </li>
445 </ul>
446
447 <h3>HTML</h3>
448 <pre class="prettyprint lang-html">&lt;!--
449 Note: if the csv data contains commas ("10,000 days") wrap it in quotes
450 --&gt;
451 &lt;div class="csv" style="display:none;"&gt;
452 Album,Artist,Price (USD)
453 Lateralus,Tool,$13.00
454 Aenima,Tool,$12.00
455 "10,000 days",Tool,$14.00
456 Down In It,Nine Inch Nails,$3.00
457 Broken,Nine Inch Nails,$6.00
458 Muse,Black Holes and Revelations,$7.00
459 Anon,"fake album, with comma", $1.00
460 Album,Artist,Price (USD)
461 &lt;/div&gt;
462
463 &lt;div id=&quot;csv2Table&quot;&gt;&lt;/div&gt;</pre>
464 <h3>Javascript</h3>
465 <pre class="prettyprint lang-js">$(function() {
466 $('#csv2Table').tablesorter({
467 theme: 'blue',
468 widgets: ['zebra'],
469 widgetOptions: {
470 // *** build widget core ***
471 build_type : 'csv',
472 build_source : $('.csv'),
473 build_complete : 'tablesorter-build-complete', // triggered event when build completes
474
475 // *** CSV & array ***
476 build_headers : {
477 rows : 1, // Number of header rows from the csv
478 classes : [], // Header classes to apply to cells
479 text : [], // Header cell text
480 widths : ['3%', '27%', '50%', '20%'] // set header cell widths
481 },
482 build_footers : {
483 rows : 1, // Number of header rows from the csv
484 classes : [], // Footer classes to apply to cells
485 text : [] // Footer cell text
486 },
487 build_numbers : {
488 addColumn : "#", // include row numbering column?
489 sortable : true // make column sortable?
490 },
491
492 // *** CSV options ***
493 build_csvStartLine : 0, // line within the csv to start adding to table
494 build_csvSeparator : "," // csv separator
495 }
496 });
497 });</pre>
498 <h3>Result</h3>
499 <div id="csv2Table" class="container"></div>
500
501 </div>
502
503 <h3><a href="#">Setup - CSV (txt file via ajax)</a></h3>
504 <div>
505
506 <h3>Notes</h3>
507 <ul>
508 <li>If the csv data is obtained via ajax instead of from a DOM element, only the <code>build_source</code> option needs to change, everything else stays the same.
509 <ul>
510 <li>Set the <code>build_source</code> to contain any <a href="http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings">ajax settings</a> required to load the data.</li>
511 <li>In this case the csv file is contained within a text file (<code>build.txt</code>), so a <code>url</code> option is set to point to the file and the <code>dataType</code> option (set to <code>html</code>) is set so that ajax knows the type of file to access.</li>
512 <li>The remaining default settings were left out of the example below.</li>
513 </ul>
514 </li>
515 <li>Please note that browsers like Chrome will not allow you to access locally hosted (desktop) files. Test it with Firefox instead.</li>
516 </ul>
517
518 <h3>Build.txt file</h3>
519 <pre class="prettyprint lang-html">Album,Artist,Price ($)
520 Lateralus,Tool,$13.00
521 Aenima,Tool,$12.00
522 "10,000 days",Tool,$14.00
523 Down In It,Nine Inch Nails,$3.00
524 Broken,Nine Inch Nails,$6.00
525 Muse,Black Holes and Revelations,$7.00
526 Anon,"fake album, with comma", $1.00
527 Album,Artist,Price ($)</pre>
528 <h3>HTML</h3>
529 <pre class="prettyprint lang-html">&lt;div id=&quot;csv2Table2&quot;&gt;&lt;/div&gt;</pre>
530 <h3>Javascript</h3>
531 <pre class="prettyprint lang-js">$(function() {
532 $('#csv2Table2').tablesorter({
533 theme: 'blue',
534 widgets: ['zebra'],
535 widgetOptions: {
536 // *** build widget core ***
537 build_type : 'csv',
538 build_source : { url: 'assets/csv.txt', dataType: 'text' },
539 build_headers : {
540 widths : ['30%', '50%', '20%'] // set header cell widths
541 }
542 }
543 });
544 });</pre>
545 <h3>Result</h3>
546 <div id="csv2Table2" class="container"></div>
547
548 </div>
549
550 <h3><a href="#">Setup - Object (javascript variable)</a></h3>
551 <div>
552 <h3>Notes</h3>
553 <div class="accordion">
554 <h3><a href="#">Definitions - General:</a></h3>
555 <div>
556 These are definitions to words as used within this documentation.
557 <ul>
558 <li>A <strong>key-value pair</strong> (or <strong>attribute</strong>): Within an object, the way to assign a value is by using a key-value pair (<code>"name" : "Fred"</code>; which is like saying <code>name = "Fred"</code> or <code>x = 1</code> outside of an object).</li>
559 <li>A <strong>block</strong> is essentially the "value" portion of a key-value pair; specifically when referring to the value of the header, footer, row and cell blocks.</li>
560 <li>An <strong>array</strong> is a list or group of values (i.e. <code>['x','y','z']</code>).</li>
561 <li>An <strong>array of arrays</strong> is an array that contains more arrays (i.e. <code>[ ['a','b','c'], ['d','e','f' ] ]</code>). This applies to the header, row and footer blocks.</li>
562 <li>Any build table widget object:
563 <ul>
564 <li>These objects are a grouping of key-value pairs used to define a tbody, row or cell.</li>
565 <li>These objects contains all of the attributes which are to be added to a table element while building the table.</li>
566 <li>The keys used in these attributes will look familiar - <code>class</code> (see row 7 below), <code>colspan</code> (see row 4 below), <code>data-</code>attributes (see row 7 cell 3 below), etc.</li>
567 <li>Depending on where an object is located (tbody, row or cell), it will have a special key word or words which are significant (see the Objects section below).</li>
568 </ul>
569 </li>
570 </ul>
571 </div>
572
573 <h3><a href="#">Blocks:</a></h3>
574 <div>
575 (row #), or (row # cell #) in these descriptions refer to the demo object code below
576 <ul>
577 <li><code>header</code> block:
578 <ul>
579 <li>This block will contain an array of rows. The rows can be defined as another array of cell text (row 1-3,5-6), or row objects (row 0, 4 &amp; 7).</li>
580 <li>The <code>header</code> object key name can be modified by changing the widget <code>build_objectHeaderKey</code> option.</li>
581 </ul>
582 </li>
583 <li><code>footers</code> block:
584 <ul>
585 <li>This block can contain all of the same data as the header block.</li>
586 <li>This block has one additional setting, it can also contain a very specific string: <code>"clone"</code>. When this setting is used, the footer will be created by making a clone of the header.</li>
587 <li>The <code>footers</code> object key name can be modified by changing the widget <code>build_objectFooterKey</code> option.</li>
588 </ul>
589 </li>
590 <li><code>rows</code> block:
591 <ul>
592 <li>This block will contain an array of rows. The rows can be defined as another array of cell text, or as a row object (same as the header &amp; footer blocks).</li>
593 <li>In addition, this block can contain a tbody object (tbody 2 &amp; tbody 3; see the tbody object section below for more details).</li>
594 <li>The <code>rows</code> object key name can be modified by changing the widget <code>build_objectRowKey</code> option.</li>
595 </ul>
596 </li>
597 <li><code>cells</code> block:
598 <ul>
599 <li>This block will contain an array of cells. The cells can be defined as cell text (string), or cell objects.</li>
600 <li>The <code>cells</code> object key name can be modified by changing the widget <code>build_objectCellKey</code> option.</li>
601 </ul>
602 </li>
603 </ul>
604 </div>
605
606 <h3><a href="#">Object Content</a></h3>
607 <div>
608 <ul>
609 <li>tbody object
610 <ul>
611 <li>The tbody object contains all of the attributes that are to be applied to a particular table tbody (<code>tbody</code>).</li>
612 <li>The tbody object <strong>requires</strong> a <code>newTbody</code> key set to <code>true</code>. It is the ONLY way the build widget can differeniate a row object from a "new" tbody object.</li>
613 <li>If the <code>newTbody</code> attribute is <code>true</code>, the build widget will place all remaining rows into a new tbody; or all rows, until it encounters another valid tbody object, into a new tbody.</li>
614 <li>If the <code>newTbody</code> attribute is <code>false</code>, the build widget will assume the object is for a row and add it as a row.</li>
615 <li>See "TBODY 2" and "TBODY 3" tbody objects in the code example below.</li>
616 </ul>
617 </li>
618 <li>row object
619 <ul>
620 <li>The row object contains all of the attributes that are to be applied to a particular table row (<code>tr</code>).</li>
621 <li>The row object requires a <code>cells</code> attribute.</li>
622 <li>The cells attribute will contain all table cell data for the cells within that table row.</li>
623 <li>This attribute can be changed by modifying the build table widget's <code>build_objectCellKey</code> (default is <code>"cells"</code>).</li>
624 <li>See row 4 and row 7 in the demo code below.</li>
625 </ul>
626 </li>
627 <li>row array
628 <ul>
629 <li>A row array can contain either the text for a table cell (row 0 cell 2 &amp; 4), or a cell object (row 0 cell 1,3,5,6) contains.</li>
630 <li>It can also contain all cell text (row 1-3,5-6) or all cell objects (almost row 7) for cells within that row.</li>
631 <li>This method does not allow adding any row (<code>tr</code>) attributes, (i.e. class or data-attributes).</li>
632 </ul>
633 </li>
634
635 <li>cell object
636 <ul>
637 <li>The cell object contains all of the attributes that are to be applied to a particular table cell (<code>th</code> (thead or tfoot) or <code>td</code> (tbody).</li>
638 <li>This object requires either a <code>text</code> (row 0 &amp; row 7) or <code>html</code> (row 4) attribute to add content into the cell.</li>
639 <li>Within the first header row only, if a <code>width</code> attribute is defined, it will be applied to a <code>&lt;col&gt;</code> element and placed within a <code>&lt;colgroup&gt;</code> before the header.</li>
640 <li>See row 0 (header), row 4 and row 7 (rows) in the demo code below.</li>
641 </ul>
642 </li>
643 <li>cell text
644 <ul>
645 <li>Within a row array or object, only the cell text can be included (row 0 cell 2,4; row 1-3,5-6; row 7 cell 5).</li>
646 <li>Within the header only, whenever cell content is added as a string, the widget will check the <code>widgetOptions.build_headers.classes</code> &amp; <code>wo.build_headers.widths</code> (first row only) and apply any values it finds for that column to those cells.</li>
647 </ul>
648 </li>
649
650 </ul>
651 </div>
652 </div>
653
654 <h3>HTML</h3>
655 <pre class="prettyprint lang-html">&lt;div id=&quot;object2Table&quot;&gt;&lt;/div&gt;</pre>
656 <h3>Javascript</h3>
657 <pre class="prettyprint lang-js">$(function() {
658 // object or JSON
659 var dataObject = {
660 headers : [
661 [
662 // each object/string is a cell
663 { text: 'First Name', class: 'fname', width: '10%' }, // row 0 cell 1
664 'Last Name', // row 0 cell 2
665 { text: 'Age', class: 'age', 'data-sorter' : false }, // row 0 cell 3
666 'Total', // row 0 cell 4
667 { text: 'Discount', class : 'sorter-false' }, // row 0 cell 5
668 { text: 'Date', class : 'date' } // row 0 cell 6
669 ]
670 ],
671 footers : 'clone', // clone headers or assign array like headers
672 rows : [
673 // TBODY 1
674 [ 'Peter', 'Parker', 28, '$9.99', '20%', 'Jul 6, 2006 8:14 AM' ], // row 1
675 [ 'John', 'Hood', 33, '$19.99', '25%', 'Dec 10, 2002 5:14 AM' ], // row 2
676 [ 'Clark', 'Kent', 18, '$15.89', '44%', 'Jan 12, 2003 11:14 AM' ], // row 3
677
678 // TBODY 2
679 { newTbody: true, class: 'tablesorter-infoOnly' },
680 { cells : [ { html: '<strong>Info Row</strong>', colSpan: 6 } ] }, // row 4
681
682 // TBODY 3
683 { newTbody: true },
684 [ 'Bruce', 'Evans', 22, '$13.19', '11%', 'Jan 18, 2007 9:12 AM' ], // row 5
685 [ 'Bruce', 'Almighty', 45, '$153.19', '44%', 'Jan 18, 2001 9:12 AM' ], // row 6
686
687 { class: 'specialRow', // row 7
688 cells: [
689 // each object/string is a cell
690 { text: 'Fred', class: 'fname' }, // row 7 cell 1
691 { text: 'Smith', class: 'lname' }, // row 7 cell 2
692 { text: 18, class: 'age', 'data-info': "fake ID!, he's only 16" }, // row 7 cell 3
693 { text: '$22.44', class: 'total' }, // row 7 cell 4
694 '8%', // row 7 cell 5
695 { text: 'Aug 20, 2012 10:15 AM', class: 'date' } // row 7 cell 6
696 ],
697 'data-info' : 'This row likes turtles'
698 }
699 ]
700 };
701
702 $('#object2Table').tablesorter({
703 theme: 'blue',
704 widgets: ['zebra'],
705 data : dataObject, // same as using build_source (build_source would override this)
706 widgetOptions : {
707 // *** build object options ***
708 build_objectRowKey : 'rows', // object key containing table rows
709 build_objectCellKey : 'cells', // object key containing table cells (within the rows object)
710 build_objectHeaderKey : 'headers', // object key containing table headers
711 build_objectFooterKey : 'footers' // object key containing table footers
712 }
713 });
714
715 });</pre>
716 <h3>Result</h3>
717 <div id="object2Table" class="container"></div>
718 </div>
719
720 <h3><a href="#">Setup - Object (json file via ajax)</a></h3>
721 <div>
722
723 <h3>Notes</h3>
724 <ul>
725 <li>The important difference between this demo and the javascript variable demo above is that this one retrieves a JSON file:
726 <ul>
727 <li>Set the <code>build_source</code> option to contain any <a href="">ajax settings</a> to load the json.</li>
728 <li>To load the json file, set the <code>url</code> option to point to the json file and the <code>dataType</code> set to <code>"json"</code>.</li>
729 <li>The remaining default settings were left out of the example below.</li>
730 </ul>
731 </li>
732 <li>Please see the "Object (javascript variable)" section above for more details on how to set up the JSON for this demo.</li>
733 <li>Note that JSON formatting is very specific - only double quotes (<code>"</code>) can be used to wrap attributes, all keys must be wrapped in quotes, no comments, etc.</li>
734 <li>Always verify that the JSON is valid (use <a href="http://jsonlint.com/">JSONLint</a>) and realize that browsers like Chrome will not allow you to access locally hosted (desktop) JSON files. Test it with Firefox instead.</li>
735 </ul>
736
737 <h3>Javascript</h3>
738 <pre class="prettyprint lang-js">$(function() {
739 $('#object2Table2').tablesorter({
740 theme: 'blue',
741 widgets: ['zebra'],
742 widgetOptions: {
743 build_type : 'json',
744 build_source : { url: 'assets/build.json', dataType: 'json' }
745 }
746 });
747 });</pre>
748
749 <h3>Build.json file</h3>
750 <pre class="prettyprint lang-js">{
751 "headers": [
752 [
753 { "text": "First Name", "class": "fname", "width": "20%" },
754 "Last Name",
755 { "text": "Age", "class": "age", "data-sorter": false },
756 "Total",
757 { "text": "Discount", "class": "sorter-false" },
758 { "text": "Date", "class": "date", "width": "20%" }
759 ]
760 ],
761 "footers": "clone",
762 "rows": [
763 [ "Peter", "Parker", 28, "$9.99", "20%", "Jul 6, 2006 8:14 AM" ],
764 [ "John", "Hood", 33, "$19.99", "25%", "Dec 10, 2002 5:14 AM" ],
765 [ "Clark", "Kent", 18, "$15.89", "44%", "Jan 12, 2003 11:14 AM" ],
766
767 { "newTbody": true, "class": "tablesorter-infoOnly" },
768 { "cells": [ { "html": "<strong>JSON Info Row</strong>", "colspan": 6 } ] },
769
770 { "newTbody": true },
771 [ "Bruce", "Evans", 22, "$13.19", "11%", "Jan 18, 2007 9:12 AM" ],
772 [ "Brice", "Almighty", 45, "$153.19", "44%", "Jan 18, 2001 9:12 AM" ],
773
774 { "class": "specialRow",
775 "cells": [
776 { "text": "Fred", "class": "fname" },
777 { "text": "Smith", "class": "lname" },
778 { "text": 18, "class": "age", "data-info": "fake ID!, he's only 16" },
779 { "text": "$22.44", "class": "total" },
780 "8%",
781 { "text": "Aug 20, 2012 10:15 AM", "class": "date" }
782 ],
783 "data-info": "This row likes turtles"
784 }
785 ]
786 }</pre>
787 <h3>HTML (before)</h3>
788 <pre class="prettyprint lang-html">&lt;div id=&quot;object2Table2&quot;&gt;&lt;/div&gt;</pre>
789
790 <h3>HTML (after)</h3>
791 <pre class="prettyprint lang-html">&lt;div id=&quot;object2Table2&quot;&gt;
792 &lt;table class=&quot;tablesorter tablesorter-blue&quot; role=&quot;grid&quot;&gt;
793 &lt;colgroup&gt;
794 &lt;col style=&quot;width: 20%;&quot;&gt;
795 &lt;col&gt;
796 &lt;col&gt;
797 &lt;col&gt;
798 &lt;col&gt;
799 &lt;col style=&quot;width: 20%;&quot;&gt;
800 &lt;/colgroup&gt;
801 &lt;thead&gt;
802 &lt;tr&gt;
803 &lt;th class=&quot;fname&quot;&gt;First Name&lt;/th&gt;
804 &lt;th&gt;Last Name&lt;/th&gt;
805 &lt;th class=&quot;age&quot; data-sorter=&quot;false&quot;&gt;Age&lt;/th&gt;
806 &lt;th&gt;Total&lt;/th&gt;
807 &lt;th class=&quot;sorter-false&quot;&gt;Discount&lt;/th&gt;
808 &lt;th class=&quot;date&quot;&gt;Date&lt;/th&gt;
809 &lt;/tr&gt;
810 &lt;/thead&gt;
811 &lt;tbody&gt;
812 &lt;tr&gt;
813 &lt;td&gt;Peter&lt;/td&gt;
814 &lt;td&gt;Parker&lt;/td&gt;
815 &lt;td&gt;28&lt;/td&gt;
816 &lt;td&gt;$9.99&lt;/td&gt;
817 &lt;td&gt;20%&lt;/td&gt;
818 &lt;td&gt;Jul 6, 2006 8:14 AM&lt;/td&gt;
819 &lt;/tr&gt;
820 &lt;tr&gt;
821 &lt;td&gt;John&lt;/td&gt;
822 &lt;td&gt;Hood&lt;/td&gt;
823 &lt;td&gt;33&lt;/td&gt;
824 &lt;td&gt;$19.99&lt;/td&gt;
825 &lt;td&gt;25%&lt;/td&gt;
826 &lt;td&gt;Dec 10, 2002 5:14 AM&lt;/td&gt;
827 &lt;/tr&gt;
828 &lt;tr&gt;
829 &lt;td&gt;Clark&lt;/td&gt;
830 &lt;td&gt;Kent&lt;/td&gt;
831 &lt;td&gt;18&lt;/td&gt;
832 &lt;td&gt;$15.89&lt;/td&gt;
833 &lt;td&gt;44%&lt;/td&gt;
834 &lt;td&gt;Jan 12, 2003 11:14 AM&lt;/td&gt;
835 &lt;/tr&gt;
836 &lt;/tbody&gt;
837 &lt;tbody class=&quot;tablesorter-infoOnly&quot;&gt;
838 &lt;tr&gt;
839 &lt;td colspan=&quot;6&quot;&gt;
840 &lt;strong&gt;JSON Info Row&lt;/strong&gt;
841 &lt;/td&gt;
842 &lt;/tr&gt;
843 &lt;/tbody&gt;
844 &lt;tbody&gt;
845 &lt;tr&gt;
846 &lt;td&gt;Bruce&lt;/td&gt;
847 &lt;td&gt;Evans&lt;/td&gt;
848 &lt;td&gt;22&lt;/td&gt;
849 &lt;td&gt;$13.19&lt;/td&gt;
850 &lt;td&gt;11%&lt;/td&gt;
851 &lt;td&gt;Jan 18, 2007 9:12 AM&lt;/td&gt;
852 &lt;/tr&gt;
853 &lt;tr&gt;
854 &lt;td&gt;Brice&lt;/td&gt;
855 &lt;td&gt;Almighty&lt;/td&gt;
856 &lt;td&gt;45&lt;/td&gt;
857 &lt;td&gt;$153.19&lt;/td&gt;
858 &lt;td&gt;44%&lt;/td&gt;
859 &lt;td&gt;Jan 18, 2001 9:12 AM&lt;/td&gt;
860 &lt;/tr&gt;
861 &lt;tr class=&quot;specialRow&quot; data-info=&quot;This row likes turtles&quot;&gt;
862 &lt;td class=&quot;fname&quot;&gt;Fred&lt;/td&gt;
863 &lt;td class=&quot;lname&quot;&gt;Smith&lt;/td&gt;
864 &lt;td class=&quot;age&quot; data-info=&quot;fake ID!, he&#39;s only 16&quot;&gt;18&lt;/td&gt;
865 &lt;td class=&quot;total&quot;&gt;$22.44&lt;/td&gt;
866 &lt;td&gt;8%&lt;/td&gt;
867 &lt;td class=&quot;date&quot;&gt;Aug 20, 2012 10:15 AM&lt;/td&gt;
868 &lt;/tr&gt;
869 &lt;/tbody&gt;
870 &lt;tfoot&gt;
871 &lt;tr&gt;
872 &lt;th class=&quot;fname&quot;&gt;First Name&lt;/th&gt;
873 &lt;th&gt;Last Name&lt;/th&gt;
874 &lt;th class=&quot;age&quot; data-sorter=&quot;false&quot;&gt;Age&lt;/th&gt;
875 &lt;th&gt;Total&lt;/th&gt;
876 &lt;th class=&quot;sorter-false&quot;&gt;Discount&lt;/th&gt;
877 &lt;th class=&quot;date&quot;&gt;Date&lt;/th&gt;
878 &lt;/tr&gt;
879 &lt;/tfoot&gt;
880 &lt;/table&gt;
881 &lt;/div&gt;</pre>
882
883
884 <h3>Result</h3>
885 <div id="object2Table2" class="container"></div>
886
887 </div>
888
889 </div> <!-- end accordion -->
890
891 </div> <!-- end #main -->
892
893 <div class="csv hidden">
894 Album,Artist,Price
895 Lateralus,Tool,$13.00
896 Aenima,Tool,$12.00
897 "10,000 days",Tool,$14.00
898 Down In It,Nine Inch Nails,$3.00
899 Broken,Nine Inch Nails,$6.00
900 Muse,Black Holes and Revelations,$7.00
901 Anon,"fake album, with comma", $1.00
902 Album,Artist,Price (USD)
903 </div>
904
905 </body>
906 </html>