Rumah  >  Artikel  >  hujung hadapan web  >  javascript 表格内容排序 简单操作示例代码_javascript技巧

javascript 表格内容排序 简单操作示例代码_javascript技巧

WBOY
WBOYasal
2016-05-16 17:05:32791semak imbas
复制代码 代码如下:


<script> <BR>var listInfos = new Array(); <BR>listInfos[0] = new Array(); <BR>listInfos[0][0] = {'name':'推荐页1','DayCount':666,'AvgTime':29872,'ErrCount':180663,'ErrorRate':'2873%','DaySystemErrorCount':0,'DaySystemrErrorRate':'0%'} <BR>listInfos[0][1] = {'name':'推荐页2','DayCount':593896,'AvgTime':24946,'ErrCount':222,'ErrorRate':'2%','DaySystemErrorCount':0,'DaySystemrErrorRate':'0%'} <BR>listInfos[0][2] = {'name':'推荐页3','DayCount':956,'AvgTime':27957,'ErrCount':111,'ErrorRate':'10%','DaySystemErrorCount':0,'DaySystemrErrorRate':'0%'} <BR>listInfos[1] = new Array(); <BR>listInfos[1][0] = {'name':'推荐页4','DayCount':666,'AvgTime':116,'ErrCount':180663,'ErrorRate':'2873%','DaySystemErrorCount':0,'DaySystemrErrorRate':'0%'} <BR>listInfos[1][1] = {'name':'推荐页5','DayCount':11,'AvgTime':222,'ErrCount':222,'ErrorRate':'2%','DaySystemErrorCount':0,'DaySystemrErrorRate':'0%'} <BR>listInfos[1][2] = {'name':'推荐页6','DayCount':956,'AvgTime':956,'ErrCount':111,'ErrorRate':'10%','DaySystemErrorCount':0,'DaySystemrErrorRate':'0%'} <BR>function dateDesc(listInfos,field){ <BR>for( var i=0; i < listInfos.length ; i++ ){ <BR>for( var j = i+1 ; j < listInfos.length ; j++ ){ <BR>if( isCommaPercent(listInfos[i][field]) < isCommaPercent(listInfos[j][field]) ){ <BR>var arrayTemp = new Array(); <BR>arrayTemp = listInfos[i]; <BR>listInfos[i] = listInfos[j]; <BR>listInfos[j] = arrayTemp; <BR>} <br><br>} <BR>} <BR>return listInfos; <BR>} <BR>function dataAsc(listInfos,field){ <BR>for( var i=0; i < listInfos.length ; i++ ){ <BR>for( var j = i+1 ; j < listInfos.length ; j++ ){ <BR>if( isCommaPercent(listInfos[i][field]) > isCommaPercent(listInfos[j][field]) ){ <BR>var arrayTemp = new Array(); <BR>arrayTemp = listInfos[i]; <BR>listInfos[i] = listInfos[j]; <BR>listInfos[j] = arrayTemp; <BR>} <br><br>} <BR>} <BR>return listInfos; <BR>} <br><br>function isCommaPercent(value){ <BR>var valueFloat; <BR>value = value.toLocaleString(); <BR>valueFloat = ( value.indexOf(',') > 0 )? value.split(',').join(''):value; <BR>valueFloat = (valueFloat.indexOf('%') > 0)?parseFloat(valueFloat.substr(0,valueFloat.indexOf('%'))): parseFloat(valueFloat); <BR>return valueFloat; <BR>} <br><br><br><br>function sortOperation(sortInfos,field,sort){ <BR>var listInfos = new Array(); <BR>if( sort == 'desc' ){ <BR>for(var i=0; i < sortInfos.length ; i++ ){ <BR>listInfos[i] = dateDesc(sortInfos[i],field); <BR>} <BR>}else if( sort == 'asc' ){ <BR>for(var i=0; i < sortInfos.length ; i++ ){ <BR>listInfos[i] = dataAsc(sortInfos[i],field); <BR>} <BR>}else{ <BR>alert('操作错误...'); <BR>return false; <BR>} <br><br>var tableStrList =''; <BR>for( var i=0; i < listInfos.length ; i++ ){ <BR>var tableStr='<h1>程序'; <BR>tableStr+= '<table width="100%" cellspacing="0" cellpadding="0" border="1" class="programTabble"><tbody><tr class="indexTableTr">'; <BR>tableStr +='<td width="16%">程序名称<td width="14%">当天访问量(次)<td width="14%">平均响应时间(us)<td width="14%">错误数(次)'; <BR>tableStr +='<td width="14%">错误率(%) <td width="14%">系统错误数(次) <td width="14%">系统错误率(%) '; <BR>for( var j = 0 ; j < listInfos[i].length ; j++ ){ <BR>tableStr +='<tr>'; <BR>tableStr +='<td><a href="detail.php?type=programs&pid=1">'+listInfos[i][j]['name']+''; <BR>tableStr +='<td>'+listInfos[i][j]['DayCount']+''; <BR>tableStr +='<td>'+listInfos[i][j]['AvgTime']+''; <BR>tableStr +='<td>'+listInfos[i][j]['ErrCount']+''; <BR>tableStr +='<td>'+listInfos[i][j]['ErrorRate']+''; <BR>tableStr +='<td>'+listInfos[i][j]['DaySystemErrorCount']+''; <BR>tableStr +='<td>'+listInfos[i][j]['DaySystemrErrorRate']+''; <BR>tableStr +=''; <BR>} <BR>tableStr +=''; <BR>tableStrList += tableStr <BR>} <br><br>document.getElementById("html").innerHTML=tableStrList; <BR>} <BR>sortOperation(listInfos,'DayCount','asc') <BR></script>
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn