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

本文介绍了一个使用纯JavaScript实现的表格排序功能,通过简单的点击表头即可实现不同列的数据排序,支持多种数据类型如日期、整数等,并展示了完整的HTML和JavaScript代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
table排序类,点击第一行标题可以排序
http://www.corange.cn//uploadfiles/1005-2_65008.jpg

<!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>&nbsp;<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>
经典小巧的表格组件 What's new in version 4. * This version does not support Delphi 4 and C++ Builer 4 anymore. + There are new components for unified loading tabular information from server to client with after-treatment this data on the client:  editing, sorting, filtrations and unloading changed given on  the server back. Components do not include drivers to access to server.  To ensure functionality of components it is sufficiently to write a  global event that execute SQL expression with parameters using your  favorite access data base engine and return DataSet-cursor.  The technology is represented by two main component:   TMemTableEh - dataset, which hold all data in memory.   You can consider it as array of records.    More other it:     Supports a special interface, that allows DBGridEh component      to view all datas without moving active record.     Allows load data from TDataDriverEh (DataDriver property).     Allows to unload changes back to DataDriver, operative or postponed      (in dependencies of CachedUpdates property).     Allows to create master/datail relationship on client side      (by filterring record) or on external source (by updating      parameters [Params] and requering data from DetailDataDriver).     Allows to sort datas, including Calculated and Lookup field.     Allows to create and fill data at design-time and save data in dfm      file of the form.     Allows to keep records tree like relations. Each record can      have record nodes and work as node of parental record. TDBGridEh      component can show of the tree-type structure of records.     Allows to show internal array of other TMemTableEh ( ExternalMemData      property ) and work with this data: sort, filter, edit.     Has an interface to get the list of all values of the field,      ignoring local filter. TDBGridEh uses this property for      automatic building a list in DropDownBox of the filter.    TDataDriverEh - executes two tasks:     1. Delivers records in TMemTableEh.     2. Processes records, changed in TMemTableEh (writes them in other       dataset, or call events for processing the changes to the program).    Furthermore, there are several components that inheritted from TDataDriverEh.    It is a TSQLDataDriverEh, that have properies to keep four SQL expressions and    parameters for querying data, removing a record, insertions record,    updating record and querying one record. TSQLDataDriverEh can not requests data    from server, but can prepare parameters of the request and call global    event, where programm can transfer SQL expression on the server.    There are several components that inherited from TSQLDataDriverEh, which    can send SQL expressions on the server through the corresponding access engine.    It is TBDEDataDriverEh, TIBXDataDriverEh, TDBXDataDriverEh and TADODataDriverEh    component.    There are a powerful design-time editors for TMemTableEh and TSQLDataDriverEh. * New version of TDBGridEh has changed a parent. Now TCustomDBGridEh is inheritted  from TCustomGridEh. If in your forms DBGrid and DBGridEh are not used  simultaneously then turning on the new version must not appear a problems.  However in some cases compiler can raise error about inconsistency of  Grids.TGridDrawState and GridsEh.TGridDrawState types or similar errors.  To deliverance from this errors it is necessary to delete 'Grids' and  'DBGrids' unit from the 'uses' clause. If 'Grids' and  'DBGrids' are necessary in the uses clause, then it is need to move  these strings in place before 'GridsEh' and 'DBGridEh'.  If even in this case you will get error messages about TGridDrawState types,  it is necessary to elaborate the name of type by the name of the module.  For instance write GridsEh.TGridDrawState or Grids.TGridDrawState instead  of TGridDrawState. + In TDBGridEh   Added ContraColCount property. Property defines amount write nonscrolled   columns. These columns can not get a input focus. + If DBGridEh is connected to TMemTableEh, then it allows:   View all datas, not moving active record.   Show data as tree (If TMemTableEh is in the TreeList mode).   Move records as tree-node in tree (When dgsRowMoving is in OptionsEh).   Automatic building a list in DropDownBox of the filter cell.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值