File: example-extractors-parsers.html

package info (click to toggle)
jquery-tablesorter 1%3A2.31.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,024 kB
  • sloc: javascript: 19,496; sh: 14; makefile: 8
file content (124 lines) | stat: -rw-r--r-- 5,155 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>jQuery tablesorter 2.0 - Using extractors with parsers</title>

	<!-- jQuery -->
	<script src="js/jquery-latest.min.js"></script>

	<!-- Demo stuff -->
	<link rel="stylesheet" href="css/jq.css">
	<link href="css/prettify.css" rel="stylesheet">
	<script src="js/prettify.js"></script>
	<script src="js/docs.js"></script>

	<!-- Tablesorter: required -->
	<link rel="stylesheet" href="../css/theme.blue.css">
	<script src="../js/jquery.tablesorter.js"></script>
	<script src="../js/parsers/parser-input-select.js"></script>

	<script id="js">// add parser through the tablesorter addParser method
$.tablesorter.addParser({
	// set a unique id
	id: 'grades',
	is: function() {
		// return false so this parser is not auto detected
		return false;
	},
	format: function(s) {
		return s.toLowerCase()
			.replace(/good/, 2)
			.replace(/medium/, 1)
			.replace(/bad/, 0);
	},
	type: 'numeric'
});

$(function() {
	$("table").tablesorter({
		theme: 'blue'
		// "extractor-select" and "sorter-grades" are added as a class name in the HTML
		// or un-comment out the option below
		// ,headers: { 6: { extractor: 'select', sorter: 'grades' } }
	});

});</script>
</head>
<body>

<div id="banner">
	<h1>table<em>sorter</em></h1>
	<h2>Using extractors with parsers</h2>
	<h3>Flexible client-side table sorting</h3>
	<a href="index.html">Back to documentation</a>
</div>

<div id="main">

	<p class="tip">
		<em>NOTE!</em>
	</p>
	<ul>
		<li>The extractor type was added in <span class="version">v2.17.6</span> and allows you to apply two parsers to a column:
			<ul>
				<li>In this demo, the last column contains select dropdowns that have "good", "medium" and "bad" options.</li>
				<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>
				<li>Thanks to <a href="https://github.com/TheSin-">TheSin-</a> for adding this functionality!</li>
			</ul>
		</li>
		<li>The extractor <em>is always</em> used before the parser.</li>
		<li>Individual columns can be modified by adding the following (they all do the same thing), set in order of priority:
			<ul>
				<li>jQuery data <code>data-extractor="select"</code>.</li>
				<li>metadata <code>class="{ extractor: 'select'}"</code>. This requires the metadata plugin.</li>
				<li>headers option <code>headers : { 0 : { extractor : 'select' } }</code>.</li>
				<li>header class name <code>class="extractor-select"</code>.</li>
			</ul>
		</li>
	</ul>

	<h1>Demo</h1>
	<div id="demo"><table class="tablesorter">
	<thead>
		<tr>
			<th class="sorter-text">Name</th>
			<th>Major</th>
			<th>Gender</th>
			<th>English</th>
			<th>Japanese</th>
			<th>Calculus</th>
			<th class="extractor-select sorter-grades">Overall grades</th> <!-- set the column parser using a class name -->
		</tr>
	</thead>
	<tbody>
		<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>
		<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>
		<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>
		<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>
		<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>
		<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>
		<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>
		<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>
		<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>
	</tbody>
</table></div>

	<h1>Javascript</h1>
	<div id="javascript">
		<pre class="prettyprint lang-javascript"></pre>
	</div>
	<h1>HTML</h1>
	<div id="html">
		<pre class="prettyprint lang-html"></pre>
	</div>

<div class="next-up">
	<hr />
	Next up: <a href="example-option-date-format.html">Changing the date format &rsaquo;&rsaquo;</a>
</div>

</div>

</body>
</html>