您的位置:首页 > Web前端 > JavaScript

table排序类,点击第一行标题可以排序

2008-12-08 10:20 295 查看
<script type="text/javascript"><!--
google_ad_client = "pub-4490194096475053";
/* 内容页,300x250,第一屏 */
google_ad_slot = "3685991503";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>table排序类,点击第一行标题可以排序
corange.cn</title>
</head>
<body>
<style type="text/css">
.fu_list{ width:400px; border:1px solid #ebebeb;line-height:20px; font-size:12px;}
.fu_list thead td{background-color:#ebebeb;}
.fu_list td{padding:5px;}
.fu_list a{outline:none;/*ff*/hide-focus:expression(this.hideFocus=true);/*ie*/ text-decoration:none; color:#333;}
.fu_list thead a{padding-right:15px;}
.fu_list thead a.up, .fu_list thead a.down{ background:url(up.gif) right center no-repeat; }
.fu_list thead a.down{background-image:url(down.gif);}
</style>
<table border="0" cellspacing="0" cellpadding="0" class="fu_list">
<thead>
<tr>
<td> <a href="javascript:void(0)" id="idTitle">名称</a> / <a href="javascript:void(0)" id="idExt">类型</a></td>
<td width="200" align="center"><a href="javascript:void(0)" id="idAddtime" class="up">上传时间</a></td>
<td width="50" align="center"><a href="javascript:void(0)" id="idSize">大小</a></td>
</tr>
</thead>
<tbody id="idList">
<tr>
<td _ext="rar">new.rar</td>
<td align="center" _order="2008/9/12 8:51:09">2008-9-12 8:51:09</td>
<td align="right" _order="433247">423.09 K</td>
</tr>
<tr>
<td _ext="js">TagControl.js</td>
<td align="center" _order="2008/9/23 11:26:57">2008-9-23 11:26:57</td>
<td align="right" _order="1387">1.35 K</td>
</tr>
<tr>
<td _ext="js">Scroller.js</td>
<td align="center" _order="2008/9/23 11:26:57">2008-9-23 11:26:57</td>
<td align="right" _order="2556">2.5 K</td>
</tr>
<tr>
<td _ext="js">AlertBox.js</td>
<td align="center" _order="2008/9/23 11:26:57">2008-9-23 11:26:57</td>
<td align="right" _order="3565">3.48 K</td>
</tr>
<tr>
<td _ext="htm">1.htm</td>
<td align="center" _order="2008/10/4 20:21:54">2008-10-4 20:21:54</td>
<td align="right" _order="11394">11.13 K</td>
</tr>
<tr>
<td _ext="htm">4.htm</td>
<td align="center" _order="2008/10/4 20:21:54">2008-10-4 20:21:54</td>
<td align="right" _order="351">351 b</td>
</tr>
<tr>
<td _ext="xml">news.xml</td>
<td align="center" _order="2008/10/4 20:24:11">2008-10-4 20:24:11</td>
<td align="right" _order="14074">13.74 K</td>
</tr>
<tr>
<td _ext="xsl">news.xsl</td>
<td align="center" _order="2008/10/4 20:24:11">2008-10-4 20:24:11</td>
<td align="right" _order="16796">16.4 K</td>
</tr>
<tr>
<td _ext="js">function.js</td>
<td align="center" _order="2008/10/4 20:24:11">2008-10-4 20:24:11</td>
<td align="right" _order="2844">2.78 K</td>
</tr>
</tbody>
</table>
<input name="" type="button" value="有x的排后面" id="idBtn" />
<script type="text/javascript">
var $ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
function Each(list, fun){
for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
};
/////////////////////////////////////
////////////////////////////////
var TableOrder = Class.create();
TableOrder.prototype = {
initialize: function(tbody) {
var oThis = this;

this.tBody = $(tbody);//tbody对象
this.Rows = [];//行集合
this._order = null;//排序对象

Each(this.tBody.rows, function(o){ oThis.Rows.push(o); })
},
//排序并显示
Sort: function() {
//排序
if(!this._order){ return false };//没有排序对象返回

this.Rows.sort(!this._order.Compare ? this.Compare() : this._order.Compare);//排序
this._order.Down && this.Rows.reverse();//取反
//显示表格
var oFragment = document.createDocumentFragment();
Each(this.Rows, function(o){ oFragment.appendChild(o); });
this.tBody.appendChild(oFragment);
},
//比较函数
Compare: function() {
var oThis = this;
return function(o1, o2) {
var value1 = oThis.GetValue(o1), value2 = oThis.GetValue(o2);
return value1 < value2 ? -1 : value1 > value2 ? 1 : 0;
};
},
//获取比较值
GetValue: function(tr) {
var data = tr.getElementsByTagName("td")[this._order.Index].getAttribute(this._order.Attribute);
//数据转换
switch (this._order.DataType.toLowerCase()) {
case "int":
return parseInt(data) || 0;
case "float":
return parseFloat(data) || 0;
case "date":
return Date.parse(data) || 0;
case "string":
default:
return data.toString() || "";
}
},
//添加并返回一个排序对象
Add: function(index, options) {
var oThis = this;
return new function(){
//默认属性
this.Attribute = "innerHTML";//获取数据的属性
this.DataType = "string";//数据类型
this.Down = false;//是否按顺序
this.Compare = null;//自定义排序函数
Object.extend(this, options || {});
//排序对象的属性
this.Index = index;
this.Sort = function(){ oThis._order = this; oThis.Sort(); };
};
}
}
var to = new TableOrder("idList");
function SetOrder(obj, index, options){
var o = $(obj);
//添加一个排序对象
var order = to.Add(index, options);
o.onclick = function(){
//取相反排序
order.Down = !order.Down;
//设置样式
Each(SetOrder._arr, function(o){ o.className = ""; })
o.className = order.Down ? "down" : "up";
//排序显示
order.Sort();
return false;
}
//_arr是记录排序项目(这里主要用来设置样式)
SetOrder._arr ? SetOrder._arr.push(o) : SetOrder._arr = [];
}
SetOrder("idTitle", 0);
SetOrder("idExt", 0, { Attribute: "_ext" });
SetOrder("idAddtime", 1, { Attribute: "_order", DataType: "date" });
SetOrder("idSize", 2, { Attribute: "_order", DataType: "int" });
var order2 = to.Add(0, {
Compare: function(o1, o2) {
var value1 = /x/i.test(to.GetValue(o1)), value2 = /x/i.test(to.GetValue(o2));

if(value1 && !value2){
return 1;
} else if (!value1 && value2){
return -1;
} else {
return 0;
}

//return value1 < value2 ? -1 : value1 > value2 ? 1 : 0;
}
});
$("idBtn").onclick = function(){
order2.Sort();
}
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息