JSP 前端数据本地排序实例代码前端数据本地排序实例代码
在前端中我们经常需要数据的排序,首先写引入我写好的js
$(function($) {
$('#sclazzId').val($('#voId').val());
document.getElementsByName('nameup')[0].style.display = "none";
document.getElementsByName('nameup')[1].style.display = "none";
document.getElementsByName('nameup')[2].style.display = "none";
document.getElementsByName('nameup')[3].style.display = "none";
var tabobj = document.getElementById("tbValue");
/*
* for (var i = 2; i < tabobj.rows.length; i++) {
*
* if (tabobj.rows[i].cells[1].innerHTML == code) {
*
* tabobj.rows[i].style.backgroundColor = "red";
*
* break; } }
*/
});
function OnChange(code) {
// alert(document.getElementById("drpIndustry").value+"_"+code+".html");
window.location.href = document.getElementById("drpIndustry").value + "_"
+ code + ".html";
}
// 转换器,将列的字段类型转换为可以排序的类型:String,int,float
function convert(sValue, sDataType) {
switch (sDataType) {
case "int":
if (sValue != "--")
return parseInt(sValue);
else
return -10000000000000;
case "float":
if (sValue != "--")
return parseFloat(sValue);
else
return -10000000000000.0;
case "date":
if (sValue != "--")
return new Date(Date.parse(sValue));
else
return "1900-01-01";
default:
return sValue.toString();
}
}
// 排序函数产生器,iCol表示列索引,sDataType表示该列的数据类型
function generateCompareTRs(iCol, sDataType) {
return function compareTRs(oTR1, oTR2) {
var vValue1 = convert(oTR1.cells[iCol].firstChild.nodeValue, sDataType);
var vValue2 = convert(oTR2.cells[iCol].firstChild.nodeValue, sDataType);
if (vValue1 < vValue2) {
return -1;
} else if (vValue1 > vValue2) {
return 1;
} else {
return 0;
}
};
}
// 排序方法
function sortTable(sTableID, iCol, sDataType) {
if (document.getElementsByName('nameup')[parseInt(iCol) - 1].style.display == 'none') {