点这里看效果下载完整实例测试程序代码:
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.prototype.bind = function(object) {
var __method = this, args = Array.prototype.slice.call(arguments); args.shift();
return function() {
return __method.apply(object, args.concat(Array.prototype.slice.call(arguments)));
}
}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(table) {
this.tBody = $(table).tBodies[0];//tbody对象
this.Rows = [];//行集合
this._order = null;//排序对象
Each(this.tBody.rows, function(o){ this.Rows.push(o); }.bind(this));
},
//排序并显示
Sort: function() {
//没有排序对象返回
if(!this._order){ return false };
//排序
this.Rows.sort(!this._order.Compare ? this.Compare.bind(this) : 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);
//执行附加函数
this._order.onSort();
},
//比较函数
Compare: function(o1, o2) {
var value1 = this.GetValue(o1), value2 = this.GetValue(o2);
return value1 < value2 ? -1 : value1 > value2 ? 1 : 0;
},
//获取比较值
GetValue: function(tr) {
var td = tr.getElementsByTagName("td")[this._order.Index]
, data = td[this._order.Attri] ? td[this._order.Attri] : td.getAttribute(this._order.Attri);
//数据转换
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.Attri = "innerHTML";//获取数据的属性
this.DataType = "string";//比较的数据类型
this.Down = true;//是否按顺序
this.onSort = function(){};//排序时执行
this.Compare = null;//自定义排序函数
Object.extend(this, options || {});
//td索引
this.Index = index;
this.Sort = function(){ oThis._order = this; oThis.Sort(); };
};
}
}
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.prototype.bind = function(object) {
var __method = this, args = Array.prototype.slice.call(arguments); args.shift();
return function() {
return __method.apply(object, args.concat(Array.prototype.slice.call(arguments)));
}
}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(table) {
this.tBody = $(table).tBodies[0];//tbody对象
this.Rows = [];//行集合
this._order = null;//排序对象
Each(this.tBody.rows, function(o){ this.Rows.push(o); }.bind(this));
},
//排序并显示
Sort: function() {
//没有排序对象返回
if(!this._order){ return false };
//排序
this.Rows.sort(!this._order.Compare ? this.Compare.bind(this) : 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);
//执行附加函数
this._order.onSort();
},
//比较函数
Compare: function(o1, o2) {
var value1 = this.GetValue(o1), value2 = this.GetValue(o2);
return value1 < value2 ? -1 : value1 > value2 ? 1 : 0;
},
//获取比较值
GetValue: function(tr) {
var td = tr.getElementsByTagName("td")[this._order.Index]
, data = td[this._order.Attri] ? td[this._order.Attri] : td.getAttribute(this._order.Attri);
//数据转换
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.Attri = "innerHTML";//获取数据的属性
this.DataType = "string";//比较的数据类型
this.Down = true;//是否按顺序
this.onSort = function(){};//排序时执行
this.Compare = null;//自定义排序函数
Object.extend(this, options || {});
//td索引
this.Index = index;
this.Sort = function(){ oThis._order = this; oThis.Sort(); };
};
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货