Debian Codegraph @

README.md

raw symbols SCIP
1 Stupid jQuery Table Sort
2 ========================
3
4 This is a stupid jQuery table sorting plugin. Nothing fancy, nothing really
5 impressive. Overall, stupidly simple. Requires jQuery 1.7 or newer.
6
7 [View the demo here][0]
8
9 See the examples directory.
10
11 Installation via [npm][2]
12 -------------------------
13
14 $ npm i stupid-table-plugin
15
16 Installation via Bower
17 ----------------------
18
19 $ bower install jquery-stupid-table
20
21
22 Example Usage
23 -------------
24
25 The JS:
26
27 $("table").stupidtable();
28
29 The HTML:
30
31 <table>
32 <thead>
33 <tr>
34 <th data-sort="int">int</th>
35 <th data-sort="float">float</th>
36 <th data-sort="string">string</th>
37 </tr>
38 </thead>
39 <tbody>
40 <tr>
41 <td>15</td>
42 <td>-.18</td>
43 <td>banana</td>
44 </tr>
45 ...
46 ...
47 ...
48
49 The thead and tbody tags must be used.
50
51 Add a `data-sort` attribute of "DATATYPE" to the th elements to make them sortable
52 by that data type. If you don't want that column to be sortable, just omit the
53 `data-sort` attribute.
54
55
56 Predefined data types
57 ---------------------
58
59 Our aim is to keep this plugin as lightweight as possible. Consequently, the
60 only predefined datatypes that you can pass to the th elements are
61
62 * `int`
63 * `float`
64 * `string` (case-sensitive)
65 * `string-ins` (case-insensitive)
66
67 These data types will be sufficient for many simple tables. However, if you need
68 different data types for sorting, you can easily create your own!
69
70 Data with multiple representations/predefined order
71 ---------------------------------------------------
72
73 Stupid Table lets you sort a column by computer friendly values while displaying
74 human friendly values via the `data-sort-value` attribute on a td element. For
75 example, to sort timestamps (computer friendly) but display pretty formated
76 dates (human friendly)
77
78 <table>
79 <thead>
80 <tr>
81 <th data-sort="string">Name</th>
82 <th data-sort="int">Birthday</th>
83 </tr>
84 </thead>
85 <tbody>
86 <tr>
87 <td>Joe McCullough</td>
88 <td data-sort-value="672537600">April 25, 1991</td>
89 </tr>
90 <tr>
91 <td>Clint Dempsey</td>
92 <td data-sort-value="416016000">March 9, 1983</td>
93 </tr>
94 ...
95 ...
96 ...
97
98 In this example, Stupid Table will sort the Birthday column by the timestamps
99 provided in the `data-sort-value` attributes of the corresponding tds. Since
100 timestamps are integers, and that's what we're sorting the column by, we specify
101 the Birthday column as an `int` column in the `data-sort` value of the column
102 header.
103
104
105 Default sorting direction
106 -------------------------
107
108 By default, columns will sort ascending. You can specify a column to sort "asc"
109 or "desc" first.
110
111 <table>
112 <thead>
113 <tr>
114 <th data-sort="float" data-sort-default="desc">float</th>
115 ...
116 </tr>
117 </thead>
118 </table>
119
120 Sorting a column on load
121 ------------------------
122
123 If you want a specific column to be sorted immediately after
124 `$table.stupidtable()` is called, you can provide a `data-sort-onload=yes`
125 attribute.
126
127 <table>
128 <thead>
129 <tr>
130 <th data-sort="float" data-sort-onload=yes>float</th>
131 ...
132 </tr>
133 </thead>
134 </table>
135
136 Multicolumn sorting
137 -------------------
138
139 A multicolumn sort allows you to define secondary columns to sort by in the
140 event of a tie with two elements in the sorted column. See [examples/multicolumn-sort.html](https://rawgit.com/joequery/Stupid-Table-Plugin/master/examples/multicolumn-sort.html).
141 Specify a comma-separated list of th identifiers in a `data-sort-multicolumn`
142 attribute on a `<th>` element. An identifier can be an integer (which represents
143 the index of the th element of the multicolumn target) or a string (which
144 represents the id of the th element of the multicolumn target).
145
146 <table>
147 <thead>
148 <tr>
149 <th id="int-column" data-sort="int" data-sort-multicolumn="1,string-column">int</th>
150 <th id="float-column" data-sort="float" data-sort-multicolumn="string-column,int-column">float</th>
151 <th id="string-column" data-sort="string" data-sort-multicolumn="1,0">string</th>
152 </tr>
153 </thead>
154 <tbody>
155 <tr>
156 <td>1</td>
157 <td>10.0</td>
158 <td>a</td>
159 </tr>
160 <tr>
161 <td>1</td>
162 <td>10.0</td>
163 <td>a</td>
164 </tr>
165
166 Sorting a column programatically
167 --------------------------------
168
169 After you have called `$("#mytable").stupidtable()`, if you wish to sort a
170 column without requiring the user to click on it, select the column th and call
171
172
173 var $table = $("#mytable").stupidtable();
174 var $th_to_sort = $table.find("thead th").eq(0);
175 $th_to_sort.stupidsort();
176
177 // You can also force a direction.
178 $th_to_sort.stupidsort('asc');
179 $th_to_sort.stupidsort('desc');
180
181 Updating a table cell's value
182 -----------------------------
183
184 If you wish for Stupid Table to respond to changes in the table cell values, you
185 must explicitely inform Stupid Table to update its cache with the new values. If
186 you update the table display/sort values without using this mechanism, your
187 newly updated table **will not sort correctly!**
188
189 /*
190 * Suppose $age_td is some td in a table under a column specified as an int
191 * column. stupidtable() must already be called for this table.
192 */
193 $age_td.updateSortVal(23);
194
195 Note that this only changes the internal sort value (whether you specified a
196 `data-sort-value` or not). Use the standard jQuery `.text()` / `.html()` methods
197 if you wish to change the display values.
198
199
200 Callbacks
201 ---------
202
203 To execute a callback function after a table column has been sorted, you can
204 bind on `aftertablesort`.
205
206 var table = $("table").stupidtable();
207 table.bind('aftertablesort', function (event, data) {
208 // data.column - the index of the column sorted after a click
209 // data.direction - the sorting direction (either asc or desc)
210 // data.$th - the th element (in jQuery wrapper)
211 // $(this) - this table object
212
213 console.log("The sorting direction: " + data.direction);
214 console.log("The column index: " + data.column);
215 });
216
217 Similarly, to execute a callback before a table column has been sorted, you can
218 bind on `beforetablesort`.
219
220 See the complex_example.html file.
221
222 Creating your own data types
223 ----------------------------
224
225 Sometimes you don't have control over the HTML produced by the backend. In the
226 event you need to sort complex data without a `data-sort-value` attribute, you
227 can create your own data type. Creating your own data type for sorting purposes
228 is easy as long as you are comfortable using custom functions for sorting.
229 Consult [Mozilla's Docs][1] if you're not.
230
231 Let's create an alphanum datatype for a User ID that takes strings in the form
232 "D10", "A40", and sorts the column based on the numbers in the string.
233
234 <thead>
235 <tr>
236 <th data-sort="string">Name</th>
237 <th data-sort="int">Age</th>
238 <th data-sort="alphanum">UserID</th>
239 </tr>
240 </thead>
241 <tbody>
242 <tr>
243 <td>Joseph McCullough</td>
244 <td>20</td>
245 <td>D10</td>
246 </tr>
247 <tr>
248 <td>Justin Edwards</td>
249 <td>29</td>
250 <td>A40</td>
251 </tr>
252 ...
253 ...
254 ...
255
256 Now we need to specify how the **alphanum** type will be sorted. To do that,
257 we do the following:
258
259 $("table").stupidtable({
260 "alphanum":function(a,b){
261
262 var pattern = "^[A-Z](\\d+)$";
263 var re = new RegExp(pattern);
264
265 var aNum = re.exec(a).slice(1);
266 var bNum = re.exec(b).slice(1);
267
268 return parseInt(aNum,10) - parseInt(bNum,10);
269 }
270 });
271
272 This extracts the integers from the cell and compares them in the style
273 that sort functions use.
274
275 StupidTable Settings
276 --------------------
277
278 As of 1.1.0 settings have been introduced. Settings are defined like so:
279
280 var $table = $("#mytable");
281 $table.stupidtable_settings({
282 // Settings for this table specified here
283 });
284 $table.stupidtable();
285
286 Listed below are the available settings.
287
288 ### will_manually_build_table
289
290 (Introduced in verison 1.1.1)
291
292 Options:
293
294 * `true`
295 * `false` (default)
296
297 By default, every time a column is sorted, stupidtable reads the DOM to extract
298 all the values from the table. For tables that will not change or for very large
299 tables, this behavior may be suboptimal. To modify this behavior, set the
300 `will_manually_build_table` setting to `true`. However, you will be responsible
301 for informing stupidtable that the table has been modified by calling
302 `$table.stupidtable_build()`.
303
304 var $table = $("#mytable");
305 $table.stupidtable_settings({
306 will_manually_build_table: true
307 });
308 $table.stupidtable();
309
310 // Make some modification to the table, such as deleting a row
311 ...
312 ...
313
314 // Since will_manually_build_table is true, we must build the table in order
315 // for future sorts to properly handle our modifications.
316 $table.stupidtable_build();
317
318 ### should_redraw
319
320 (Introduced in verison 1.1.0)
321
322 The `should_redraw` setting allows you to specify a function that determines
323 whether or not the table should be redrawn after it has been internally sorted.
324
325 The `should_redraw` function takes a `sort_info` object as an argument. The
326 object keys available are:
327
328 * `column` - An array representing the sorted column. Each element of the array is of the form `[sort_val, $tr, index]`
329 * `sort_dir` - `"asc"` or `"desc"`
330 * `$th` - The jquery object of the `<th>` element that was clicked
331 * `th_index` - The index of the `<th>` element that was cliked
332 * `$table` - The jquery object of the `<table>` that contains the `<th>` that was clicked
333 * `datatype` - The datatype of the column
334 * `compare_fn` - The sort/compare function associated with the `<th>` clicked.
335
336 **Example**: If you want to prevent stupidtable from redrawing the table if the
337 column sorted has all identical values, you would do the following:
338
339 var $table = $("#mytable");
340 $table.stupidtable_settings({
341 should_redraw: function(sort_info){
342 var sorted_column = sort_info.column;
343 var first_val = sorted_column[0];
344 var last_val = sorted_column[sorted_column.length - 1][0];
345
346 // If first and last element of the sorted column are the same, we
347 // can assume all elements are the same.
348 return sort_info.compare_fn(first_val, last_val) !== 0;
349 }
350 });
351 $table.stupidtable();
352
353 License
354 -------
355
356 The Stupid jQuery Plugin is licensed under the MIT license. See the LICENSE
357 file for full details.
358
359 Tests
360 -----
361
362 Visit `tests/test.html` in your browser to run the QUnit tests.
363
364
365 [0]: http://joequery.github.io/Stupid-Table-Plugin/
366 [1]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort
367 [2]: https://www.npmjs.com/package/stupid-table-plugin