* @param sDataType
* The data type of this field is int, float, date. By default, it is processed as a string
*/
function sortTable(sTableID, iCol, sDataType) {
var oTable = document.getElementById(sTableID);
var oTBody = oTable.tBodies[0];
var colDataRows = oTBody .rows;
var aTRs = new Array;
for ( var i = 0; i < colDataRows.length; i ) {
aTRs[i] = colDataRows[i];
}
if (oTable.sortCol == iCol) {
aTRs.reverse();
} else {
aTRs.sort(generateCompareTRs(iCol, sDataType));
}
var oFragment = document.createDocumentFragment();
for ( var j = 0; j < aTRs.length; j ) {
oFragment.appendChild(aTRs[j]);
}
oTBody. appendChild(oFragment);
oTable.sortCol = iCol;
}
Encapsulate the above code into a js file and reference it in the html page.
Test test.html:
< html xmlns = "http://www.w3.org/1999/xhtml" >
< title > Table column sorting title >
< script type = "text/javascript" src = "js/sortTable.js" > script >
< body >
< table border = "1" id = "tblSort" >
< thead style = " color: red; bgcolor: blank" >
th onclick = " sortTable('tblSort',2,'date');" style = "cursor: pointer" > Date th >
tr >
thead >
;
td > 5/9/2008 td >
; tr >
< ; td > 3 td >
< td > D td >
< ;/ tr >
< td > 5/4/2007 td >
< td > 34 td >
< ; td > C td >
; td > 12 td >
< / tr >
tbody >
table >
body >
html >