Home >Web Front-end >JS Tutorial >javascript table content sorting simple operation sample code_javascript skills

javascript table content sorting simple operation sample code_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:05:32826browse
Copy code The code is as follows:


<script> <br>var listInfos = new Array(); <br>listInfos[0] = new Array(); <br>listInfos[0][0] = {'name':'Recommended page 1','DayCount':666,'AvgTime':29872,'ErrCount':180663,'ErrorRate':'2873%','DaySystemErrorCount':0,'DaySystemrErrorRate':'0%'} <br>listInfos[ 0][1] = {'name':'Recommended Page 2','DayCount':593896,'AvgTime':24946,'ErrCount':222,'ErrorRate':'2%','DaySystemErrorCount':0, 'DaySystemrErrorRate':'0%'} <br>listInfos[0][2] = {'name':'Recommended Page 3','DayCount':956,'AvgTime':27957,'ErrCount':111,' ErrorRate':'10%','DaySystemErrorCount':0,'DaySystemrErrorRate':'0%'} <br>listInfos[1] = new Array(); <br>listInfos[1][0] = {'name ':'Recommended page 4','DayCount':666,'AvgTime':116,'ErrCount':180663,'ErrorRate':'2873%','DaySystemErrorCount':0,'DaySystemrErrorRate':'0%'} <br>listInfos[1][1] = {'name':'Recommended Page 5','DayCount':11,'AvgTime':222,'ErrCount':222,'ErrorRate':'2%',' DaySystemErrorCount':0,'DaySystemrErrorRate':'0%'} <br>listInfos[1][2] = {'name':'Recommended Page 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('Operation error...'); <BR>return false; <BR>} <br><br>var tableStrList =''; <BR>for( var i=0; i < listInfos.length ; i ){ <BR>var tableStr='<h1>program</h1>'; <br>tableStr = '<table width="100%" cellspacing="0" cellpadding="0" border=" 1" class="programTabble"><tbody><tr class="indexTableTr">'; <br>tableStr ='<td width="16%">Program Name</td>&lt ;td width="14%">Visits on the day (times)</td><td width="14%">Average response time (us)</td><td width="14 %">Number of errors (times)</td>'; <br>tableStr ='<td width="14%">Error rate (%)</td> <td width="14 %">Number of system errors (times)</td> <td width="14%">System error rate (%)</td> </tr> '; <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'] '</a></td>'; <br>tableStr ='<td>' listInfos[i][j ]['DayCount'] '</td>'; <br>tableStr ='<td>' listInfos[i][j]['AvgTime'] '</td>'; <br>tableStr = '<td>' listInfos[i][j]['ErrCount'] '</td>'; <br>tableStr ='<td>' listInfos[i][j]['ErrorRate'] ' </td>'; <br>tableStr ='<td>' listInfos[i][j]['DaySystemErrorCount'] '</td>'; <br>tableStr ='<td>' listInfos [i][j]['DaySystemrErrorRate'] '</td>'; <br>tableStr ='</tr>'; <br>} <br>tableStr ='</tbody></ table>'; <br>tableStrList = tableStr <br>} <br><br>document.getElementById("html").innerHTML=tableStrList; <br>} <br>sortOperation(listInfos,'DayCount','asc ') <br></script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn