用Javascript进行简单的Table点击排序


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>K2046.TableSorter</title>
<style type="text/css">...
body{...}{font-size:12px;line-height:25px;}
tr{...}{height:25px;}
th{...}{font-size:14px;border-bottom:solid 1px #DDDDDD;background-color:#EEEEEE;}
td{...}{font-size:12px;text-align:center;}
.SortDescCss{...}{background-image:url(http://k2046.cn/tb/Desc.gif);background-repeat:no-repeat;background-position:right center;}
.SortAscCss{...}{background-image:url(http://k2046.cn/tb/Asc.gif);background-repeat:no-repeat;background-position:right center;}
</style>
<script language="javascript" type="text/javascript" src="http://www.cnblogs.com/Files/robot/TableSorter.js"></script>
</head>
<body>
代码:new TableSorter("tb1");<br />
效果:点击任意表头可执行排序.<br />
<table border="0" style="border:solid 1px #DDDDDD;width:360px;" cellpadding="0" cellspacing="0" id="tb1">
<tr>
<th>序号</th>
<th>姓名</th>
<th>工号</th>
<th>职位</th>
<th>性别</th>
<th>业绩</th>
<th>报到时间</th>
</tr>
<tr>
<td>1</td>
<td>刘德华</td>
<td>8008</td>
<td>总裁</td>
<td>男</td>
<td>98</td>
<td>2008-04-12</td>
</tr>
<tr>
<td>2</td>
<td>吴奇隆</td>
<td>8004</td>
<td>主管</td>
<td>男</td>
<td>80</td>
<td>2008-04-16</td>
</tr>
<tr>
<td>3</td>
<td>王光良</td>
<td>8003</td>
<td>经理</td>
<td>男</td>
<td>85</td>
<td>2008-04-15</td>
</tr>
<tr>
<td>4</td>
<td>张学友</td>
<td>8009</td>
<td>副总裁</td>
<td>男</td>
<td>90</td>
<td>2008-04-11</td>
</tr>
<tr>
<td>5</td>
<td>张柏芝</td>
<td>8005</td>
<td>助理</td>
<td>女</td>
<td>78</td>
<td>2008-04-13</td>
</tr>
<tr>
<td>6</td>
<td>陈冠希</td>
<td>8001</td>
<td>总监</td>
<td>男</td>
<td>60</td>
<td>2008-04-18</td>
</tr>
<tr>
<td>7</td>
<td>陈慧琳</td>
<td>8002</td>
<td>试用期</td>
<td>女</td>
<td>85</td>
<td>2008-04-18</td>
</tr>
<tr>
<td>8</td>
<td>张曼玉</td>
<td>8007</td>
<td>高级经理</td>
<td>女</td>
<td>82</td>
<td>2008-04-16</td>
</tr>
<tr>
<td>9</td>
<td>周润发</td>
<td>8006</td>
<td>副总裁</td>
<td>男</td>
<td>88</td>
<td>2008-04-13</td>
</tr>
</table>
<br />
代码:new TableSorter("tb2", 0, 2, 5, 6);<br />
效果:点击表头0,2,5,6列可执行排序.<br />
<table border="0" style="border:solid 1px #DDDDDD;width:360px;" cellpadding="0" cellspacing="0" id="tb2">
<tr>
<th>序号</th>
<th>姓名</th>
<th>工号</th>
<th>职位</th>
<th>性别</th>
<th>业绩</th>
<th>报到时间</th>
</tr>
<tr>
<td>1</td>
<td>刘德华</td>
<td>8008</td>
<td>总裁</td>
<td>男</td>
<td>98</td>
<td>2008-04-12</td>
</tr>
<tr>
<td>2</td>
<td>吴奇隆</td>
<td>8004</td>
<td>主管</td>
<td>男</td>
<td>80</td>
<td>2008-04-16</td>
</tr>
<tr>
<td>3</td>
<td>王光良</td>
<td>8003</td>
<td>经理</td>
<td>男</td>
<td>85</td>
<td>2008-04-15</td>
</tr>
<tr>
<td>4</td>
<td>张学友</td>
<td>8009</td>
<td>副总裁</td>
<td>男</td>
<td>90</td>
<td>2008-04-11</td>
</tr>
<tr>
<td>5</td>
<td>张柏芝</td>
<td>8005</td>
<td>助理</td>
<td>女</td>
<td>78</td>
<td>2008-04-13</td>
</tr>
<tr>
<td>6</td>
<td>陈冠希</td>
<td>8001</td>
<td>总监</td>
<td>男</td>
<td>60</td>
<td>2008-04-18</td>
</tr>
<tr>
<td>7</td>
<td>陈慧琳</td>
<td>8002</td>
<td>试用期</td>
<td>女</td>
<td>85</td>
<td>2008-04-18</td>
</tr>
<tr>
<td>8</td>
<td>张曼玉</td>
<td>8007</td>
<td>高级经理</td>
<td>女</td>
<td>82</td>
<td>2008-04-16</td>
</tr>
<tr>
<td>9</td>
<td>周润发</td>
<td>8006</td>
<td>副总裁</td>
<td>男</td>
<td>88</td>
<td>2008-04-13</td>
</tr>
</table>
<br /><br />
代码:new TableSorter("tb3").OnSorted = function(c, t)<br />
{<br />
alert("table is sorted by " + c.innerHTML + " " + (t ? "Asc" : "Desc"));<br />
}<br />
效果:点击任意表头可执行排序并返回提示.
<br />
<table border="0" style="border:solid 1px #DDDDDD;width:360px;" cellpadding="0" cellspacing="0" id="tb3">
<tr>
<th>序号</th>
<th>姓名</th>
<th>工号</th>
<th>职位</th>
<th>性别</th>
<th>业绩</th>
<th>报到时间</th>
</tr>
<tr>
<td>1</td>
<td>刘德华</td>
<td>8008</td>
<td>总裁</td>
<td>男</td>
<td>98</td>
<td>2008-04-12</td>
</tr>
<tr>
<td>2</td>
<td>吴奇隆</td>
<td>8004</td>
<td>主管</td>
<td>男</td>
<td>80</td>
<td>2008-04-16</td>
</tr>
<tr>
<td>3</td>
<td>王光良</td>
<td>8003</td>
<td>经理</td>
<td>男</td>
<td>85</td>
<td>2008-04-15</td>
</tr>
<tr>
<td>4</td>
<td>张学友</td>
<td>8009</td>
<td>副总裁</td>
<td>男</td>
<td>90</td>
<td>2008-04-11</td>
</tr>
<tr>
<td>5</td>
<td>张柏芝</td>
<td>8005</td>
<td>助理</td>
<td>女</td>
<td>78</td>
<td>2008-04-13</td>
</tr>
<tr>
<td>6</td>
<td>陈冠希</td>
<td>8001</td>
<td>总监</td>
<td>男</td>
<td>60</td>
<td>2008-04-18</td>
</tr>
<tr>
<td>7</td>
<td>陈慧琳</td>
<td>8002</td>
<td>试用期</td>
<td>女</td>
<td>85</td>
<td>2008-04-18</td>
</tr>
<tr>
<td>8</td>
<td>张曼玉</td>
<td>8007</td>
<td>高级经理</td>
<td>女</td>
<td>82</td>
<td>2008-04-16</td>
</tr>
<tr>
<td>9</td>
<td>周润发</td>
<td>8006</td>
<td>副总裁</td>
<td>男</td>
<td>88</td>
<td>2008-04-13</td>
</tr>
</table>
<br />
<script language="javascript" type="text/javascript">...
new TableSorter("tb1");
new TableSorter("tb2", 0, 2 , 5, 6);
new TableSorter("tb3").OnSorted = function(c, t)
...{
alert("table is sorted by " + c.innerHTML + " " + (t ? "Asc" : "Desc"));
}
</script>
</body>
</html>
javascript代码:

  /**//*使用说明 :

  方法1:

  new TableSorter("tb1");

  效果:

  id为tb1的table的第一行任意单元格都可以点击进行排序.

  方法2:

  new TableSorter("tb1", 0, 1, 3);

  效果:

  id为tb1的table的第一行0,1,3单元格可以进行点击排序.

  */

  function TableSorter(table)
  ...{
  this.Table = this.$(table);
  if(this.Table.rows.length <= 1)
  ...{
  return;
  }
  this.Init(arguments);
  }

  //以下样式针对表头的单元格.

TableSorter.prototype.NormalCss = "NormalCss";//没有执行排序时的样式.
TableSorter.prototype.SortAscCss = "SortAscCss";//升序排序时的样式.
TableSorter.prototype.SortDescCss = "SortDescCss";//降序排序时的样式.
//初始化table的信息和操作.
TableSorter.prototype.Init = function(args)
...{
this.ViewState = [];
for(var x = 0; x < this.Table.rows[0].cells.length; x++)
...{
this.ViewState[x] = false;
}
if(args.length > 1)
...{
for(var x = 1; x < args.length; x++)
...{
if(args[x] > this.Table.rows.length)
...{
continue;
}
else
...{
this.Table.rows[0].cells[args[x]].onclick = this.GetFunction(this,"Sort",args[x]);
this.Table.rows[0].cells[args[x]].style.cursor = "pointer";
}
}
}
else
...{
for(var x = 0; x < this.Table.rows[0].cells.length; x++)
...{
this.Table.rows[0].cells[x].onclick = this.GetFunction(this,"Sort",x);
this.Table.rows[0].cells[x].style.cursor = "pointer";
}
}
}
//简写document.getElementById方法.
TableSorter.prototype.$ = function(element)
...{
return document.getElementById(element);
}
//取得指定对象的脱壳函数.
TableSorter.prototype.GetFunction = function(variable,method,param)
...{
return function()
...{
variable[method](param);
}
}
//执行排序.
TableSorter.prototype.Sort = function(col)
...{
var SortAsNumber = true;
for(var x = 0; x < this.Table.rows[0].cells.length; x++)
...{
this.Table.rows[0].cells[x].className = this.NormalCss;
}
var Sorter = [];
for(var x = 1; x < this.Table.rows.length; x++)
...{
Sorter[x-1] = [this.Table.rows[x].cells[col].innerHTML, x];
SortAsNumber = SortAsNumber && this.IsNumeric(Sorter[x-1][0]);
}
if(SortAsNumber)
...{
for(var x = 0; x < Sorter.length; x++)
...{
for(var y = x + 1; y < Sorter.length; y++)
...{
if(parseFloat(Sorter[y][0]) < parseFloat(Sorter[x][0]))
...{
var tmp = Sorter[x];
Sorter[x] = Sorter[y];
Sorter[y] = tmp;
}
}
}
}
else
...{
Sorter.sort();
}
if(this.ViewState[col])
...{
Sorter.reverse();
this.ViewState[col] = false;
this.Table.rows[0].cells[col].className = this.SortDescCss;
}
else
...{
this.ViewState[col] = true;
this.Table.rows[0].cells[col].className = this.SortAscCss;
}
var Rank = [];
for(var x = 0; x < Sorter.length; x++)
...{
Rank[x] = this.GetRowHtml(this.Table.rows[Sorter[x][1]]);
}
for(var x = 1; x < this.Table.rows.length; x++)
...{
for(var y = 0; y < this.Table.rows[x].cells.length; y++)
...{
this.Table.rows[x].cells[y].innerHTML = Rank[x-1][y];
}
}
this.OnSorted(this.Table.rows[0].cells[col], this.ViewState[col]);
}
//取得指定行的内容.
TableSorter.prototype.GetRowHtml = function(row)
...{
var result = [];
for(var x = 0; x < row.cells.length; x++)
...{
result[x] = row.cells[x].innerHTML;
}
return result;
}
TableSorter.prototype.IsNumeric = function(num)
...{
return /^d+[.d+]?$/.test(num);
}
//可自行实现排序后的动作.
TableSorter.prototype.OnSorted = function(cell, IsAsc)
...{
return;
}



相关阅读:
you *might* want to use the less safe log_bin_trust_function_creators variable
特效窗口一网打尽
一个连接两个不同MYSQL数据库的PHP程序
网页制作掌握的最常用的HTML标记
Ubuntu中安装与卸载KDE桌面方法
Dell 1200笔记本安装WinXP时盘符错乱
用vbscript实现隐藏任务栏图标的脚本
PHP框架之Qcodo
可拖动可改变大小div的实现代码
在一个硬盘上安装两个Linux操作系统方法
z-blog分页条中的上下分页调用
jBox 2.3基于jquery的最新多功能对话框插件 常见使用问题解答
PHP Array交叉表实现代码
PHP注释实例技巧
快速导航

Copyright © 2016 phpStudy |