1. Comparison function generator:
Copy code code as follows:
/**
* Comparison function generator
*
* @param iCol
* Number of data rows
* @param sDataType
* The data type of the row
* @return
*/
function generateCompareTRs(iCol, sDataType) {
return function compareTRs(oTR1, oTR2) {
vValue1 = convert(oTR1.cells[iCol].firstChild.nodeValue, sDataType);
vValue2 = convert(oTR2.cells[iCol].firstChild.nodeValue, sDataType);
if (vValue1 < vValue2) {
return -1;
} else if (vValue1 > vValue2) {
Return 1;
} else {
Return 0;
}
};
}
2. Process comparison character types:
Copy code code as follows:
/**
* Process the sorted field type
*
* @param sValue
* The field value defaults to character type, that is, compare ASCII codes
* @param sDataType
* Field type only supports the formats of mm/dd/yyyy or mmmm dd,yyyyy(January 12, 2004)
* @return
*/
function convert(sValue, sDataType) {
switch (sDataType) {
case "int" :
return parseInt(sValue);
case "float" :
return parseFloat(sValue);
case "date" :
return new Date(Date.parse(sValue));
default:
return sValue.toString();
}
}
3. Main function:
Copy code code as follows:
/**
* Sort table columns by table header
*
* @param sTableID
* Table ID to process <table id=''>
* @param iCol
* Field column id eg: 0 1 2 3 ...
* @param sDataType
* The field data type int, float, date is processed by default.
*/
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 question code into a js file and reference it in the html page.
Test test.html:
Copy code code as follows:
< html xmlns = "http://www.w3.org/1999/xhtml" >
< title > Table column sort</ title >
< script type = "text/javascript" src = "js/sortTable.js" > </ script >
< body >
< table border = "1" id = "tblSort" >
< thead style = "color: red; bgcolor: blank" >
< tr >
< th onclick = " sortTable('tblSort',0);" style = "cursor: pointer" > LastName </ th >
< th onclick = " sortTable('tblSort',1,'int');" style = "cursor: pointer" > Number </ th >
< th onclick = " sortTable('tblSort',2,'date');" style = "cursor: pointer" > Date </ th >
</ tr >
</ thead >
< tbody >
< tr >
< td > A </ td >
< td > 1 </ td >
< td > 5/9/2008 </ td >
</ tr >
< tr >
< td > B </ td >
< td > 3 </ td >
< td > 6/9/2008 </ td >
</ tr >
< tr >
< td > D </ td >
< td > 6 </ td >
< td > 5/4/2008 </ td >
</ tr >
< tr >
< td > E </ td >
< td > -5 </ td >
< td > 5/4/2007 </ td >
</ tr >
< tr >
< td > H </ td >
< td > 34 </ td >
< td > 5/8/2008 </ td >
</ tr >
< tr >
< td > C </ td >
< td > 12 </ td >
< td > 1/4/2018 </ td >
</ tr >
</ tbody >
</ table >
</ body >
</ html >