php使用js对表格进行排序

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

<!DOCTYPE>

<html>

<head>

<meta http-equiv="Content-type" content="text/html" charset="utf-8">

<title>sorttable</title>

<style>

*{

margin:0px;

padding:0px;

}

body{

background:#ccc;}

table{

width:350px;

margin:0auto;

background-color:#eee;}

table th{

cursor:hand;

padding:5px 0;

background-color:#999;}

table td{

background-color:#fff;font-size:16px;

font-weight:normal;

text-align:center;

line-height:30px;

}</style>

<script language="javascript">functionsortCells(type){vartbs=document.getElementsByTagName("table")[0];vararr=[];vararr2=[];for(vari=1;i<tbs.rows.length;i++){vartext=tbs.rows[i].cells[type].innerText;

arr.push(text);

arr2[text]=i;

}if(type==0){

arr.sort(function(a,b){returna-b});

}else{

arr.sort();

}vartemp="";for(varj=1;j<tbs.rows.length;j++){

temp=tbs.rows[j].cells[0].innerText;

tbs.rows[j].cells[0].innerText=tbs.rows[arr2[arr[j-1]]].cells[0].innerText;

tbs.rows[arr2[arr[j-1]]].cells[0].innerText=temp;

temp=tbs.rows[j].cells[1].innerText;

tbs.rows[j].cells[1].innerText=tbs.rows[arr2[arr[j-1]]].cells[1].innerText;

tbs.rows[arr2[arr[j-1]]].cells[1].innerText=temp;

temp=tbs.rows[j].cells[2].innerText;

tbs.rows[j].cells[2].innerText=tbs.rows[arr2[arr[j-1]]].cells[2].innerText;

tbs.rows[arr2[arr[j-1]]].cells[2].innerText=temp;//console.log(arr2);for(vari=1;i<tbs.rows.length;i++){vartext=tbs.rows[i].cells[type].innerText;

arr2[text]=i;

}

}

}</script>

</head>

<body>

<center>sorttable</center>

<table border="0">

<tr>

<th onclick="sortCells(0);">序号</th>

<th onclick="sortCells(1);">姓名</th>

<th onclick="sortCells(2);">日期</th>

</tr>

<tr>

<td>2</td>

<td>BB</td>

<td>2015-09-12</td>

</tr>

<tr>

<td>3</td>

<td>CC</td>

<td>2015-07-12</td>

</tr>

<tr>

<td>1</td>

<td>AA</td>

<td>2015-09-11</td>

</tr>

<tr>

<td>4</td>

<td>DD</td>

<td>2015-06-12</td>

</tr>

</table>

</body>

</html>

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航