Making a Sortable Table with JavaScript

asked Apr 26, 2012 in General by darkshark (1,500 points)
Hi all
I’ve been looking for a script to sort JavaScript so that when the user clicks on the column header, the table sorts on that column and if the user click again it sorts the other way. I’ve came across a few scripts but none of them worked the exact way I just described. If anyone is familiar with a script that could do this I would really appreciate it if you can share it with me.

2 Answers

0 like 0 dislike
answered Apr 26, 2012 by lightSpeed (2,420 points)
Hi there

This should help

•Load the sortabletable.js file into your site.
•Reference the script in the <head> section of your pages.

•Wrap the row that contains your column headers with <thead></thead> tags.
•Make sure the row with the colum headers used <th></th> tags instead of <td></td>.
•Wrap the rows that contain your table data with <tbody></tbody> tags.
•Add an id="MyTable" attribute to the <table> tag for each table you want sorted.  Of course, the id must be unique on the page.
•Place the sortable table object immediately after each sortable table.  It's shown in the sample code on the site, easy to place with a simple cut and paste, but remember to replace the 'MyTable' string with the id you used in that table's <table> tag.
0 like 0 dislike
answered Apr 26, 2012 by mike231 (2,480 points)