Animated table sort (REGEXP friendly)
Overview
This plugin allows you to animatedly sort a table based on a column's <td>s, or on the content/value of a child/descendant element within those <td>s. The various <td>s fly to their new homes, giving a nice effect. It also supports sorting on REGEXP matches. You can also control whether row relationships are maintained, whether it sorts on ascii or numeric and ascending or descending.
Usage & parameters
Call tableSort(params) on a table returned by a jQuery selector.
$(selector).tableSort(params);
...where params is an object of property/value pairings, including:
onCol (int, required) - the index of the column in your table to sort on (not zero-indexed)
keepRelationships (bool, default: false) - if true, row relationships wil be maintained. That is, the sibling <td>s will move also
regexp (RegExp, default: null) - a string representing a regular expression (/pattern/) to sort on instead of the complete contents of the <td>. Useful with regexpIndex - see below.
regexpIndex (int, default: 0) - if sorting on a regular expression, this is the index of the returned array of matches to use for the sort, should it return more than one match. See below for an example of this, where a regexp sub-pattern is used.
child (string) - sort on the content/value of a child/descendant element within each <td>, rather than on the <td>s themselves. This should be a selector string to match that element. See notes for more info.
sortType (string, default: ascii) - either 'ascii' or, if wanting to sort numerically, 'numeric'
sortDesc (bool, default: false) - if true, the sort will be done in descending order, not ascending
Example 1
Click the column headings to sort the table based on that column
| Place | County | Phone code | Approx. population |
|---|---|---|---|
| Nottingham | Nottinghamshire | 0115 | 292400 |
| Luton | Bedfordshire | 01582 | 203800 |
| Huddersfield | Yorkshire | 01484 | 146234 |
| Carlisle | Cumbria | 01228 | 103700 |
| Brighton | East Sussex | 01273 | 155919 |
| Woking | Surrey | 01483 | 62796 |
| Basingstoke | Hampshire | 01256 | 82913 |
| Rhyll | Clwyd | 01745 | 24889 |
Here's the code applied to the links in the <th>s:
1//column 1 - by place name
2$('#example1_table').sortTable({
3 onCol: 1,
4 keepRelationships: true
5});
6//column 2 - by county
7$('#example1_table').sortTable({
8 onCol: 2,
9 keepRelationships: true
10});
11//column 3 - by phone code
12$('#example1_table').sortTable({
13 onCol: 3,
14 keepRelationships: true,
15 sortType: 'numeric'
16});
17//column 4 - by approx. population
18$('#example1_table').sortTable({
19 onCol: 4,
20 keepRelationships: true,
21 sortType: 'numeric'
22});
Example 2 - REGEXP
To illustrate better how the regular expression parameters can be useful, consider the following example.
| Product category |
|---|
| Playstation 3 games (164) |
| XBox 360 games (94) |
| Nintendo Wii games (142) |
| Nintendo DS games (89) |
| PC games (221) |
| Apple Mac games (18) |
This simple table shows a number of product categories and, for each, the number of products in each one.
Here, we want to sort on the number of products, not on the whole contents of each <td>. Stipulating this in a suitable regular expression string passed in the regexp parameter allows this.
Click the link in the <th> to see the demo.
Here's the code behind this example:
1$('#example1_table').sortTable({
2 onCol: 1,
3 sortType: 'numeric',
4 sortDesc: true,
5 regexp: /\((\d+)\)/, //match just the number of products
6 regexpIndex: 1, //sort on just the number, not brackets too
7 keepRelationships: true
8});
Note we specify regexpIndex as 1 because we our regexp pattern returns an array of matches containing two elements, and we want to sort on the number in isolation, i.e. the sub-pattern, not on the surrounding brackets as well.
If this is Greek to you, click here for more information on Javascript regular expressions.
Notes
DOM
Since <td>s are very fixed elements and aren't animatable, the animation is done by some DOM-scripted <div>s, which animate to their new homes then repopulate their new <td> homes.
These complimentary elements are removed once the animation is complete, leaving you with a legitimate table, each of the <td>s of which genuinely contain the content they appear to. This means you can perform further traversal or manipulation on the table without issue.
UPDATE - 17/07/2010: sort on child element
Following a request, you can now sort on the content/value of a child/descendant element within each <td> by passing a selector string to match that element in a new parameter, child.
So for example, if the <td>s in column one each contained a text field, and you wanted to sort the table on the current values in those fields, you could run the following:
1$('#example1_table').sortTable({
2 onCol: 1,
3 keepRelationships: true,
4 child: 'input'
5});
Important: ensure child matches only one element in each <td>, or the sort will fail. So in the above example, if each <td> contained more than one <input>, the script would be break. To avoid this, be more specific, e.g.
child: 'input.myfield'