table sort
23 October 2006 // javascript. things.
No, your eyes don't deceive you, this is Yet Another Javascript Table Sort script, designed to be unobtrusive, simple and flexible... (skipped 1000 lines of happy talk ;)
| Login | Points | Created at | US Date | German Date | Money | Image |
| Blythe | 50 | Wed, November 24, 2004 | 04/12/03 | 3.9.05 | $6,89 | img20.png |
| Edmund | 104 | Fri, January 23, 2004 | 03/20/03 | 10.1.05 | $87,75 | img27.png |
| Jonquil | 12 | Sat, January 22, 2005 | 04/15/03 | 30.8.04 | $1.239,14 | img1b.png |
| Lee | 55 | Fri, March 11, 2005 | 06/03/05 | 4.5.05 | $41,74 | img129.png |
| Sky | 33 | Fri, May 27, 2005 | 02/02/05 | 22.9.04 | -$8,92 | img15.png |
| Archibald | 75 | Tue, January 18, 2005 | 01/14/04 | 23.2.04 | -- | img10a.png |
| Emma | 70 | Tue, August 23, 2005 | 01/14/05 | 01.05.03 | $6,64 | img.png |
| Erin | 92 | Tue, August 9, 2005 | 08/27/03 | 01.03.04 | $12,77 | image |
| Edgar | 25 | Fri, January 2, 2004 | 12/22/03 | 12.06.05 | $56,20 | img1.png |
| Prudence | 85 | Thu, April 29, 2004 | 05/13/04 | 15.8.03 | -$37,62 | img10.png |
| Walter | 50 | Sun, March 16, 2003 | 03/27/04 | 16.7.05 | $1.234,56 | |
| Reginald | 81 | Sat, March 8, 2003 | 10/07/04 | 21.3.05 | $74,74 | img3.png |
| Mercy | 50 | Thu, June 17, 2004 | 09/04/03 | 30.07.05 | $75,96 | img1.png |
| Holly | 60 | Tue, August 23, 2005 | 12/14/04 | 10.03.04 | $53,77 | not found |
| Cedric | 76 | Mon, June 28, 2004 | 08/18/04 | 13.1.03 | $1,48 | img1a.png |
usage
<script src="tablesort.js"></script>
that's all about it. ;)
By default, the script starts automatically at page load and makes all tables in document sortable. To disable autostart, set TableSortNoAutoStart to true before including the script.
As usual, your tables should be divided into THEAD and TBODY, you also need to declare three css classes for table header cells, for example:
/* indicates that the column can be sorted */
td.sortable
{ background:#606060; font-weight: bold; cursor: pointer; }
/* indicates that the column is sorted from low to high */
td.sorted_asc
{ background:#606060 url(up.gif) center right no-repeat; }
/* indicates that the column is sorted from high to low */
td.sorted_desc
{ background:#606060 url(down.gif) center right no-repeat; }
data types
TS can handle different kinds of data in table columns. The built-in sorting rules are
- nocase -- case-insensitive sort (default)
- alpha -- case-sensitive sort
- numeric -- 123 or 123.5 or 1e+3
- natural -- natural sort (like Image column in the table above)
- currency -- currency values like $0,55 or 1,234.56€
- date -- dates (in any format javascript parse can handle)
- usdate -- US dates (mm/dd/yyyy)
- eudate -- German dates (dd.mm.yyyy)
TS is able to detect most data types based on the contents of the first cell in the column.
api
For javascript programmers among us ;) TS provides two public functions: enable() and sort() . The prototype of enable() is as follows
TableSort.enable(table, column, compare)
This turns on sorting of table table on column column using compare as comparison function (well, was it really necessary to say that? Good programs don't need any documentation, do they? ;) Anyways,
tableis either an id or DOM table objectcolumnis a column number (starting from 1) or title (without any html)compareis one of the rules listed above (in quotes) or a pointer to your own comparison function (as in sort)
If an argument is omitted (use '' or null as placeholder when appropriate), TS uses the following defaults:
- if
tableis omitted, it takes all tables (with THEADs) - if
columnis omitted, all columns will be sortable - if
compareis omitted, TS does its best to guess the correct datatype for the column
Examples:
// sort all columns in all tables using default comparison rules
TableSort.enable()
// sort 'Points' (the second) column in the 'people' table
// using default comparison rule
TableSort.enable('people', 'Points')
// same as above
TableSort.enable('people', 2)
// same as above
TableSort.enable(document.getElementsByTagName('TABLE')[0], 2)
// sort 'Created at' using 'human date' comparison
TableSort.enable('people', 'Created at', 'date')
// sort 'Product Number' in all tables using custom comparator
TableSort.enable('', 'Product Number', MyDomain.compareProductNumbers)
TS handles events (clicks) on its own, it is also possible to call the sorting function from your code. That's what the sort method is for.
TableSort.sort(table, column, compare)
The parameters are as above, but table and column are required.
previous work
This script is based on the works of Paul Sowden and Stuart Langridge. There is also a plenty of similar scripts already written...
comment on this