5,696,038 members and growing! (11,946 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

Sorting HTML Tables using Javascript

By Han Yu

A simple method of making your HTML tables sortable. No additional coding necessary!
Javascript, Dev

Posted: 24 Nov 2001
Updated: 24 Sep 2002
Views: 273,165
Bookmarked: 59 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
60 votes for this Article.
Popularity: 7.47 Rating: 4.20 out of 5
2 votes, 8.7%
1
2 votes, 8.7%
2
1 vote, 4.3%
3
4 votes, 17.4%
4
14 votes, 60.9%
5

demo image

Introduction

This JavaScript code can be used to convert tables in ordinary HTML into sortable ones by associating each title column with an onClick event to automatically sort the data rows.

No additional coding is necessary. All you need to do is give your table an ID field, include the sortTable.js file and call initTable in your document's onLoad method.

<HTML>
<HEAD>
<TITLE>Sample Table</TITLE>
</HEAD>
<BODY onLoad='initTable("table0");'>

<SCRIPT SRC="sortTable.js"></SCRIPT>

<TABLE ID="table0" BORDER=1>
	<TBODY>
	<TR><TD ROWSPAN=2>NO</TD><TD COLSPAN=2>IDS</TD><TD ROWSPAN=2>Date</TD></TR>
	<TR><TD>ID</TD><TD>Device ID</TD></TR>
	<TR><TD COLSPAN=4> </TD></TR>
	<TR><TD COLSPAN=3> </TD><TD> </TD></TR>
	<TR><TD>1</TD><TD>k</TD><TD>00030</TD><TD>11/09/01 12:14:00 pm</TD></TR>
	<TR><TD>2</TD><TD>c</TD><TD>00006</TD><TD>11/11/01 12:15:00 pm</TD></TR>
	<TR><TD>3</TD><TD>a</TD><TD>00016</TD><TD>10/16/01 08:14:00 am</TD></TR>
	<TR><TD>4</TD><TD>b</TD><TD>00031</TD><TD>09/05/01 10:05:00 am</TD></TR>
</TABLE>

History

27 Nov 2001 - updated source

3 Dec 2001 - updated source to include better comments. Also changed the main engine so that the data rows with various properties can be swapped as a whole. This change makes the script more flexible

6 Dec 2001 - The script now uses delete/insertCell for all the cell manipulation. So, what does this mean? Well, it's really flexible now. It should retain all the cell properties even when the script gets applied, including those of the title cells.

18 Dec 2001 - Fixed a few more bugs and improved the script so that it can handle different types of title cells.

23 Jan 2002 - Updated source files

15 Mar 2002

  • Automatic sort upon page load
  • Handling of <THEAD> <TFOOT> tags

5 Apr 2002

  • The script now preserves most common cell properties.
  • Bug fix in isDate method.

10 Apr 2002

  • Title rows can now be in a separate table
  • CSS preserved for cells
  • Bug fix in the status caption
  • Title link events now propagated to the entire cells

25 Apr 2002

  • Removes HTML tags when comparing cell content

25 July 2002:

  • Handles Euro date format
  • Fixed up/down replacement reg exp
  • Title column hints (via TITLE) added
  • Changed up/down char to up/down arrows
  • Up/down arrows get displayed if doSortUponLoad is set
  • Added a new feature to allow skipping columns when sorting
  • Title cells can be highlighted (bgcolor chaged) onMouseOver ...

25 Sep 2002:

  • Mouse pointer becomes hourglass while sorting
  • Bug fix where sorting is mistakenly done twice if doSortUponLoad is set
  • Sort direction alternates even between tables

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Han Yu


http://www.geocities.com/sc_hanyu/
Occupation: Web Developer
Location: United States United States

Other popular Client side scripting articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 119 (Total in Forum: 119) (Refresh)FirstPrevNext
GeneralSorting by second word in a column.memberJohn Rogers16:50 24 Jun '08  
Generalhelp....?memberbakfull soulie11:18 6 May '08  
GeneralRe: help....?memberJohn Rogers16:46 24 Jun '08  
Questionsetting columns to ignorememberalastarwaterwolf14:13 21 Aug '07  
Questionforumsmemberviji270319855:25 20 Aug '07  
Generalgreat scriptmemberrodsavestuff10:01 14 Feb '07  
QuestionProblems with columns with select fieldsmembersteffersonj5:42 14 Nov '06  
Generalfixed headermemberJ512198222:29 6 Aug '06  
GeneralRe-initialization in conjunction with Filtermemberahooja7:12 28 Jun '06  
GeneralProblem with empty cellsmembersiribudigi8:44 9 May '06  
GeneralHow to sort on columns having checkbox ?memberCottonGear2:07 20 Oct '05  
GeneralDate with format DD-MON-YY is not sorting properlymemberdhan_raj2:10 14 Oct '05  
GeneralTry this script, its awesomememberS.A.S.23:16 18 Sep '05  
GeneralChanging the column that sortsmemberCrystalB2510:45 15 Feb '05  
GeneralRe: Changing the column that sortsmemberandroidik4:55 18 Mar '05  
Generalwrong css class in firefoxmemberLaubi_4:22 9 Jan '05  
Generalskip rows while sorting table. Great Effortmembersreepavanic5:11 18 Nov '04  
GeneralFormatting Table and fieldsmembergareth13516849812:09 21 Apr '04  
GeneralCells with a LinkmemberJoeStone9:20 21 Jan '04  
GeneralSorting a table on XML pagesussAnonymous10:27 26 Nov '03  
GeneralAnother bug found!!!memberZhuofei Wang16:47 18 Sep '03  
GeneralEmpty cells problemmemberDrJ222:35 10 Aug '03  
GeneralVery Slow for large datamemberamitsinha10:54 4 Aug '03  
GeneralRe: Very Slow for large datamemberStephane Rodriguez.11:25 4 Aug '03  
GeneralRe: Very Slow for large datamemberQing Jiang5:53 24 Feb '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Sep 2002
Editor: Chris Maunder
Copyright 2001 by Han Yu
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project