| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<title>jQuery tablesorter 2.0 - Writing custom parsers</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 |
<script src="../addons/pager/jquery.tablesorter.pager.js"></script> |
| 20 |
|
| 21 |
<script id="js">// add parser through the tablesorter addParser method |
| 22 |
$.tablesorter.addParser({ |
| 23 |
// set a unique id |
| 24 |
id: 'grades', |
| 25 |
is: function(s, table, cell, $cell) { |
| 26 |
// return false so this parser is not auto detected |
| 27 |
return false; |
| 28 |
}, |
| 29 |
format: function(s, table, cell, cellIndex) { |
| 30 |
// format your data for normalization |
| 31 |
return s.toLowerCase() |
| 32 |
.replace(/good/,2) |
| 33 |
.replace(/medium/,1) |
| 34 |
.replace(/bad/,0); |
| 35 |
}, |
| 36 |
// set type, either numeric or text |
| 37 |
type: 'numeric' |
| 38 |
}); |
| 39 |
|
| 40 |
$(function() { |
| 41 |
$("table").tablesorter({ |
| 42 |
theme: 'blue' |
| 43 |
// "sorter-grades" added as a class name in the HTML (new in v2.0.11) |
| 44 |
// or un-comment out the option below |
| 45 |
// ,headers: { 6: { sorter: 'grades' } } |
| 46 |
}); |
| 47 |
|
| 48 |
});</script> |
| 49 |
</head> |
| 50 |
<body> |
| 51 |
|
| 52 |
<div id="banner"> |
| 53 |
<h1>table<em>sorter</em></h1> |
| 54 |
<h2>Writing custom parsers</h2> |
| 55 |
<h3>Flexible client-side table sorting</h3> |
| 56 |
<a href="index.html">Back to documentation</a> |
| 57 |
</div> |
| 58 |
|
| 59 |
<div id="main"> |
| 60 |
|
| 61 |
<p class="tip"> |
| 62 |
Notes about the <code>addParser</code> template: |
| 63 |
</p> |
| 64 |
<ul> |
| 65 |
<li>The <code>id</code> block is required and must be unique.</li> |
| 66 |
<li>The <code>is</code> block will allow the parser to be used for autodetecting the parser |
| 67 |
<ul> |
| 68 |
<li>Most custom parsers are made for a specific set of data/column, so the <code>is</code> block usually just returns false telling the plugin that the parser doesn't match any columns.</li> |
| 69 |
<li>If so desired, the function provides four parameters: <code>s</code> contains the text from the cell, <code>table</code> is the table DOM element, <code>cell</code> is the current cell DOM element and <code>$cell</code> is the current cell as a jQuery object (added <span class="version">v2.18.0</span>).</li> |
| 70 |
<li>This function must <code>return true</code> before the plugin will use the custom parser's format block to process the column content.</li> |
| 71 |
<li>In version 2.7.4, the <code>is</code> block became optional because most parsers just return false.</li> |
| 72 |
</ul> |
| 73 |
</li> |
| 74 |
<li>The <code>format</code> block is used to normalize your data and return it to the plugin for sorting |
| 75 |
<ul> |
| 76 |
<li>Within this function, modify the given text from the cell (<code>s</code>) or obtain parameters and/or other data from the cell (<code>cell</code>) then return this data to the plugin.</li> |
| 77 |
<li>As an example, the date parser takes the date string (e.g. <code>"12/25/2013"</code>) and converts it into a numeric value (<code>1387951200000</code>; <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getTime">ref</a>) to make sorting and comparing dates easier.</li> |
| 78 |
<li>Use the <code>cellIndex</code> if the cells within columns contain different data - see this <a href="example-parsers-advanced.html">demo</a> for an example.</li> |
| 79 |
</ul> |
| 80 |
</li> |
| 81 |
<li>The <code>parsed</code> block (added <span class="version">v2.15.0</span>) |
| 82 |
<ul> |
| 83 |
<li>This parameter is a flag used by the filter widget.</li> |
| 84 |
<li>When <code>true</code>, the filter widget will only search through parsed data for matches.</li> |
| 85 |
<li>If <code>false</code> (default value), the filter widget will search through the cell text for matches.</li> |
| 86 |
<li>Currently, only the parsers for inputs, checkboxes and selects have this parameter set to <code>true</code>.</li> |
| 87 |
</ul> |
| 88 |
</li> |
| 89 |
<li>The <code>type</code> block sets the type of sort to use: |
| 90 |
<ul> |
| 91 |
<li>Set it to either <code>"text"</code> or <code>"numeric"</code>.</li> |
| 92 |
<li>This tells the plugin the type of sorter to use.</li> |
| 93 |
<li>Text type sorting uses a natural sort and is a tiny bit slower than a pure numeric sort.</li> |
| 94 |
</ul> |
| 95 |
</li> |
| 96 |
</ul> |
| 97 |
|
| 98 |
<h3>Add Parser Template</h3> |
| 99 |
<pre class="prettyprint lang-javascript">$.tablesorter.addParser({ |
| 100 |
// use a unique id |
| 101 |
id: 'myparser', |
| 102 |
is: function(s, table, cell, $cell) { |
| 103 |
// s is the text from the cell |
| 104 |
// table is the current table (as a DOM element; not jQuery object) |
| 105 |
// cell is the current table cell (DOM element) |
| 106 |
// $cell is the current table cell (jQuery object; added v2.18.0) |
| 107 |
// return false if you don't want this parser to be auto detected |
| 108 |
return false; |
| 109 |
}, |
| 110 |
format: function(s, table, cell, cellIndex) { |
| 111 |
// s is the text from the cell |
| 112 |
// table is the current table (as a DOM element; not jQuery object) |
| 113 |
// cell is the current table cell (DOM element) |
| 114 |
// cellIndex is the current cell's column index |
| 115 |
// format your data for normalization |
| 116 |
// (i.e. do something to get and/or modify your data, then return it) |
| 117 |
return s; |
| 118 |
}, |
| 119 |
// flag for filter widget (true = ALWAYS search parsed values; false = search cell text) |
| 120 |
parsed: false, |
| 121 |
// set the type to either numeric or text (text uses a natural sort function |
| 122 |
// so it will work for everything, but numeric is faster for numbers |
| 123 |
type: 'numeric' |
| 124 |
});</pre> |
| 125 |
|
| 126 |
<h1>Demo</h1> |
| 127 |
<div id="demo"><table class="tablesorter"> |
| 128 |
<thead> |
| 129 |
<tr> |
| 130 |
<th class="sorter-text">Name</th> |
| 131 |
<th>Major</th> |
| 132 |
<th>Gender</th> |
| 133 |
<th>English</th> |
| 134 |
<th>Japanese</th> |
| 135 |
<th>Calculus</th> |
| 136 |
<th class="sorter-grades">Overall grades</th> |
| 137 |
</tr> |
| 138 |
</thead> |
| 139 |
<tbody> |
| 140 |
<tr><td>Student01</td><td>Languages</td><td>male</td><td>80</td><td>70</td><td>75</td><td>medium</td></tr> |
| 141 |
<tr><td>Student02</td><td>Mathematics</td><td>male</td><td>90</td><td>88</td><td>100</td><td>good</td></tr> |
| 142 |
<tr><td>Student03</td><td>Languages</td><td>female</td><td>85</td><td>95</td><td>80</td><td>good</td></tr> |
| 143 |
<tr><td>Student04</td><td>Languages</td><td>male</td><td>20</td><td>50</td><td>65</td><td>bad</td></tr> |
| 144 |
<tr><td>Student05</td><td>Mathematics</td><td>female</td><td>70</td><td>78</td><td>70</td><td>medium</td></tr> |
| 145 |
<tr><td>Student06</td><td>Mathematics</td><td>male</td><td>44</td><td>65</td><td>60</td><td>bad</td></tr> |
| 146 |
</tbody> |
| 147 |
</table></div> |
| 148 |
|
| 149 |
<h1>Javascript</h1> |
| 150 |
<h3>Grades Parser Code</h3> |
| 151 |
<div id="javascript"> |
| 152 |
<pre class="prettyprint lang-javascript"></pre> |
| 153 |
</div> |
| 154 |
<h1>HTML</h1> |
| 155 |
<p class="tip"> |
| 156 |
<em>NOTE!</em> Assigning the parser using a class name was added in version 2.0.11 (it is not part of the original plugin; use the <code>headers</code> option in older versions). |
| 157 |
</p> |
| 158 |
<div id="html"> |
| 159 |
<pre class="prettyprint lang-html"></pre> |
| 160 |
</div> |
| 161 |
|
| 162 |
<div class="next-up"> |
| 163 |
<hr /> |
| 164 |
Next up: <a href="example-parsers-advanced.html">Writing custom parsers, advanced ››</a> |
| 165 |
</div> |
| 166 |
|
| 167 |
</div> |
| 168 |
|
| 169 |
</body> |
| 170 |
</html> |