DelphiFAQ Home Search:

Make an HTML table sortable

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:

I need to display data in a sortable HTML table. How can I do that?

Answer:

HTML itself does not offer sorting in the TABLE structure. You need to use javascript for this.

I found a free javascript that can be plugged into your site with a minimum of change of your HTML document.

Simply include the sortTable.js file into your document - ideally within the HEAD section.

Give your table a unique name and also specify the ID property with the same name.

Call the initialization initTable() with the ID of your table.
If you have multiple tables, you need to call initTable() several times.

Make sure to have no row spans.
Make sure to have no col spans in the header (you may have it in the data rows though!).

You can download the javascript code file here: sortTable.js


Here the sorted table in action - click on the table header row to sort by any column:

First name Last name Age
Bill Jones 45
Ann Moss 35
Jane Michels 40

<script src="/software/sortTable.js">

<Table id=usertable border=1 cellspacing=2 cellpadding=2>
//..
</Table>

<script language="javascript">
initTable("usertable");
</script>

Comments:

2007-09-28, 19:13:12
anonymous from United States  
the script throws an unhandled exception when my asp.net page loads. sigh.
2008-04-04, 00:43:48
anonymous from India  
In my jsp i m having table inside multiple table
for Ex
<table>
<table>
</table>
</table>
i tried with this structure, no result.Also I tried wity your sample, its working fine
can u please give me a idea?
Thank you,

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
Leave your comment here:
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.