排序代碼
this.oTable=document.getElementById(sTableID);
this.oTBody=this.oTable.tBodies[0];
this.colDataRows=this.oTBody.rows;
this.aTRs= [];
this.iCol=iCol;
this.sDataType=sDataType;
}
SortTable.prototype={
convert:function(sValue, sDataType){
switch( sDataType){
case "int":
return parseInt(sValue);
case "float":
return parseFloat(sValue);
case "date":
回傳新的Date(sValue);
預設:
回傳sValue.toString();
}
},
generateCompareTRs:function(iCol, sDataType, that){
回傳函數(oTR1,oTR2){
var vValue1= that.convert(oTR1.cells[iCol) ].firstChild.nodeValue, sDataType),
vValue2= that.convert(oTR2.cells[iColno].firstChild.nodede , sDataType);
if(vValue1 回傳-1;
} else if(vValue1 > vValue2){
return 1;
} 其他{
回傳0 ;
}
};
},
sort:function(){
for(var i=0,l=this.colDataRows.length;ithis.aTRs.push(this. colDataRows[i]);
}
if(this.oTable.sortCol === this.iCol){
this.aTRs.reverse();
} else {
this.aTRs.sort(this.generateCompareTRs(this.iCol, this.sDataType, this));
}
var oFragment=document.createDocumentF 但forment(); (var i=0,l=this.aTRs.length;ioFragment.appendChild(this.aTRs[i]);
}
this.oTBody.appendChild(oFragment (程式碼
程式碼如下:
函數bindSortTable(sTableID, iCol, sDataType){
var table=document.getElementById(sTableID),
var table=document.getElementById(sTableID),
}
}
window.onload=function(){
bindSortTable("tblSort", 0);
bindSortTable("tblSort",1);
bindSortTable("tblSort",2,"int");
bindSortTable("tblSort",3,"float");
bindSortTable("tblSort",3,"float");
bindSortTable("tblSort",4,"日期");
}
完整示範:
JSCode 示範
來源:http://artwl.cnblogs.com