【www.gdgbn.com--jquery】

 

html代码

table#tb {
text-align:center;
border:1px #ccc solid;
border-collaps教程e:collapse;
font-size:12px;
font-family:arial, helvetica, sans-serif;
color:#666;
width:900px;
background:url(room-bg.gif) 0 -13px repeat-x ;
}
table#tb td {
border-bottom:#ccc 1px solid;
padding:.3em 0 .3em 0;
}

#tb th {
height:30px;
color:#009;
padding-right:16px;
}
#tb thead tr{

}

#tb td.highlight{color:#000;}
#tb th.selectup {
background:url(up1.png) no-repeat right center transparent ;

}
#tb th.selectdown {
background:url(down1.png) no-repeat right center transparent ;
}
#tb tfoot{
font-weight:bold;
color:#06f;
background:url(room-bg.gif) 0 -13px repeat-x ;
}

js代码
window.onload=function(){
function fini(num){
table.th[num].classname=
table.th[num].classname=="selectup"?
"selectdown":"selectup";//切换标示图标

each(table.row,function(o){//highlight高亮当前排序的列
o.cells[num].classname="highlight";
if(table.index!=null&&table.index!=num){
o.cells[table.index].classname="";
}
});
if(table.index!=null&&table.index!=num){//另外的被点击 原先的被取消表示图标
table.th[table.index].classname="";
}
}

var table=new tablelistsort($("tb"),{data:8,filetype:9,fn:fini})//文档载入后new传递参数
}

js代码

 

var class={
create:function(){
return function(){
this.init.apply(this,arguments)
}
}
}
function each(o,fn){
for(var j=0,len=o.length;j fn(o[j],j)
}
}

function.prototype.bind=function(){
var method=this;
var args=array.prototype.slice.call(arguments);
var object=args.shift();
return function(){
return method.apply(object,args.concat(array.prototype.slice.call(arguments)))
}
}
function $(elem,elem2){
var arr=[];
typeof elem=="string" ?arr.push(document.getelementbyid(elem)):arr.push(elem);
elem2&&arr.push(arr[0].getelementsbytagname(elem2));
(arr.length>1)&&(typeof elem=="object") &&(arr.shift());
return arr.length!=2?arr[0]:arr[1];
}

var tablelistsort=class.create()
tablelistsort.prototype={
init:function(tables,options){
this.table=$(tables);//找到table元素
this.th=$($(this.table,"thead")[0],"th");//找到th元素
this.tbody=$(this.table,"tbody")[0];//找到tbody元素
this.row=$(this.tbody,"tr"); //找到tr元素
this.rowarr=[];//tr塞入这个数组
this.index=null;
this.options=options||{};
this.finish=this.options.fn||function(){};
this.dataindex=math.abs(this.options.data)||null;//提供比较数据类型的坐标。
this.file=math.abs(this.options.filetype)||null;//提供需要比较file类型坐标
each(this.row,function(o){this.rowarr.push(o)}.bind(this));//将tr对象列表载入进数组
for(var i=0;this.th.length>i;i++){
this.th[i].onclick=this.sort.bind(this,i)//使用bind间接保持一个下标值(变量),传递过去
this.th[i].style.cursor="pointer";
}
this.re=/([-]{0,1}[0-9.]{1,})/g;// 替换的正则表达式
this.dataindex&&subdata(this.row,this.dataindex,this.row.length);
},

sort:function(num){
//var date1=new date().gettime()//测试用于排序时间 如果想测试排序时间请讲注释去掉
//另外的被点击 原先的被取消表示图标
(this.index!=null&&this.index!=num)&&this.th[this.index].setattribute("sorted","");
this.th[num].getattribute("sorted")!="ed"?
this.rowarr.sort(this.naturalsort.bind(this,num)):this.rowarr.reverse();
//如果当前对象被点击过,使用reverse()对应的列直接反序,否则排序,因为使用的是预定义的sort()方法,所以如果要让执行排序的函数
//知道需要排序的列的下标值的话,bind()传递num过去,this来调用;
var frag=document.createdocumentfragment();//创建文档碎片
each(this.rowarr,function(o){frag.appendchild(o)});//将排序后的数组插入文档碎片
this.tbody.appendchild(frag);//碎片插入节点
this.th[num].setattribute("sorted","ed");
//$("spans").innerhtml=(new date().gettime())-date1;//测试用于排序时间 如果想测试排序时间请讲注释去掉
this.finish(num);//排序后执行的函数
this.index=num;//被排序过的坐标值
},
naturalsort:function (num,a, b) {
//判断是否是数据排序 如果是的话 查找属性
var a=this.dataindex!=num?a.cells[num].innerhtml:a.cells[num].getattribute("data"),
b=this.dataindex!=num?b.cells[num].innerhtml:b.cells[num].getattribute("data");
//num被bind方法传递过来的,找到需排序的单元格里面的内容
var x = a.tostring().tolowercase() || "", y = b.tostring().tolowercase() || "",
nc = string.fromcharcode(0),
xn = x.replace(this.re, nc + "$1" + nc).split(nc),// 将字符串分裂成数组
yn = y.replace(this.re, nc + "$1" + nc).split(nc),
xd = (new date(x)).gettime(), yd = (new date(y)).gettime()
xn = this.file!=num?xn:xn.reverse(),//如果有filetype则反序
yn = this.file!=num?yn:yn.reverse()

if ( xd && yd && xd < yd )
return -1;
else if ( xd && yd && xd > yd )
return 1;
for ( var cloc=0, nums = math.max( xn.length, yn.length ); cloc < nums; cloc++ )
if ( ( parsefloat( xn[cloc] ) || xn[cloc] ) < ( parsefloat( yn[cloc] ) || yn[cloc] ) )
//不能转换为floatnumber直接进行字母比较,如"a"<"b" print//true
return -1;
else if ( ( parsefloat( xn[cloc] ) || xn[cloc] ) > ( parsefloat( yn[cloc] ) || yn[cloc] ) )
return 1;
return 0;
}

}
function subdata(o,i,len){//如果有数据大小排序给td添加一个自定属性给data//正则也是可以的判断的//mymickey没有在这里写正则
for(var j=0;len>j;j++){
if(o[j].cells[i].innerhtml.substring(o[j].cells[i].innerhtml.lastindexof("kb")).tolowercase()=="kb"){
o[j].cells[i].setattribute("data",parsefloat(o[j].cells[i].innerhtml)*1024);
}
if(o[j].cells[i].innerhtml.substring(o[j].cells[i].innerhtml.lastindexof("mb")).tolowercase()=="mb"){
o[j].cells[i].setattribute("data",parsefloat(o[j].cells[i].innerhtml)*1000000);
}
else if(o[j].cells[i].innerhtml.substring(o[j].cells[i].innerhtml.lastindexof("gb")).tolowercase()=="gb"){
o[j].cells[i].setattribute("data",parsefloat(o[j].cells[i].innerhtml)*1000000000);
}
}
}

本文来源:http://www.gdgbn.com/wangyezhizuo/26896/