| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<title>jQuery tablesorter 2.0 - Using extractors with 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="../js/parsers/parser-input-select.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() { |
| 26 |
// return false so this parser is not auto detected |
| 27 |
return false; |
| 28 |
}, |
| 29 |
format: function(s) { |
| 30 |
return s.toLowerCase() |
| 31 |
.replace(/good/, 2) |
| 32 |
.replace(/medium/, 1) |
| 33 |
.replace(/bad/, 0); |
| 34 |
}, |
| 35 |
type: 'numeric' |
| 36 |
}); |
| 37 |
|
| 38 |
$(function() { |
| 39 |
$("table").tablesorter({ |
| 40 |
theme: 'blue' |
| 41 |
// "extractor-select" and "sorter-grades" are added as a class name in the HTML |
| 42 |
// or un-comment out the option below |
| 43 |
// ,headers: { 6: { extractor: 'select', sorter: 'grades' } } |
| 44 |
}); |
| 45 |
|
| 46 |
});</script> |
| 47 |
</head> |
| 48 |
<body> |
| 49 |
|
| 50 |
<div id="banner"> |
| 51 |
<h1>table<em>sorter</em></h1> |
| 52 |
<h2>Using extractors with parsers</h2> |
| 53 |
<h3>Flexible client-side table sorting</h3> |
| 54 |
<a href="index.html">Back to documentation</a> |
| 55 |
</div> |
| 56 |
|
| 57 |
<div id="main"> |
| 58 |
|
| 59 |
<p class="tip"> |
| 60 |
<em>NOTE!</em> |
| 61 |
</p> |
| 62 |
<ul> |
| 63 |
<li>The extractor type was added in <span class="version">v2.17.6</span> and allows you to apply two parsers to a column: |
| 64 |
<ul> |
| 65 |
<li>In this demo, the last column contains select dropdowns that have "good", "medium" and "bad" options.</li> |
| 66 |
<li>So there is a need to use a parser to extract content from the selected option within the select and then use the grades parser to give each grade a sortable value.</li> |
| 67 |
<li>Thanks to <a href="https://github.com/TheSin-">TheSin-</a> for adding this functionality!</li> |
| 68 |
</ul> |
| 69 |
</li> |
| 70 |
<li>The extractor <em>is always</em> used before the parser.</li> |
| 71 |
<li>Individual columns can be modified by adding the following (they all do the same thing), set in order of priority: |
| 72 |
<ul> |
| 73 |
<li>jQuery data <code>data-extractor="select"</code>.</li> |
| 74 |
<li>metadata <code>class="{ extractor: 'select'}"</code>. This requires the metadata plugin.</li> |
| 75 |
<li>headers option <code>headers : { 0 : { extractor : 'select' } }</code>.</li> |
| 76 |
<li>header class name <code>class="extractor-select"</code>.</li> |
| 77 |
</ul> |
| 78 |
</li> |
| 79 |
</ul> |
| 80 |
|
| 81 |
<h1>Demo</h1> |
| 82 |
<div id="demo"><table class="tablesorter"> |
| 83 |
<thead> |
| 84 |
<tr> |
| 85 |
<th class="sorter-text">Name</th> |
| 86 |
<th>Major</th> |
| 87 |
<th>Gender</th> |
| 88 |
<th>English</th> |
| 89 |
<th>Japanese</th> |
| 90 |
<th>Calculus</th> |
| 91 |
<th class="extractor-select sorter-grades">Overall grades</th> |
| 92 |
</tr> |
| 93 |
</thead> |
| 94 |
<tbody> |
| 95 |
<tr><td>Student01</td><td>Languages</td><td>male</td><td>80</td><td>70</td><td>75</td><td><select><option>good</option><option selected="selected">medium</option><option>bad</option></select></td></tr> |
| 96 |
<tr><td>Student02</td><td>Mathematics</td><td>male</td><td>90</td><td>88</td><td>100</td><td><select><option selected="selected">good</option><option>medium</option><option>bad</option></select></td></tr> |
| 97 |
<tr><td>Student03</td><td>Languages</td><td>female</td><td>85</td><td>95</td><td>80</td><td><select><option selected="selected">good</option><option>medium</option><option>bad</option></select></td></tr> |
| 98 |
<tr><td>Student04</td><td>Languages</td><td>male</td><td>20</td><td>50</td><td>65</td><td><select><option>good</option><option>medium</option><option selected="selected">bad</option></select></td></tr> |
| 99 |
<tr><td>Student05</td><td>Mathematics</td><td>female</td><td>70</td><td>78</td><td>70</td><td><select><option>good</option><option selected="selected">medium</option><option>bad</option></select></td></tr> |
| 100 |
<tr><td>Student06</td><td>Mathematics</td><td>male</td><td>44</td><td>65</td><td>60</td><td><select><option>good</option><option>medium</option><option selected="selected">bad</option></select></td></tr> |
| 101 |
<tr><td>Student07</td><td>Languages</td><td>female</td><td>99</td><td>92</td><td>89</td><td><select><option selected="selected">good</option><option>medium</option><option>bad</option></select></td></tr> |
| 102 |
<tr><td>Student08</td><td>Mathematics</td><td>female</td><td>90</td><td>98</td><td>90</td><td><select><option selected="selected">good</option><option>medium</option><option>bad</option></select></td></tr> |
| 103 |
<tr><td>Student09</td><td>Mathematics</td><td>male</td><td>55</td><td>72</td><td>66</td><td><select><option>good</option><option selected="selected">medium</option><option>bad</option></select></td></tr> |
| 104 |
</tbody> |
| 105 |
</table></div> |
| 106 |
|
| 107 |
<h1>Javascript</h1> |
| 108 |
<div id="javascript"> |
| 109 |
<pre class="prettyprint lang-javascript"></pre> |
| 110 |
</div> |
| 111 |
<h1>HTML</h1> |
| 112 |
<div id="html"> |
| 113 |
<pre class="prettyprint lang-html"></pre> |
| 114 |
</div> |
| 115 |
|
| 116 |
<div class="next-up"> |
| 117 |
<hr /> |
| 118 |
Next up: <a href="example-option-date-format.html">Changing the date format ››</a> |
| 119 |
</div> |
| 120 |
|
| 121 |
</div> |
| 122 |
|
| 123 |
</body> |
| 124 |
</html> |