js表格排序实例分析(支持int,float,date,string四种数据类型)
本文实例讲述了js表格排序的方法。分享给大家供大家参考。具体如下:
<html> <head> <title>SortTable2</title> <metahttp-equiv="content-type"content="text/html;charset=UTF-8"> <scripttype="text/javascript"> vark=0; /**//************************************************************************** 排序的主方法,有三个形参,STableTd,iCol,sDataType分别为需要排序的表格ID, 需要排序的表格列号,所在列的数据类型(支持int,float,date,string四种数据类型) **************************************************************************/ functionsortTable(sTableId,iCol,sDataType) { varoTable=document.getElementById(sTableId);//获取表格的ID varoTbody=oTable.tBodies[0];//获取表格的tbody varcolDataRows=oTbody.rows;//获取tbody里的所有行的引用 varaTRs=newArray();//定义aTRs数组用于存放tbody里的行 for(vari=0;i<colDataRows.length;i++)//依次把所有行放如aTRs数组 { aTRs.push(colDataRows[i]); } /**//*********************************************************************** sortCol属性是额外给table添加的属性,用于作顺反两种顺序排序时的判断,区分 首次排序和后面的有序反转 ************************************************************************/ if(oTable.sortCol==iCol)//非首次排序 { aTRs.reverse(); } else//首次排序 { if(k%2==0)//升序 { aTRs.sort(generateCompareTRs(iCol,sDataType)); } elseif(k%2==1)//降序 { aTRs.sort(generateCompareTRs1(iCol,sDataType)); } } varoFragment=document.createDocumentFragment();//创建文档碎片 for(vari=0;i<aTRs.length;i++)//把排序过的aTRs数组成员依次添加到文档碎片 { oFragment.appendChild(aTRs[i]); } oTbody.appendChild(oFragment);//把文档碎片添加到tbody,完成排序后的显示更新 oTable.sortCol=iCol;//把当前列号赋值给sortCol,以此来区分首次排序和非首次排序,//sortCol的默认值为-1 }; //比较函数,用于两项之间的排序 //升序 functiongenerateCompareTRs(iCol,sDataType) { returnfunctioncompareTRs(oTR1,oTR2) { varvValue1=convert(oTR1.cells[iCol].firstChild.nodeValue,sDataType); varvValue2=convert(oTR2.cells[iCol].firstChild.nodeValue,sDataType); if(vValue1<vValue2) { return-1; } elseif(vValue1>vValue2) { return1; } else { return0; } }; }; //降序 functiongenerateCompareTRs1(iCol,sDataType) { returnfunctioncompareTRs(oTR1,oTR2) { varvValue1=convert(oTR1.cells[iCol].firstChild.nodeValue,sDataType); varvValue2=convert(oTR2.cells[iCol].firstChild.nodeValue,sDataType); if(vValue1>vValue2) { return-1; } elseif(vValue1<vValue2) { return1; } else { return0; } }; }; //数据类型转换函数 functionconvert(sValue,sDataType) { switch(sDataType) { case"int":returnparseInt(sValue); case"float":returnparseFloat(sValue); case"date":returnnewDate(Date.parse(sValue)); default:returnsValue.toString(); } }; </script> </head> <body> <formname="f1"id="f1"action=""method="post"> <tableborder="1"id="tblSort"sortCol="-1"> <thead> <tr> <thonClick="sortTable('tblSort',0);"style="cursor:pointer">LastName</th> <thonClick="sortTable('tblSort',1)"style="cursor:pointer">FirstName</th> <thonClick="sortTable('tblSort',2,'date')"style="cursor:pointer">Birthday</th> <thonClick="sortTable('tblSort',3,'int')"style="cursor:pointer">Silbings</th> </tr> </thead> <tbody> <tr> <td>Simth</td> <td>John</td> <td>7/12/1978</td> <td>50nGy/h</td> </tr> <tr> <td>Johnson</td> <td>Betty</td> <td>5/12/1965</td> <td>20nGy/h</td> </tr> <tr> <td>Henderson</td> <td>Nathan</td> <td>10/15/1977</td> <td>130nGy/h</td> </tr> <tr> <td>Willianms</td> <td>James</td> <td>2/25/1949</td> <td>10nGy/h</td> </tr> <tr> <td>Gilliam</td> <td>Michael</td> <td>7/8/1980</td> <td>140nGy/h</td> </tr> <tr> <td>Walker</td> <td>Matthew</td> <td>6/18/1981</td> <td>103nGy/h</td> </tr> </tbody> </table> </form> </body> </html>
希望本文所述对大家的javascript程序设计有所帮助。