-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdemo.html
More file actions
246 lines (233 loc) · 8.2 KB
/
Copy pathdemo.html
File metadata and controls
246 lines (233 loc) · 8.2 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8'>
<title>Demo for sort-table.js</title>
<script type="text/javascript" src="sort-table.js"></script>
<style type="text/css">
table { border: 1px solid black; border-collapse: collapse; }
th, td { padding: 2px 5px; border: 1px solid black; }
thead { background: #ddd; }
table#demo2.js-sort-0 tbody tr td:nth-child(1),
table#demo2.js-sort-1 tbody tr td:nth-child(2),
table#demo2.js-sort-2 tbody tr td:nth-child(3),
table#demo2.js-sort-3 tbody tr td:nth-child(4),
table#demo2.js-sort-4 tbody tr td:nth-child(5),
table#demo2.js-sort-5 tbody tr td:nth-child(6),
table#demo2.js-sort-6 tbody tr td:nth-child(7),
table#demo2.js-sort-7 tbody tr td:nth-child(8),
table#demo2.js-sort-8 tbody tr td:nth-child(9),
table#demo2.js-sort-9 tbody tr td:nth-child(10) {
background: #dee;
}
</style>
</head>
<body>
<header>
<h1>Demo for sort-table.js</h1>
</header>
<section id="body">
<p><b>sort-table.js</b> is a pure JavaScript (no dependencies) solution
to make HTML Tables sortable.</p>
<h2>Basic Usage</h2>
<p>The basic table sorting functionality can be added simply by
including <b>sort-table.js</b> anywhere in your document and adding
CSS class 'js-sort-table' to all tables you want sortable.
It works like this:</p>
<table class="js-sort-table" id="demo1">
<caption><table class="js-sort-table"></caption>
<thead>
<tr>
<th class="js-sort-number">Row</th>
<th>Strings</th>
<th class="js-sort-date">Dates</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Shoe</td>
<td>9/11/2001</td>
</tr>
<tr>
<td>2</td>
<td>Twine</td>
<td>12/7/1941</td>
</tr>
<tr>
<td>3</td>
<td>Thread</td>
<td>1/20/2008</td>
</tr>
<tr>
<td>4</td>
<td>Rope</td>
<td>9/17/1862</td>
</tr>
<tr>
<td>5</td>
<td>Yarn</td>
<td>8/6/1945</td>
</tr>
<tr>
<td>6</td>
<td>Nylon</td>
<td>7/4/1776</td>
</tr>
</tbody>
</table>
<h2>Advanced Styling</h2>
<p><b>sort-table.js</b> uses CSS classes to track its state. This allows
for advanced styling by applying styles to the classes below.</p>
<p>Also demonstrated in the table below is the handling of
<code>thead</code>-less tables. If no <code>thead</code> is found in
the table, one will be created and the first row will be added to it.
</p>
<table class="js-sort-table" id="demo2">
<tr>
<td class="js-sort-number">Row</td>
<td>CSS class</td>
<td>Description</td>
</tr>
<tr>
<td>1</td>
<td>js-sort-table</td>
<td>Apply this to the table to indicate sortability</td>
</tr>
<tr>
<td>2</td>
<td>js-sort-asc</td>
<td>This is applied to the table when sorting ascending</td>
</tr>
<tr>
<td>3</td>
<td>js-sort-desc</td>
<td>Likewise, this indicates descending sort</td>
</tr>
<tr>
<td>4</td>
<td>js-sort-0</td>
<td>The zero-based number of the sorted column</td>
</tr>
<tr>
<td>5</td>
<td>js-sort-string</td>
<td>By default, string sorting is used, making this optional</td>
</tr>
<tr>
<td>6</td>
<td>js-sort-date</td>
<td>When applied to a cell in the header, column will be interpreted as dates</td>
</tr>
<tr>
<td>7</td>
<td>js-sort-number</td>
<td>This tells sort-table to treat the column as numbers</td>
</tr>
<tr>
<td>8</td>
<td>js-sort-last</td>
<td>Set this class to a TH to sort by the last word in each cell</td>
</tr>
<tr>
<td>9</td>
<td>js-sort-input</td>
<td>If your table has form inputs, this allows sorting by their value</td>
</tr>
</table>
<h2>Advanced Sorting</h2>
<p>There are a few supported data types, and adding new ones is very
simple. To indicate the data type a column should be sorted as, add
its CSS class to the column's header (TH) tag. String sorting
is the default type. Below are demonstrated the included types. </p>
<form>
<table class="js-sort-table" id="demo3">
<thead>
<tr>
<th colspan="3" class="js-sort-none">Multi-row</th>
<th colspan="4" class="js-sort-none">headers use the last row</th>
</tr>
<tr>
<th class="js-sort-number" title="class="js-sort-number"">number</th>
<th class="js-sort-number" title="class="js-sort-number"">number</th>
<th class="js-sort-string" title="class="js-sort-string"">string (default)</th>
<th class="js-sort-date" title="class="js-sort-date"">date</th>
<th class="js-sort-last" title="class="js-sort-last"">last</th>
<th class="js-sort-input" title="class="js-sort-input"">input</th>
<th class="js-sort-none" title="class="js-sort-none"">none</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#f">1</a></td>
<td>123</td>
<td>Shoe</td>
<td>9/11/2001</td>
<td>John Doe</td>
<td><input type="text" size="6" name="a" value="What"/></td>
<td>Use</td>
</tr>
<tr>
<td><a href="#e">2</a></td>
<td>21</td>
<td>Twine</td>
<td>12/7/1941</td>
<td>John Q Public</td>
<td><input type="text" size="6" name="ab" value="are"/></td>
<td>None</td>
</tr>
<tr>
<td><a href="#g">3</a></td>
<td>3</td>
<td>Thread</td>
<td>1/20/2008</td>
<td>John Smith</td>
<td><input type="text" size="6" name="c" value="six"/></td>
<td>To</td>
</tr>
<tr>
<td><a href="#q">4</a></td>
<td>42</td>
<td>Rope</td>
<td>9/17/1862</td>
<td>Joe T Plumber</td>
<td><input type="text" size="6" name="d" value="types"/></td>
<td>Prevent</td>
</tr>
<tr>
<td><a href="#z">5</a></td>
<td>58</td>
<td>Yarn</td>
<td>8/6/1945</td>
<td>John</td>
<td><input type="text" size="6" name="e" value="of"/></td>
<td>Sort</td>
</tr>
<tr>
<td><a href="#a">6</a></td>
<td>6</td>
<td>Nylon</td>
<td>7/4/1776</td>
<td>John Johnson</td>
<td><input type="text" size="6" name="f" value="input?"/></td>
<td>Event.</td>
</tr>
</tbody>
</table>
<p>Manual calls to tableSort can be made, </p>
<ul>
<li>such as this one that sorts descending by the second column:
<button onclick="sortTable(document.getElementById('demo3'), 1, -1);"><code>sortTable(Table, 1,
-1);</code></button>
</li>
<li>or this one that re-sorts the table in the event that something else adjusted it:
<button onclick="sortTable(document.getElementById('demo3'));"><code>sortTable(Table);</code>
</button>
</li>
</ul>
</form>
</section>
<footer>
</footer>
</body>
</html>