http://www.cnblogs.com/zyh-nhy/archive/2007/08/14/855063.html
<html><head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
<styletype="text/css">*{font-size:14px}button{margin:3px}</style>
<scripttype="text/javascript">
varmytable=null,mytable2=null;
window.onload=function(){
mytable=newCTable("tbl",10);
mytable2=newCTable("tbl2",6);
}
Array.prototype.each=function(f){for(vari=0;i<this.length;i++)f(this[i],i,this)}
function$A(arrayLike){
for(vari=0,ret=[];i<arrayLike.length;i++)ret.push(arrayLike[i]);
returnret
}
Function.prototype.bind=function(){
var__method=this,args=$A(arguments),object=args.shift();
returnfunction(){
return__method.apply(object,args.concat($A(arguments)));
}
}
functionCTable(id,rows){
this.tbl=typeof(id)=="string"?document.getElementById(id):id;
if(rows&&/^\d+$/.test(rows))this.addrows(rows)
}
CTable.prototype={
addrows:function(n){//随机添加n个tr
newArray(n).each(this.add.bind(this))
},
add:function(){//添加1个tr
varself=this;
vartr=self.tbl.insertRow(-1),td1=tr.insertCell(-1),td2=tr.insertCell(-1),td3=tr.insertCell(-1);
varchkbox=document.createElement("INPUT")
chkbox.type="checkbox"
chkbox.onclick=self.highlight.bind(self)
td1.appendChild(chkbox)
td1.setAttribute("width","35")
td2.innerHTML=Math.ceil(Math.random()*99)
td3.innerHTML=Math.ceil(Math.random()*99)
},
del:function(){//删除所选tr
varself=this
$A(self.tbl.rows).each(function(tr){if(self.getChkBox(tr).checked)tr.parentNode.removeChild(tr)})
},
up:function(){//上移所选tr
varself=this
varupOne=function(tr){//上移1个tr
if(tr.rowIndex>0){
self.swapTr(tr,self.tbl.rows[tr.rowIndex-1])
self.getChkBox(tr).checked=true
}
}
vararr=$A(self.tbl.rows).reverse()
if(arr.length>0&&self.getChkBox(arr[arr.length-1]).checked){
for(vari=arr.length-1;i>=0;i--){
if(self.getChkBox(arr[i]).checked){
arr.pop()
}else{
break
}
}
}
arr.reverse().each(function(tr){if(self.getChkBox(tr).checked)upOne(tr)});
},
down:function(){
varself=this
vardownOne=function(tr){
if(tr.rowIndex<self.tbl.rows.length-1){
self.swapTr(tr,self.tbl.rows[tr.rowIndex+1]);
self.getChkBox(tr).checked=true;
}
}
vararr=$A(self.tbl.rows)
if(arr.length>0&&self.getChkBox(arr[arr.length-1]).checked){
for(vari=arr.length-1;i>=0;i--){
if(self.getChkBox(arr[i]).checked){
arr.pop()
}else{
break
}
}
}
arr.reverse().each(function(tr){if(self.getChkBox(tr).checked)downOne(tr)});
},
sort:function(){//排序
varself=this,order=arguments[0];
varsortBy=function(a,b){
if(typeof(order)=="number"){//数字,则按数字指示的列排序
returnNumber(a.cells[order].innerHTML)>=Number(b.cells[order].innerHTML)?1:-1;//转化为数字类型比较大小
}elseif(typeof(order)=="function"){//为程序,按程序的返回结果排序
returnorder(a,b);
}else{
return1;
}
}
$A(self.tbl.rows).sort(sortBy).each(function(x){
varcheckStatus=self.getChkBox(x).checked;
self.tbl.firstChild.appendChild(x);
if(checkStatus)self.getChkBox(x).checked=checkStatus;
});
},
rnd:function()b