Click here to Skip to main content
15,886,258 members
Articles / Programming Languages / Javascript
Article

Sorting HTML Tables using Javascript

Rate me:
Please Sign up or sign in to vote.
4.68/5 (30 votes)
24 Sep 20022 min read 643.7K   11.4K   75   132
A simple method of making your HTML tables sortable. No additional coding necessary!

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
<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>&nbsp;</TD></TR>
	<TR><TD COLSPAN=3>&nbsp;</TD><TD>&nbsp;</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


Written By
Web Developer
United States United States
http://www.geocities.com/sc_hanyu/

Comments and Discussions

 
GeneralCells with links in data Pin
Dan Hogan12-Jun-03 12:15
Dan Hogan12-Jun-03 12:15 
GeneralRe: Cells with links in data Pin
Julia Belonojko8-Jul-03 8:54
sussJulia Belonojko8-Jul-03 8:54 
GeneralRe: Cells with links in data Pin
Dan Hogan8-Jul-03 9:03
Dan Hogan8-Jul-03 9:03 
QuestionRe: Cells with links in data Pin
ken50423-Nov-05 20:42
ken50423-Nov-05 20:42 
GeneralSeparate table for titles Pin
ekempter20-May-03 7:49
ekempter20-May-03 7:49 
GeneralRow properties Pin
Alexander Blood3-Dec-02 11:20
Alexander Blood3-Dec-02 11:20 
GeneralRe: Row properties Pin
Han Yu3-Dec-02 14:26
Han Yu3-Dec-02 14:26 
GeneralRe: Row properties Pin
Alexander Blood4-Dec-02 10:48
Alexander Blood4-Dec-02 10:48 
I am trying to mimic the way you moved the properties of the cell but with the rows:

I modified the initTable this way...:
....
....
....
if (! doneftable && separateTitle && titleFound)
{
ctable = table;
nRow = table.rows.length;
i = 0;
doneftable = true;
}
rowPropertyDataArray[actualNRow] = "";
currentRow = ctable.rows[i];
cRowContent = String(currentRow.innerHTML);
for (var k=0; k<rowPropertyArray.length; k++)
{
if (k == 0)
cmd = "cRowSetting=" + "String(currentRow." + rowPropertyArray[k] + ");"
else
cmd = "cRowSetting += cellPropDelimiter + " + "String(currentRow." + rowPropertyArray[k] + ");"
eval(cmd);
}

rowDataArray[actualNRow] = cRowContent + recDelimiter + String(actualNRow);;
rowPropertyDataArray[actualNRow] = cRowSetting;

skipRow = false;
// Skip if it's THEAD, TFOOT
if (ctable.rows[i].parentNode != null)
{
....
....
....


and I modified the sortTable this way:
....
....
// Re-drawing the table
rowCount = 0;
for (var i=0; i<nRow; i++)
{

if (i > 0 && rowCount != 0) {
rowContent = rowArray[rowCount].split(recDelimiter);
rowIndex = rowContent[maxNCol];
cellxProp=rowPropertyDataArray[rowIndex].split(cellPropDelimiter);
xx="";
for (var k=0; k<cellPropArray.length; k++)
{
xx += "x.";
cmd = "table.rows[i]." + rowPropertyArray[k] + "=cellxProp[" + k + "];";
eval(cmd);
}
alert(xx);
}
if (! colSpanArray[i])
{
for (var j=0; j<maxNCol; j++)
{
// Skips the columns if set to ignore
....
....



I think it should work but it does not. Any ideas?

GeneralRe: Row properties Pin
netclectic14-Nov-03 1:07
netclectic14-Nov-03 1:07 
GeneralRe: Row properties Pin
Alexander Blood14-Nov-03 4:28
Alexander Blood14-Nov-03 4:28 
GeneralRe: Row properties Pin
netclectic14-Nov-03 4:32
netclectic14-Nov-03 4:32 
GeneralRe: Row properties Pin
Alexander Blood14-Nov-03 4:39
Alexander Blood14-Nov-03 4:39 
GeneralRe: Row properties Pin
netclectic16-Nov-03 23:31
netclectic16-Nov-03 23:31 
GeneralRe: Row properties Pin
Alexander Blood17-Nov-03 9:54
Alexander Blood17-Nov-03 9:54 
QuestionHour glass & Netscape? Pin
John Rogers31-Oct-02 22:50
John Rogers31-Oct-02 22:50 
AnswerRe: Hour glass & Netscape? Pin
John Rogers6-Dec-02 13:10
John Rogers6-Dec-02 13:10 
AnswerRe: Hour glass &amp; Netscape? Pin
Anonymous24-Apr-04 7:12
Anonymous24-Apr-04 7:12 
AnswerRe: Hour glass &amp; Netscape? Pin
Jianjun Bai8-Nov-04 10:31
Jianjun Bai8-Nov-04 10:31 
Questionhas stopped working in IE5-Mac? Pin
cammac220-Sep-02 5:36
cammac220-Sep-02 5:36 
AnswerRe: has stopped working in IE5-Mac? Pin
Han Yu20-Sep-02 20:06
Han Yu20-Sep-02 20:06 
GeneralRe: has stopped working in IE5-Mac? Pin
Member 81191610-Jan-04 17:42
Member 81191610-Jan-04 17:42 
GeneralDisplay hourglass during sorting Pin
John Rogers4-Sep-02 16:54
John Rogers4-Sep-02 16:54 
GeneralRe: Display hourglass during sorting Pin
4-Sep-02 18:22
suss4-Sep-02 18:22 
GeneralRe: Display hourglass during sorting Pin
John Rogers4-Sep-02 19:13
John Rogers4-Sep-02 19:13 
GeneralRe: Display hourglass during sorting Pin
4-Sep-02 20:03
suss4-Sep-02 20:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.