js表格排序
<!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=UTF-8">
<title>无标题文档</title>
<style>
*{font-family:Arial, Helvetica, sans-serif;font-size:14px;border:none;}
body{text-align:center;}
table{margin:100px auto;}
td{width:100px;height:24px;text-align:center;line-height:24px;border:1px solid silver;}
.red{color:red;}
.top{background:#CCCCCC;cursor:pointer;}
.up{background:#FFFFCC}
.down{background:#FFFFCC}
.hov{background:#F0EFE5;}
</style>
</head><body>
<table id="table" cellpadding="0">
<tbody><tr class="top"><td>kick me</td><td>kick me</td><td>kick me</td><td>kick me</td></tr>
<tr>
<td><span id="bfn_la_bac.usa">15.43</span></td>
<td class="red">700</td>
<td>1.220</td>
<td class="red">阿</td>
</tr>
<tr><td><span id="bfn_la_c.usa">7.05</span></td>
<td class="red">4</td>
<td>3,000</td>
<td class="red">炳</td>
</tr>
<tr><td><span id="bfn_la_jpm.usa">30.62</span></td>
<td class="red">30</td>
<td>2,558,800</td>
<td class="red">和</td>
</tr>
<tr>
<td><span id="bfn_la_axp.usa">22.30</span></td>
<td class="red">5</td><td>6</td>
<td class="red">瞎</td>
</tr>
<tr><td><span id="bfn_la_mrk.usa">26.31</span></td>
<td class="red">0.6</td><td>5</td>
<td class="red">-</td>
</tr>
<tr><td><span id="bfn_la_pg.usa">63.16</span></td>
<td class="red">7</td><td>4</td>
<td class="red">子</td>
</tr>
</tbody></table>
<script type="text/javascript">
var tableSort = function(){
this.initialize.apply(this,arguments);
}
tableSort.prototype = {
initialize : function(tableId,clickRow,startRow,endRow,classUp,classDown,selectClass){
this.Table = document.getElementById(tableId);
this.rows = this.Table.rows;//所有行
this.Tags = this.rows[clickRow-1].cells;//标签td
this.up = classUp;
this.down = classDown;
this.startRow = startRow;
this.selectClass = selectClass;
this.endRow = (endRow == 999? this.rows.length : endRow);
this.T2Arr = this._td2Array();//所有受影响的td的二维数组
this.setShow();
},
//设置标签切换
setShow:function(){
var defaultClass = this.Tags[0].className;
for(var Tag ,i=0;Tag = this.Tags[i];i++){
Tag.index = i;
addEventListener(Tag ,'click', Bind(Tag,statu));
}
var _this =this;
var turn = 0;
function statu(){
for(var i=0;i<_this.Tags.length;i++){
_this.Tags[i].className = defaultClass;
}
if(turn==0){
addClass(this,_this.down)
_this.startArray(0,this.index);
turn=1;
}else{
addClass(this,_this.up)
_this.startArray(1,this.index);
turn=0;
}
}
},
//设置选中列样式
colClassSet:function(num,cla){
//得到关联到的td
for(var i= (this.startRow-1);i<(this.endRow);i++){
for(var n=0;n<this.rows[i].cells.length;n++){
removeClass(this.rows[i].cells[n],cla);
}
addClass(this.rows[i].cells[num],cla);
}
},
//开始排序 num 根据第几列