/*
*
* js操作table排序类
* --------------------------------
* 页面调用 SetOrder(tbody, obj, index, options)
* @tbody : table.tbody
* @obj : td.head.a
* @index : td.cellIndex 默认-1自动获取cellIndex, 如果列中存在隐藏列需指定cellIndex
* @option: 参数
* demo:
* SetOrder("idList", "idTitle", -1);
* SetOrder("idList", "idExt", -1, { Attribute: "_ext" });
* SetOrder("idList", "idAddtime", -1, { Attribute: "_order", DataType: "date" });
* SetOrder("idList", "idSize", -1, { Attribute: "_order", DataType: "int" });
* --------------------------------
*/
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
.Body
=
$(tbody);
//
tbody对象
this
.Rows
=
[];
//
行集合
Each(
this
.Body.rows,
function
(o){ oThis.Rows.push(o); })
},
//
排序并显示
Sort:
function
(order) {
//
排序
this
.Rows.sort(
this
.Compare(order));
//
alert(order.Down + '\n' +this.Rows[0].innerHTML);
order.Down
&&
this
.Rows.reverse();
//
显示表格
var
oFragment
=
document.createDocumentFragment();
Each(
this
.Rows,
function
(o){ oFragment.appendChild(o); });
this
.Body.appendChild(oFragment);
},
//
比较函数
Compare:
function
(order) {
var
oThis
=
this
;
return
function
(o1, o2) {
var
value1
=
oThis.GetValue(o1, order), value2
=
oThis.GetValue(o2, order);
return
value1
<
value2
?
-
1
: value1
>
value2
?
1
:
0
;
};
},
//
获取比较值
GetValue:
function
(tr, order) {
var
data
=
tr.getElementsByTagName(
"
td
"
)[order.Index].getAttribute(order.Attribute);
//
数据转换
switch
(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
;
//
是否按顺序
Object.extend(
this
, options
||
{});
//
排序对象的属性
this
.Index
=
index;
this
.Sort
=
function
(){ oThis.Sort(
this
); };
};
}
}
var
to
=
null
;
//
= new TableOrder("idList");
function
SetOrder(tbody, obj, index, options){
if
(to
==
null
) {
to
=
new
TableOrder(tbody);
}
var
o
=
$(obj);
//
alert(o.parentElement.cellIndex + ' -- ' + index);
if
(isNaN(parseInt(index))
||
parseInt(index)
<
0
) {
index
=
o.parentElement.cellIndex;
}
else
{
index
=
parseInt(index);
}
//
添加一个排序对象
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
=
[];
}