The original (server-side) solution
Thanks to everyone who chipped in with bug reports and helpfull suggestions. There’s a pint of Guinness in the virtual keg for all of you.
I’m currently in the process of rewriting a web-application that displays lots of tabular data. The previous application enabled users to sort the table rows by clicking on the table header text but unfortunately, passed the processing overhead back to the server, meaning the entire page was reloaded on each click… nasty.
The original applications table rows were “zebra striped” (again, this was originally handled by the server code), which means that a JavaScript solution has to also keep the order of the stripes after sorting & rearranging the table rows.
The JavaScript solution
This is a perfect example of where a little bit of JavaScript progressive enhancement can work wonders. The idea then was to write a script with identical functionality to the current system i.e. a script that can sort the table rows and then correctly “zebra-stripe” the rows after the sort without reloading the page.
Sortable datatypes
The script can currently sort dates (dd-mm-yyyy, mm-dd-yyyy or yyyy-mm-dd formats accepted), currency values ($, £ and €), numbers/floats and finally, plain-text (sorted in a case-insensitive manner). Additionally, the character used as the date divider (i.e. the character that separates the date parts) can be “/”, “-”, “.” or “ “ (space).
Zebra striping the table rows
Should the table be given a className of style-something, each alternate table row is given the className something after the table has been sorted i.e giving the table a className of style-alternate will tell the script to give each alternate row the className alternate.
Making any column sortable
To make any table column sortable, just give the associated table header (th) tag a className of sortable, the script will automatically determine the columns datatype, make the entire table header clickable (and not just the text contained within) and create the appropriate up/down arrow within the header when clicked (using the ↑ & ↓ characters).
Forcing a columns sort algorithm
Should a column contain a mix of datatypes (as shown in the demo’s second table), it is advised that you explicitly set the sort algorithm by adding one of the following classNames to the associated table header (th) tag: sortable-numeric, sortable-currency or sortable-date.
Stipulating custom sort functions
Should you wish to stipulate your own custom sort function, add the className sortable-yourCustomFunctionName to the table header (th) tag; for example, adding a className of sortable-myCustomSortFunction will tell the script to call a JavaScript function named myCustomSortFunction.
Additionally, the name of your custom function may only contain letters (a-Z) and the underscore character. Function names containing other characters will not be recognised by the script. The function name has been limited in this way as it is defined within a className, which itself can only contain a subset of the full ASCII character set i.e. while “$” is a valid JavaScript function name, it is not a valid character to use within a className).
Available custom sort functions
A few custom sort functions have been written and made available for those who might need them. They currently include:
- A function to sort by IP address
- A function to sort English longhand date formats e.g. 12th April 2006
- A function to sort twelve-hour timestamps e.g. 12:32a.m.
- A function to sort numbers stipulated in Scientific Notation e.g. -32.45 e 0.32
- A function to sort percentages (positive or negative, floats or integers)
The functions can now be seen in action on their own custom function demo page.
Sorting the table automatically
As it seems to be a common request, I’ve altered the script to automatically sort any table that has been given the className sortable-onload. Tables with this className are sorted immediately after the window.onload event fires.
Should you wish to specify the column to use, give the table a className of sortable-onload-N; where N specifies an integer that defines the column to use e.g. giving the table a className of sortable-onload-3 will initiate the automatic sort using the third sortable column located by the script.
Indicating the sort is underway
Very large tables may take a while to sort which, without some sort of feedback, may leave users slightly bewildered as to why nothing seems to be happening after they have just clicked a table header.
To enable you to provide this feedback, the TH node clicked by the user is temporarily assigned the className sort-active. This enables you to create appropriate styles that visually indicate to the user that the sort is underway (adding an animated gif to the background of the TH node for example). Also, during the sort process, the mouse cursor is changed to the “hourglass”. Of course, after the sort is complete, the className sort-active is removed from the TH node and the cursor returns to it’s normal state.
Using images for the arrows
As it’s a mighty popular request, I’ve updated the script to enable images to be used in place of the arrows. The images should be defined within your CSS as follows:
th span
{
display:none;
}
th.forwardSort
{
background:transparent url(your-down-arrow-image) no-repeat 100% 0;
}
th.reverseSort
{
background:transparent url(your-up-arrow-image) no-repeat 100% 0;
}
As can be seen, you should make sure the span (which is always created by the script) is removed from the document by setting it to display:none. You are then free to style the TH node as you wish.
If you are already using background images to style the TH nodes, it is then necessary to add the class no-arrow to the table’s className (which instructs the script to add non-breaking spaces to the span element and not the “arrow” character) and move the styling of the arrows into the span element as is shown below:
th span
{
display:inline;
width:2em;
height:1em;
}
th.forwardSort span
{
background:transparent url(your-down-arrow-image) no-repeat 50% 50%;
}
th.reverseSort span
{
background:transparent url(your-up-arrow-image) no-repeat 50% 50%;
}
Keyboard Accessibility
The script is now keyboard accessible and (thanks to Pat), has been tested using the screen-reader Jaws.
Demo, downloads and updates
Tested in Internet Explorer 6, Opera 8.5, Mozilla and Firefox.
View the table-sort demo, the custom function demo or download the source code.
14/02/2006 :
- Added the ‘sort onload’ functionality.
- Integrated John Resigs addEvent function.
15/02/2006 :
- Added the ability to ‘force’ the script to use a specified sort algorithm.
- Added a check for identical cell data (no sort is ran should all of a columns cells contain identical data).
08/03/2006 :
- Added the ability to specify which column the table is initially sorted on.
15/03/2006 :
- Added the className “sort-active” to the TH node and changed the cursor to “wait” during the sort process.
- Integrated the addEvent function into the fdTableSort object.
16/03/2006 :
- Added the ability to specify a custom sort function.
27/03/2006 :
- Added an innerText cache for tables with “cache-results” defined within the className.
30/03/2006 :
- Made the script keyboard accessible.

Previous Comments ~
Once again, your English is as good as my French… it’s actually “a f***ing word”, not “a f*** word”. Pluralise your obscenities my friend, pluralise your obscenities…
Wow you’re magic Brian…
Happy new year my man, I hope things are going well for you.
Thanks for the comment; magic isn’t something I’m called often!
Thanks for calling…
dep
The script should work with a dynamically generated table. Only the table headers need to exist on page load, not the table data. Are there any JavaScript errors being thrown ? Does the script create the arrow when the table header is clicked but not sort the rows ?
I’ve created a test page in which the tbody .innerHTML is created onload (similar to an ajax callback) and the script seems to work fine (although, using .innerHTML seems to freak Opera out). Thanks,
Brian
Couple of points, though: instead of marking every other line of the table in the HTML source with class=”alternative”, wouldn’t it be easier to trigger a default first sort (and therefore striping) on, for example, the first column the first time the page loads? It would make the markup easier to write and make adding new entries to the table easier (no need to order them when typing the page, no need to check correct “alternate”-marking).
I’m trying to get this to work but unfortunately I’m a javascript-newbie…
oh, and on the example page the first < /body >-tag should probably be a < body >-tag ;) .
Thanks for a very useful piece of code!
I actually wrote the script for a project at work that uses PHP to dynamically create the table (and therefore, dynamically create the initial “alternate” rows for me) – which is why I didn’t integrate an
onloadtable sort.It’s a good idea to keep adding the “alternate” rows by hand as users with JavaScript disactivated will still get a pretty striped table.
As for sorting the table
onload, if multiple columns have been designated “sortable”, which one should be used to initiate the sort?Should you wish to just stripe (but not sort) the tables
onload, you can add the following method to the fdTableSort object (prepare yourself for some bad textile code formatting!):N.B: Code removed by frequency decoder
Thanks for calling (and for spotting the error within the opening body tag),
Brian.
The project I’ll probably use it for is an index for some review pages – it won’t be updated with new entries very often, but the user who does this is not especially skilled in the use of HTML.
Running an initial sort on the first column allows him to add new entries according to a template at the end of the page source, and they’ll be sorted in alphabetical order automatically – this way he won’t have to find the correct place and rewrite the whole page (correcting each table entry with the striped/not striped class) each time a new item is inserted in the middle. I admit it’ll not look as pretty for people who have turned of javascript, but we figure this will be such a small percentage that it won’t matter; especially since the use of table is a vast improvement on the unordered list used before.
Thanks again for the help – keep up the good work!
I stumbled on what I would consider a bug. If a column has both text and numeric data in it, the sort doesn’t work correctly. It appears to randomly reorder to rows. Can something be done to correct this behaviour?
In fact, the script attempts to determine the columns datatype in the following order:
1. Date
2. Numeric
3. Currency
If none of the above datatypes are found, it then defaults to a case-insensitive character sort.
The type of sort used then depends on the data found within the columns first non-empty cell. If it’s numeric, the numeric sort algorithm is used (which will return ‘quirky’ results if other cells contain plain text). If it’s plain text, the case-insensitive sort is used which sorts cell data by comparing character code values. Again, this may seem to return ‘quirky’ results if the column contains numeric data but in fact, is a true representation of a character code sort.
I should really alter the script to test that every column cell has the same datatype before determining the sort algorithm used. A task for next week then!
Thanks for the comment – I’m glad that you like the script.
Brian.
@ Travis: If the column is found to contain both Numeric and Plain-text data, which algorithm should be used for the sort? It’s not uncommon for columns to look something like this:
12
34
n/a
54.3
n/a
44.7
How should the script determine the column datatype in this case? The only way I can think of is to determine if the column contains more numeric cells than plain-text cells before choosing the sort algorithm to use. This will still not work in all cases e.g. if the following column was encountered
12
n/a
n/a
n/a
3.4
The script would determine that the column contains plain-text data and not numeric.
The only other way I can think of is to enable the user to force an algorithm by giving the TH tag a specific className (‘sortable-numeric’ for example). I’ll keep you updated should I implement this change…
Brian
One other issue I ran into was with data like the following:
c:\Inetpub\wwwroot\appname\includes\TinyAjax.inc.php
/appname/includes/dummyfile.php
/appname/test.php
c:\inetpub\wwwroot\appname\anotherfile.php
I’m sure it’s the forward slashes that are throwing things off, but this column of data has the same random sort issue.
Oh, and one more….If a column has all the same data, for instance:
test
test
test
test
The script appears to also do a random sort. Could you add code to just not do anything (except maybe show the arrow) if all cells contain the exact same data?
OK, I think that’s all for now. Thanks for such quick responses!!
I’ve also added a check for identical cell data. Should the columns cells all contain identical data, the arrow is drawn but no sort is ran.
Rather suprisingly, it only took 10 lines of code to achieve both of the changes, so the scripts network footprint hasn’t risen dramatically (which was my initial worry)!
Again, thanks for the interest,
Brian.
test
test
test
And it doesn’t sort at all. The is actually an empty cell. I’m guessing since the script doesn’t see any data, it just completely ignores it. Is there any way around this one?
Again, thanks so much for your prompt response!
I’ll see what I can do to fix it (perhaps during my lunch hour today, if not, during the weekend – well, sunday morning to be precise).
Regards,
Brian.
Take care,
Brian
i.e.:
test
test
test
(blank cell)
(blank cell)
This column of data doesn’t sort even with the latest version. I realize it’s not THAT big of a deal since most of the cells are the same, but just thought I’d trow it out there.
I can’t seem to reproduce the problem you are having with empty cells. I’ve created another test page that seems to work:
Identical and empty cell test
I’ve checked the page in Firefox and IE6 and the columns sort properly – If your still experiencing problems, drop me an email…
Regards,
Brian.
Thanks SO much for this script. It’s a true blessing for our site (used at /resources/sermons/ and /resources/reviews/).
Hat’s off to you. You’re a genius. PayPal donate, anyone?
Good to hear from you!
Thanks for the kind words… unfortunately, I now can’t seem to shake the MC Hammer song from my head… Oh-oh-oh-oh-oh-oh-oh… I’m going to have to play some Bonnie Prince Billy to clear “can’t touch this” from the neural pathways.
Also, thats a rather spiffy site design – did you do it?
Take care my man,
Brian.
Regarding the design, while I would love to take credit for it, I cannot. At least not mostly. The frontpage design was put together by Cody Lindley (a good friend of mine). I took a couple liberties, but he is really responsible for the creativity there. I’m glad you like it.
Keep up the good work. Thank you so much for sharing this stuff with “the community”. After coming to your site for the first time about a week ago, I was an instant fan. One more thing: add a PayPal donate button. =)
It seems browsers have a bit of problems with that.. it takes so much processor power to sort that I get 4-5 warnings telling me the script is not responding.. but after 2-3minutes it did the sorting..
I know it’s probably crazy.. but do you have a solution to speed up things ?
Dominique
@ Dominique: Wow, 5000 lines… thats a lot of JavaScript processing! I suggest you remove the script completely and sort the data on the server. It’s probably not the answer you were looking for but in reality, it’s the only solution that will work should you wish to display the table in full (the script is pretty much as fast as it can be). Apologies.
If server-side processing isn’t an option, why dont you paginate the table i.e. split the table into more managable chunks. This way, users don’t have to download the entire table (at 5000 lines, thats a hefty network footprint). It may mean more .html files to deal with but it also means smaller downloads and, of course, that the script can be used.
Hi, not to use my 5000 lines at once I’m trying to include them within “tbody .innerHTML” (with an XMLhttprequest), but it only works in firefox.. not IE6. Your example page above also doesn’t work on IE6). It seems one can only do a replacement inside a “DIV id=..” not inside a table.
Do you have any ideas how to solve that ?
Hi Dominique,
Internet Explorer and Opera are quite finicky about dynamic row/cell creation. In fact, they require the use of the insertRow and insertCell methods and tend to freak out when innerHTML is used in this way.
Your JavaScript AJAX will have to return a JSON result that can be iterated over. I’ve changed the dynamic demo to do just that (using a fake AJAX JSON result) - it appears to work in Internet Explorer 6 now.
Have a look at the page source to see how the JSON result is added to the table. For more information on insertRow and insertCell methods, see the MS documentation
Also, ParticleTree have a very relevant article on preloading data with Ajax that you should try to read.
Hope this helps some. (N.B: The dynamic test above was only to show that the script does work with dynamically created tables – it was the use of innerHTML that made it fall over in IE and Opera, not the actual tableSort script).
Regards,
Brian.
Hi, good works !!
A question: How can I set which column must be sorting automatically ?
Bye
Matteo
@ Matteo: I’ve just added functionality to allow you to specify the column to use. Read the paragraph entitled “Sorting the table automatically” to understand how to specify the column.
Thanks for the idea - it only took two lines of code to achieve.
Regards,
Brian.
Good !! Very thanks !!
I’ll try to ask another question: Do you think is it possible to sort the table after another javascript event (an example in other part of the page) ?
Bye
Matteo
@ Matteo: It is possible, just call the Object method fdTableSort.initSort, passing a reference to the TH node of the column you wish to sort as a parameter i.e.
var TH = document.getElementById(‘aTHnode’);
fdTableSort.initSort(TH);
Hope this helps,
Brian
Now it’s Perfect !!
Thank you very much !!
Matteo
I’ve been integrating your (awesome!) script into a rails ruby on rails app recently (http://ladd00.triumf.ca:3000/list/6). My problem is similar to daniel’s, in that the tablesort script doesn’t work with a table that is loaded via ajax.
My app doesn’t just keep the headers and reload the table body, it actually reloads the entire table when you click ‘Show retired items (3)’ or ‘Show all data fields’. At that point, I’m guessing that the javascript is still pointing at the old table, which no longer exists, and doesn’t even know about the new table that’s been loaded in.
Rails provides a javascript callback that fires as soon as the ajax call is over. Would it be possible to call something in your script through that callback, instructing it to re-scan the page for the table? Or maybe there’s a better way to do this…
@ Simon: Hi Simon, you could try to call fdTableSort.init(). This should reinitialise the script and reinsert the necessary events etc onto the new TH nodes.
I would worry about Internet Explorer memory leakage though. Every time the script initialises, it adds an onClick event handler to each TH node with a class of “sortable”. If these nodes are being removed by the Ajax callback without first removing the old event handler, this could lead to memory leaks with each Ajax callback.
This is just a theory though – perhaps I’m wrong (I hope I’m wrong)!
Regards,
Brian.
This works great!
Is there a way I can remove the arrow all together?
My javascript is crap and I can’t really work it out. When I click on the table header text it causes the table header row to expand in height, which puts the formatting all out :(
I don’t know what’s causing it!
Please help
@ Matt: Just add the following CSS rule and hey presto, no arrow (it’s still created by the script but the following rule removes it from the display):
th.sortable span
{ display:none; }Regards,
Brian.
Ah!
Thanks heaps mate. Nice work, it does EXACTLY what I need. Legend!
Actually, one more thing :P
Can you exculde certain columns from being sorted?
Eg. I want to sort the first 5 columns, but exclude the next 2 columns. There’s only 8 rows
It’s no biggie, so if it’s too hard don’t worry about it.
Thanks again.
Matt
@ Matt: Columns are only sorted if they have been given one of the following classNames “sortable”, “sortable-numeric”, “sortable-currency” or “sortable-date”.
Any column that has not been given one of the listed classNames doesn’t get sorted…
regards,
Brian.
[th class=”sortable”>col1[/th>
[th class=”sortable”>col2[/th>
[th class=”sortable”>col3[/th>
[th class=”sortable”>col4[/th>
[th>col5[/th>
[th>col6[/th>
Take the above for example. By clicking on col1-4 it will sort, but I don’t want it to move the col5-6 data. It’s as if you are freezing col5-6.
Sorry I should have explained it better the first time.
@: Matt: Ah-Ha, understood!
Sorry, it would take an immense amount of code to achieve so I’m afraid I’ll have to pass on this one. Apologies…
Regards,
Brian.
No problem, I’m happy the way it is. Just had to ask :)
Thanks again!
Is there a way to change the cursor to ‘wait’ while the sorting happens? On large tables, it’s not obvious anything is happening until it happens.
Thanks for the script, it’s robust and super-easy to use
@ KenLin: I’ve altered the code to do just that (You won’t notice the cursor change within the demo as the tables are small).
Additionally, I’ve added the class “sort-active” to the TH node clicked by the user. This allows you to style the TH node any way you so desire (adding an animated gif to the background for example) while the sort is being calculated. Of course, this class is removed once the sort process has completed.
Hope this helps…
Regards,
Brian.
This looks like what I’ve been searching for!
But, your demo does work perfect with older releases of Firefox 1.0.4, “zebra striping” is broken.
The only thing I am missing is a way to specify a custom sort-function. Did I miss something?
@ Michael: I’ll look into the Zebra Striping issue (a strange issue as I’m just adding the appropriate className to the TR node). As for a custom sort function, it’s an idea I didn’t think of so it’s currently not supported.
It’s a good idea though, so, if it doesn’t take immense amounts of code to achieve, I’ll try to add the functionality…
Update: It only took one line of code to achieve, so I’ve already integrated the functionality into the source code. Hope this helps.
Thanks for the interest,
Brian.
I was wondering what I would need to add so that a visually impaired user could tab from column to column. They could then pick the column to sort. I haven’t started to implement your code because generally if I can’t tab through it won’t work for jaws users. I simply love all the features and want to use it for a few of our html table pages. Thanks. And get that donation button out there!
@ Pat: I have been wondering about this myself…
My original idea was to create a link within each sortable TH node (containing the original TH nodes text of course) that, when activated, called the appropriate JavaScript to initiate the sort but… this would involve creating a link with an HREF like href=”#” and I’m not at all sure how Jaws etc handles links of this type i.e. will Jaws reset the read point to the top of the page even if the JavaScript attempted to cancel the event (e.g. return false)?
I shall try to look into this today and if Jaws etc will respect the “stop event”, I’ll try to integrate it into the code.
As for the Paypal button, you’re the third person who has mentioned it so… I might just look into that as well (hey, it might even keep the kid in nappies/diapers for a week!).
Thanks for the interest,
Brian
Brian,
I thought I posted this earlier but I guess I didn’t finish the update.
This is what our JAWS expert told me when he checked out the script and our correspondence.
“There shouldn’t be a problem with creating a link with the href=”#” and the return false event. JAWS does a good job of keeping the focus where it should be when you use this method (as opposed to heading back to the top). ”
Thanks, Pat
@ Pat: Thanks for the Jaws info!
I've a working version that creates anchors within the sortable TH nodes (as described in my last comment) that I shall upload this weekend (sorry, I've no FTP access from work today).
It uses the onkeypress event to check for keyboard events (pressing “Enter” initiates the sort, pressing “Tab” moves to the next anchor tag as expected).
I’ll upload it as a separate demo and if you think it’s a winner, I’ll roll the changes into the main code.
Again, thanks for the Jaws info - there is suprisingly little info out there with regard to Jaws and JavaScript.
Regards,
Brian.
That’s great Brian. We’ll check it out. You’re right, there’s not much information on JAWS and JavaScript.
Brian, did you run into an obstacle? I didn’t see the demo link. Thanks. Pat
No, no obstacle – just work, babies, vicious “toy” dogs and an insane public transport system populated by an even more insane assortment of the French general public… but thats another story altogether.
Heres an initial attempt at a keyboard accessible version of the script.
I’ve only tested it so far in Firefox and IE6. Hope it plays nicely with Jaws.
Regards,
Brian.
Brian,
Here’s what our Jaws expert said.
“That works a lot better from a keyboard standpoint. In looking at the script behind it I’d make a small modification to let the JAWS user know which column they are sorting on in the title attribute itself. Usually JAWS defers to looking strictly at the title attribute or the text itself (not both) – so it’s usually safest to let ‘em know in the title attribute. Here was the line I saw in the script that I modded slightly for that problem:
Current Script:
Line 89 – a.title = “Sort on this column”;
Modified Script:
Line 89 – a.title = “Sort on ” + thtext;
Frequency coder already had a variable housing the column name (thtext) so I just substituted that for the “this column” text.”
His only other question was how would the jaws person know which way the table is sorted. He didn’t know if this was even an issue since most tables don’t tell you.
I’m ready to use the script!
Thanks.
@ Pat: Many thanks for the Jaws testing.
I’ll roll the changes/recommendations into the main source code and update the demo accordingly.
Also, I currently create the “arrow” within the anchor tag but may create it as a child of the TH node – which would make CSS styling easier to achieve.
Update: I’ve rolled the changes into the current source code - and the arrow is now created as a child of the TH node.
Regards,
Brian.
Brian,
You’re the best! We are going to start rolling the script out this week.
Pat
Great script. Just one thing is bothering me before I can roll out my project. I have a table of Films that will be shown at a festival. My main problem is sorting by time. Currently the format is in 12-hour time. I display times like this:
8:00pm, 8:00am
Now you can see my sorting issue, dealing with am/pm. Is there an easy way around this? I`m sooo close, I just have this one little problem.
BTW – I wish I found your website earlier, the stuff I could have used when I had problems… owell :)
@ theBag:The script allows you to call custom sort functions for specific columns. You could try the following:
Update: I’ve changed the demo to use this function as it’s “custom sort function” so you can see it in action.
It allows the am/pm to be written in various ways e.g. am, a.m, a.m., AM etc. It also enables you to have spaces between the hour and the am/pm parts and the hour/minute dividor can be a full-stop or colon. Additionally, it takes into acount that 12.30am is actually before 1.00am and checks to see if a non timestamp has been passed in (allowing you to place data like “Unknown” within a table cell for any film that hasn’t yet been scheduled or whose start time is currently… well, unknown…)
Regards,
Brian
Hi Brian,
thank you so much for your wonderful script. I have just one hint: If you have a table header with two rows (and some rowspans) and you sort on a column in the second row, then it does not erase the arrow any more.
Do you know how to resolve that?
Greetings,
Pete.
@ Pete: Hi Pete, in fact, the script was written to handle one table header per column – which makes sense, as if there are two columns assigned to one table header, how will the script know which of the two columns to perform a sort on, the first or the second?
You could add another classname to the TH tag, for example, “sort-column-2” (which would tell the script to sort on the second of the two columns) and alter the script accordingly.
Unfortunately, I currently haven’t got the time to attempt it myself – apologies (but it may be something I look into later).
Should I change the script to take rowspans into account, I’ll mail you an updated version.
Again, sorry I can’t be more helpful.
Update: I’ve completely misread your comment and thought colspans instead of rowspans. Unfortunately, the same response applies, I just don’t have the time to look into it.
Regards,
Brian.
Brian,
Apologies for the delay in thanking you! Thanks!
You can see my rendered table here:
http://sydfilmfest.sitesuite.ws/page/search_results.html
All I need now is to be able to sort by venue that is residing in the same column as time. But I have delegated such a task to my developer. He seems to think its possible.
Thanks again for your work.
@ theBag: Hey, no problem, glad to help…
I was at the Sydney film festival (for animated films) a few years ago that was held in the park… very good indeed. The highlight had to be Gabriel Byrne (a fellow Irishman), drunk as a skunk and dressed in a white linen suit presenting the awards. Goes to show – never let an Irishman near the “Greenroom”!
Regards,
Brian.
Hi Brian,
I just got rid of my second header row. But now I have another question: How would you sort currencies in the Continental European format?
For example: 1.000,50
Point for the thousand and comma for the cents.
Cheers,
Pete.
@ Pete: You could try to write your own custom sort function. I’ll start you off (I’m typing this live into the comment box so don’t actually know if there are any JavaScript errors)
Give the above function a whirl and get back to me if it falls over dramatically.
Regards,
Brian.
Hey decoder:
Sounds like a great script with lots of cool features.
I am planning on trying it out for the project i am doing.
Are you planning to adding scrollable feature to the tables so the header is always on the top.
Have you also done any benchmarking in terms of when this solution becomes inefficient with respect to server side sorting?
For someone that wants to use their own up/down arrow graphics, I think this does the trick:
arrow = document.createElement('img');arrow.setAttribute('src','/images/sortup.gif');...
arrow = document.createElement('img');arrow.setAttribute('src','/images/sortdown.gif');...
span.appendChild(arrow);@ Hariharan: I don’t think I’m going to add the sticky header feature as I want to keep the script as light as possible. A static header technique has recently been shown over at Cody Lindley’s site that you may be interested in (which involves no JavaScript).
As for benchmarking… it all depends on the type of data within the table (dates take longer than Integers to sort etc). No JavaScript solution that I have seen can handle really large tables, which indeed, might be better off sorted server side.
@ Matt: I’ve a new version of the script that enables you to define your arrow images within the CSS. This way, I’m not creating hardcoded images within the JavaScript and changing the arrows can be achievied by changing the stylesheet (which might be usefull for people using stylesheet switchers). I’ll try to get the new version of the script up this week.
Thanks for the comments.
Update: I've changed the script to enable images to be defined within the CSS.
Regards,
Brian.
Hi,
looks great, is a how to or a step by step example for “web starter” aviable?
especial: the way to implement the script in a existing php file with a table.
Thanks a lot,
Gregor
I implemented this in 1 minute, in a table generated by php, very easy !!!
Thank you sooooo much
@ Gregor: Hi Gregor, I currently don’t have the time to write a full “how-to” tutorial (I’m just back from vacation and have a two week backlog of work to get through – nice!). You should view the page source of the demo to see how to include the necessary javascript file within your HTML page and also how to designate table columns as sortable (check out the classnames on the TH tags).
If your really stuck, send me the PHP code that creates the table and I’ll get it working for you…
P.S. As it’s a 100% JavaScript solution, it makes no difference to the script if the table is created by PHP/ASP etc, the script just needs to find the correct classnames within the TH tags.
Apologies for the rather unhelpful reply, I shall add the tutorial to my TODO list for future reference though!
@DarkSide: Thanks, glad it helped you out!
Hi Brian,
First of all….THANKS!!! Love this script Before this I was still doin’ serverside sorting.
I’ve seen a couple op those, but so far this one was the only one where you could choose if a column has to be sortable or not, so again thank you very much.
Best regards, Nils
If you don’t mind, I’ll drop a question.
I’m trying the feature were I can choose my own image(s) instead of the arrow(s)
So I add the extra lines in the stylesheet as you described, but nothing happened. Not the regular arrows or the images show up.
You can see my example at: http://www.vanzijderveld.nl/sorttable/table-sort.htm
Did I miss something?
Thanks in advanced
@ Nils: Hi, glad the script is of use!
I think (fingers crossed) the problem lies with the css, try changing the last three rules to read:
table.generique th span
display:none;{
}
table.generique th.forwardSort
background:transparent url(sort_down.gif) no-repeat 100% 0;{
}
table.generique th.reverseSort
background:transparent url(sort_up.gif) no-repeat 100% 0;{
}
i.e. add the “table.generique”.
If this doesn’t solve the problem then leave another comment and I’ll look into it for you (perhaps it’s a bug in the code).
Regards,
Brian
Hi Brian,
Thanks for the quick response!
Your suggestion makes the images show! So that’s allright. But now something else comes up:
If you sort on a column and you sort the same column again (ASC/DESC) and THEN you sort on another column, the image on the first column will stay!!!
So at this point, it is possible to show on all columns-headers the “sort-image” (By clicking twice on each column)
It looks like a small bug to me???
You can test it again on: http://www.vanzijderveld.nl/sorttable/table-sort.htm
Anyway, thanks so far!
Nils
@ Nils: Yep, I spotted that one myself… the culprit is the following line within the code (line 154):
th.className = th.className.replace(‘reverseSort’,’’);
It should read:
th.className = th.className.replace(/forwardSort|reverseSort/g,’’);
I’ll upload the changed code a.s.a.p
thanks again,
Brian
I am currently using your javascript on a table.
the table has a lot of contents to it.
it takes 8 seconds to load and the hourglass isn’t being displayed
could you either answer why the hourglass doesn’t work or give me some sample code to add a
.gif to the header column while the sorting in being processed
thanks
Yep, that’ll do the trick!
Thank you very, very much for the quick response!
Greetings from the Nertherlands :)
Nils
Edit: No problem Nils, glad to be of help. I should say to all and sundy though that the CSS used for the demo was taken lock-stock-and-barrel from an application I'm currently writing at work - which is why the "table.generique" references exist.
The use of this CSS declaration is not necessary and in fact, I suggest that it's removed from the CSS alltogether.
@ Blake:
Q: Why isn’t the hourglass being displayed?
A: I have no idea.
Q: Give me some sample code to add a .gif to the header column while the sorting in being processed.
A: The class “sort-active” is added to the TH node during the sort. Change your CSS to reference the .gif e.g.
th.sort-active {
background: transparent url(yourImage.gif) no-repeat 100% 50% !important;
}
regards,
Brian.
Hi Brian,
I’m back again (hope you don’t mind)
After testing I noticed that the “sortable-date” class is sorting on mm/dd/yyyy or yyyy/mm/dd.
But what if I have the european format dd/mm/yyyy or even dd/mm/yy
In this format the sort-function is not correct?
Is it possible that you could somewhere declare a specific format?
Hope you can help again?
Greetings from the Dutch,
Nils
@ Nils: You could write your own custom sort function or change the current one to test for the European format before the American format within the method “dateFormat” (it currently tests in the following order mm-dd-yyyy, dd-mm-yyyy and finally yyyy-mm-dd).
Regards,
Brian.
I am having an issue on a colimun that is using sortable numeric class when it sorts 0 and negative numers it puts 0 below negative numbers have you seen this before
@ Travis: Hi Travis, it was a really dumb piece of logic within the sortNumeric method. I’ve changed the method in question (and also fixed the same error within the sortCurrency method). Things should work as intended now.
Regards,
Brian.
Hi Brian
I’m a beginner when it comes to javascript and i haven’t worked with css before
i was just wondering, the sample css code you wrote for me, where should it be enter in your code?
Thanks
Blake
@ Blake: Hi Blake, I suggest you view the demo source code.
You will see that the CSS declarations for the page are all encapsulated within “style” tags within the “head” of the HTML document e.g.
[style type=”text/css”]
{ border-left:1px solid #C1DAD7; font-size:1em; width:96%; border-spacing: 0px; empty-cells:show; margin:0 auto 1em auto; text-align:left; padding:0; }table.generique
... lots more CSS …
[/style]
If this is how you are adding CSS to your page, just add the new CSS rule to the end of the list e.g.
[style type=”text/css”]
... the original CSS …
table.generique th.sort-active {
background: transparent url(yourImage.gif) no-repeat 100% 50% !important;
}
[/style]
If you are using external stylesheets, just add the new rule to the external stylesheet.
If your stuck, send me the HTML page (to the email address shown within the site footer) and I’ll get it working for you.
Hope this helps,
Brian.
P.S. Blake, you previously said that your table takes up to 8 seconds to sort. To be honest, I think that tables of this magnitude are better off sorted on the server as an 8 second wait equates to a terrible user experience. Just my 2 cents!
That did the trick can you tell me how to modify the script so the first time it sorts it does descending first for numeric values.
Thank you so much this script is amazing
Hi Brian,
Yeah, you’re right, I change the sequence of the dateformat and that was it!
Thanks again for pointing it to me!
good luck,
Nils
@ Travis: Thats a tricky one. The script sorts in descending order by first sorting in ascending order and then simply reversing the sort (using the .reverse method of the array object) so I think you will have to try to tinker with the code to get this to work.
@ Nils: No problem, it’s probably something I should have documented anyway.
Hi Brian
This is an awesome piece of code!
I am battling to get the 'hour glass' to work. Here is the last few lines of my css file:
Edit: CSS removed by frequency decoder
Any ideas?
Thanks,
James
Update: Hi James, I've updated the code with a fix for the hour glass problem.
hey brian
this code i think does exactly what i need , but i experienced a problem when i downloaded it, i recieved a microsoft script host error “window” is not defined”. has this happened to anyone else?
@ Michael: It sounds as if you attempted to “execute” the .js file that you downloaded. As it’s not included within an HTML document at this stage, Windows will display the error in question.
Don’t worry, it’s not an error with the script!
Regards,
Brian.
great code, just wondering if its possible to get it working with a php table that has recordset paging.. i have got it working when all records are shown in the recordset but when i show only 10 for example, the sort only sorts through those ten, i would like to sort the whole recordset,
any ideas?
cheers
John
@ John: The script can only sort table rows that are already present within the HTML. A script that deals with a paginated table will require some sort of Ajax request to the server etc, something that this script was never meant to accomplish.
You could always create the entire table within the HTML and only show 10 rows at a time (hiding the other rows with the CSS style display:hidden; for example) but this would require a complete rewrite of the current script.
Sorry I can’t be more help.
Regards,
Brian.
Hi, I need that the last row in the table remaining the LAST after any column sort, is it possible?. I’m a newbie in javascript, but if you can recognize the total rows is possible to sort total-1 rows???
@ Dany: Hi Dany, just wrap the last row within a TFOOT and the script will leave it alone.
Thanks for the comment,
Brian.
Hi,
First of all I have to compliment you for the great work you’ve done here. I have been testing the script this afternoon but could not make exactly what I wanted to do.
Here is the question: how can I make the table be striped when the page is displayed without having to make a “sort” call. In fact the way the table is displayed without any sorting is the way I want it to be on load. So if I put “sortable-onload” it will sort the table (and hence stripe it ) yet will not be displayed as I want it to be initially.
Thank you very much for any suggestion, and again great work.
@ Jak: Hi, unfortunately, the zebra-striping and sort actions are both contained within the same method so it’s currently impossible to just stripe the table onLoad. Should you wish to only zebra-stripe the table (and have no need at all for the sort), you should check out the 15 Days of jQuery site that has this functionality written in under 3 lines of code.
If your JavaScript savvy, you could also change the current script to contain the striping mechanism within it’s own method (it’s the last 4 lines of code within the initSort method).
If you email me, I can alter the code for you and send you on the new version as an attachment.
Hope this helps,
Brian.
God bless you
Brian,
if have a column with these values: A1, A100, A23, A230 …
Currently, the cool script is sorting it like that: A100, A1, A230, A23 …
How would you customize the script to have this order: A1, A23, A100, A230 …
Regards,
Pete.
@ Pete: Hi Pete, it’s just a simple matter of removing the leading “A”. Try this (written directly into the comment box so it might just have syntax errors!):
function sortPete(a, b) {
// Get the innerText of the TR nodes var aa = fdTableSort.getInnerText(a.getElementsByTagName(“td”)[fdTableSort.pos]); var bb = fdTableSort.getInnerText(b.getElementsByTagName(“td”)[fdTableSort.pos]); // Replace anything but numbers aa = aa.replace(/[^0-9]/g,””); bb = bb.replace(/[^0-9]/g,””); if(isNaN(aa)) return -1; else if(isNaN(bb)) return 1; return aa-bb;}
Hope this helps,
Brian
Thanks Brian,
and sorry for my imprecise explanation. My column has values like:
A1, B200, A150, B2 …
And the script should sort it like: A1, A150, B2, B200 …
Do you also know how to solve this?
Pete.