表格操作类(添加,删除,排序,上移,下移)

http://www.cnblogs.com/zyh-nhy/archive/2007/08/14/855063.html

<html>
None.gif
<head>
None.gif
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
ExpandedBlockStart.gifContractedBlock.gif
<styletype="text/css">dot.gif*{dot.gif}{font-size:14px}button{dot.gif}{margin:3px}</style>
ExpandedSubBlockStart.gifContractedSubBlock.gif
<scripttype="text/javascript">dot.gif
InBlock.gif
InBlock.gif
varmytable=null,mytable2=null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifwindow.onload
=function()dot.gif{
InBlock.gifmytable
=newCTable("tbl",10);
InBlock.gifmytable2
=newCTable("tbl2",6);
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifArray.prototype.each
=function(f)dot.gif{for(vari=0;i<this.length;i++)f(this[i],i,this)}
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
function$A(arrayLike)dot.gif{
InBlock.gif
for(vari=0,ret=[];i<arrayLike.length;i++)ret.push(arrayLike[i]);
InBlock.gif
returnret
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifFunction.prototype.bind
=function()dot.gif{
InBlock.gif
var__method=this,args=$A(arguments),object=args.shift();
ExpandedSubBlockStart.gifContractedSubBlock.gif
returnfunction()dot.gif{
InBlock.gif
return__method.apply(object,args.concat($A(arguments)));
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
functionCTable(id,rows)dot.gif{
InBlock.gif
this.tbl=typeof(id)=="string"?document.getElementById(id):id;
InBlock.gif
if(rows&&/^\d+$/.test(rows))this.addrows(rows)
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifCTable.prototype
=dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifaddrows:
function(n)dot.gif{//随机添加n个tr
InBlock.gif
newArray(n).each(this.add.bind(this))
ExpandedSubBlockEnd.gif}
,
ExpandedSubBlockStart.gifContractedSubBlock.gifadd:
function()dot.gif{//添加1个tr
InBlock.gif
varself=this;
InBlock.gif
vartr=self.tbl.insertRow(-1),td1=tr.insertCell(-1),td2=tr.insertCell(-1),td3=tr.insertCell(-1);
InBlock.gif
varchkbox=document.createElement("INPUT")
InBlock.gifchkbox.type
="checkbox"
InBlock.gifchkbox.onclick
=self.highlight.bind(self)
InBlock.giftd1.appendChild(chkbox)
InBlock.giftd1.setAttribute(
"width","35")
InBlock.giftd2.innerHTML
=Math.ceil(Math.random()*99)
InBlock.giftd3.innerHTML
=Math.ceil(Math.random()*99)
ExpandedSubBlockEnd.gif}
,
ExpandedSubBlockStart.gifContractedSubBlock.gifdel:
function()dot.gif{//删除所选tr
InBlock.gif
varself=this
ExpandedSubBlockStart.gifContractedSubBlock.gif$A(self.tbl.rows).each(
function(tr)dot.gif{if(self.getChkBox(tr).checked)tr.parentNode.removeChild(tr)})
ExpandedSubBlockEnd.gif}
,
ExpandedSubBlockStart.gifContractedSubBlock.gifup:
function()dot.gif{//上移所选tr
InBlock.gif
varself=this
ExpandedSubBlockStart.gifContractedSubBlock.gif
varupOne=function(tr)dot.gif{//上移1个tr
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(tr.rowIndex>0)dot.gif{
InBlock.gifself.swapTr(tr,self.tbl.rows[tr.rowIndex
-1])
InBlock.gifself.getChkBox(tr).checked
=true
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
vararr=$A(self.tbl.rows).reverse()
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(arr.length>0&&self.getChkBox(arr[arr.length-1]).checked)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(vari=arr.length-1;i>=0;i--)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(self.getChkBox(arr[i]).checked)dot.gif{
InBlock.gifarr.pop()
ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
InBlock.gif
break
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gifarr.reverse().each(
function(tr)dot.gif{if(self.getChkBox(tr).checked)upOne(tr)});
ExpandedSubBlockEnd.gif}
,
ExpandedSubBlockStart.gifContractedSubBlock.gifdown:
function()dot.gif{
InBlock.gif
varself=this
ExpandedSubBlockStart.gifContractedSubBlock.gif
vardownOne=function(tr)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(tr.rowIndex<self.tbl.rows.length-1)dot.gif{
InBlock.gifself.swapTr(tr,self.tbl.rows[tr.rowIndex
+1]);
InBlock.gifself.getChkBox(tr).checked
=true;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
vararr=$A(self.tbl.rows)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(arr.length>0&&self.getChkBox(arr[arr.length-1]).checked)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(vari=arr.length-1;i>=0;i--)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(self.getChkBox(arr[i]).checked)dot.gif{
InBlock.gifarr.pop()
ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
InBlock.gif
break
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gifarr.reverse().each(
function(tr)dot.gif{if(self.getChkBox(tr).checked)downOne(tr)});
ExpandedSubBlockEnd.gif}
,
ExpandedSubBlockStart.gifContractedSubBlock.gifsort:
function()dot.gif{//排序
InBlock.gif
varself=this,order=arguments[0];
ExpandedSubBlockStart.gifContractedSubBlock.gif
varsortBy=function(a,b)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(typeof(order)=="number")dot.gif{//数字,则按数字指示的列排序
InBlock.gif
returnNumber(a.cells[order].innerHTML)>=Number(b.cells[order].innerHTML)?1:-1;//转化为数字类型比较大小
ExpandedSubBlockStart.gifContractedSubBlock.gif
}
elseif(typeof(order)=="function")dot.gif{//为程序,按程序的返回结果排序
InBlock.gif
returnorder(a,b);
ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
InBlock.gif
return1;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif$A(self.tbl.rows).sort(sortBy).each(
function(x)dot.gif{
InBlock.gif
varcheckStatus=self.getChkBox(x).checked;
InBlock.gifself.tbl.firstChild.appendChild(x);
InBlock.gif
if(checkStatus)self.getChkBox(x).checked=checkStatus;
ExpandedSubBlockEnd.gif}
);
ExpandedSubBlockEnd.gif}
,
ExpandedSubBlockStart.gifContractedSubBlock.gifrnd:
function()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值