Table.html
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script type= "Text/javascript" src= "Tablesortt.js" ></script>
<link type= "Text/css" rel= "StyleSheet" href= "Tablesort.css"/>
<title> Click Table header Sort </title>
<style type= "Text/css" >
Body {
Font-family:verdana, Helvetica, Arial, Sans-serif;
Font:message-box;
}
Code {
Font-size:1em;
}
</style>
<body>
<table id= "Myheadtab" cellspacing= "0" onclick= "SortColumn (event)" >
<thead>
<tr>
<td><a href= "#" >StringName</a></td>
<TD title= "caseinsensitivestring" > <a href= "#" >String</a></td>
<td> <a href= "#" >Number</a></td>
<td> <a href= "#" >Date</a></td>
<td> <a href= "#" >no sort</a></td>
</tr>
</thead>
<tbody>
<tr>
<td>apple</td>
<td>Strawberry</td>
<td>45</td>
<td>2001-03-13</td>
<td>item 0</td>
</tr>
<tr>
<td>Banana</td>
<td>orange</td>
<td>7698</td>
<td>1789-07-14</td>
<td>item 1</td>
</tr>
<tr>
<td>orange</td>
<td>Banana</td>
<td>4546</td>
<td>1949-07-04</td>
<td>item 2</td>
</tr>
<tr>
<td>Strawberry</td>
<td>apple</td>
<td>987</td>
<td>1975-08-19</td>
<td>item 3</td>
</tr>
<tr>
<td>Pear</td>
<td>blueberry</td>
<td>98743</td>
<td>2001-01-01</td>
<td>item 4</td>
</tr>
<tr>
<td>blueberry</td>
<td>Pear</td>
<td>4</td>
<td>2001-04-18</td>
<td>item 5</td>
</tr>
</tbody>
</table>
</body>
Tablesort.css
Tr{background:window;}
Td{color:windowtext; font:menu; padding:1px; padding-left:5px; padding-right:5px;
border-right:1px solid Buttonshadow;
border-bottom:1px solid Buttonshadow;
}
Table {border-top:1px solid buttonshadow;
border-left:1px solid Buttonshadow;
border-right:1px solid buttonhighlight;
border-bottom:1px solid buttonhighlight;
}
Thead td {Background:buttonface; font:menu; border:1px outset white;
padding-top:0; padding:bottom:0;
border-top:1px solid buttonhighlight;
border-left:1px solid buttonhighlight;
border-right:1px solid Buttonshadow;
border-bottom:1px solid Buttonshadow;
height:16px;
}
Thead. arrow{font-family:webdings; color:black; padding:0; font-size:10px;
height:11px; width:10px; Overflow:hidden;
Margin-bottom:5; Margin-top:-3; padding:0; padding-top:0; Padding-bottom:2;}
/*nice Vertical Positioning:-) * *
Tablesortt.js
var dom = (document.getelementsbytagname)? True:false;
var IE5 = (document.getelementsbytagname && document.all)? True:false;
var arrowup, Arrowdown;
if (IE5 | | | | DOM)
Initsorttable ();
function initsorttable () {
Arrowup = document.createelement ("SPAN");
var tn = document.createTextNode ("5");
Arrowup.appendchild (TN);
Arrowup.classname = "Arrow";
Arrowdown = document.createelement ("SPAN");
var tn = document.createTextNode ("6");
Arrowdown.appendchild (TN);
Arrowdown.classname = "Arrow";
}
function sorttable (Tablenode, Ncol, Bdesc, stype) {
var tBody = tablenode.tbodies[0];
var trs = Tbody.rows;
var trl= trs.length;
var a = new Array ();
for (var i = 0; i < TRL; i++) {
A[i] = trs[i];
}
var start = new Date;
Window.status = "sorting data ...";
A.sort (Comparebycolumn (Ncol,bdesc,stype));
Window.status = "sorting Data Done";
for (var i = 0; i < TRL; i++) {
Tbody.appendchild (A[i]);
Window.status = "Updating row" + (i + 1) + "of" + TRL +
"(Time Spent:" + (new Date-start) + "MS");
}
Check for Onsort
if (typeof Tablenode.onsort = = "string")
Tablenode.onsort = new Function ("", tablenode.onsort);
if (typeof Tablenode.onsort = = "function")
Tablenode.onsort ();
}
function caseinsensitivestring (s) {
return String (s). toUpperCase ();
}
function Parsedate (s) {
Return Date.parse (S.replace (/\-/g, '/'));
}
function Tonumber (s) {
Return number (S.replace (/[^0-9\.) /g, ""));
}
function Comparebycolumn (Ncol, bdescending, stype) {
var c = Ncol;
var d = bdescending;
var ftypecast = String;
return function (n1, N2) {
if (Ftypecast (Getinnertext (n1.cells[c)) < Ftypecast (Getinnertext (n2.cells[c)))
Return d? -1: +1;
if (Ftypecast (Getinnertext (n1.cells[c)) > Ftypecast (Getinnertext (n2.cells[c)))
Return d? +1:-1;
return 0;
};
}
function Sortcolumnwithhold (e) {
Find TABLE Element
var el = ie5? E.srcelement:e.target;
var table = getParent (el, "table");
Backup old cursor and onclick
var oldcursor = table.style.cursor;
var oldclick = Table.onclick;
Change cursor and onclick
Table.style.cursor = "Wait";
Table.onclick = null;
The event object is destroyed after this thread, but we are only need
The srcelement and/or the target
var fakeevent = {srcelement:e.srcelement, target:e.target};
Call SortColumn in a new thread to allow the UI thread to be updated
With the Cursor/onclick
Window.settimeout (function () {
SortColumn (fakeevent);
Once done resore cursor and onclick
Table.style.cursor = Oldcursor;
Table.onclick = Oldclick;
}, 100);
}
function SortColumn (e) {
var tmp = E.target? E.target:e.srcelement;
var theadparent = getParent (tmp, "THEAD");
var el = getParent (tmp, "TD");
if (theadparent = = null)
Return
if (el! = null) {
var p = El.parentnode;
var i;
Typecast to Boolean
El._descending =! Boolean (el._descending);
if (Theadparent.arrow! = null) {
if (tHeadParent.arrow.parentNode! = EL) {
theadparent.arrow.parentnode._descending = null; Reset sort Order
}
THeadParent.arrow.parentNode.removeChild (Theadparent.arrow);
}
if (el._descending)
Theadparent.arrow = Arrowup.clonenode (true);
Else
Theadparent.arrow = Arrowdown.clonenode (true);
El.appendchild (Theadparent.arrow);
Get the index of the TD
var cells = P.cells;
var L = cells.length;
for (i = 0; i < L; i++) {
if (cells[i] = = EL) break;
}
var table = getParent (el, "table");
Can ' t fail
SortTable (table,i,el._descending, El.getattribute ("type"));
}
}
function Getinnertext (EL) {
if (IE5) return el.innertext; Not needed but it is faster
var str = "";
var cs = El.childnodes;
var L = cs.length;
for (var i = 0; i < L; i++) {
Switch (cs[i].nodetype) {
Case 1://element_node
str + = Getinnertext (Cs[i]);
Break
Case 3://text_node
str + = Cs[i].nodevalue;
Break
}
}
return str;
}
function GetParent (el, Ptagname) {
if (el = = null) return null;
else if (El.nodetype = = 1 && el.tagName.toLowerCase () = = Ptagname.tolowercase ())//Gecko bug, supposed to is Uppe Rcase
Return el;
Else
Return GetParent (El.parentnode, ptagname);
}
JSP Click Table Header Sort