资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
Js在排序中的应用:1、构造函数:getAttribute()是针对XML可以通用的,只要在表格中的语法符合XML的语法就可以使用。(function()functionSortableTable(table)thistable=table;2、调用构造函数:thisheaders=tabletHead厂ows0cells;for(vari=0;ithis.hEadersl巳ngth;i+十)/this.head巳sivalueType=alert(thishE“desi.valuetype);_这里应该用getAttribute(“valu、etype”)3、对表头的属性初始化for(vari=0;ihis.headers.length;i+)this.headersi.valueType=this.headersi.getAttribute(valuetthis.headersi.valueFormat=this.headersi.getAttribute(para4、对图像“”进行初始化varimg=)cumentcreateElement(img)imgclassName=sortDirimage;thissortDirlmage=img;5、在一个表格排序的初始化基本如下functionSortableTable(table)this.table=table;this.headers=table.tHead.rows0.cells;for(vari=0;ithis.headers.length;i+)this.headersi.valueType=this.headersi.getAttribute(valuetthis.headersij.valueFormat=this.headersi.getAttribute(paraIvarimgdocument.createElement(img);img.className=sortDirimage;this.sortDirImage=img;this.tBody=table.tBodies0;this.rows=this.tBody.rows;var_this=this;for(vari=0,vtfformat;ihis.headers.length;i+)vt=this.headersi.getAttribute(valuetype)|string;this.headersi.format=this.headersi.getAttribute(param);每个表头有自己的compare(比较)方法this.headersi.compare=SortableTable.compareMethodsvt;addEvent(this.headersi/click,function()this.currentDir=-this.currentDir|1;_this.sortCol(this.cellIndex/this.currentDir);)?Ivarimg-document.createElement(img);img.className=sortDirimage;this.sortDirlmage=img;日期的转换:nnerText6、在IE中有:返回一个元素中的text元素,火狐是没有这个属性的,所以要有一个方法来获取这个属性:functiongetlnnerText(node)获取指定萌点里面的文本if(node.nodeType=3)/文本节点returnnode.nodeValue.trim();elseif(node.nodeType=l)/元素节点varret=11;for(vari=0;inode.chitdNodes.length;i+)ret+=getlnnerText(nodechildNodesi);returnret;7、将开头和结尾的空格去掉的trim()方法:String.prototype.trim=function()returnthis.replace(/As+/f).replace(/s+$/f);|8、在一个表格排序的排序的方法的构建:SortableTable.prototype=sortCol:function(collndexfdir)排序指定的列/dir指定以倒序排还正序排,取值1默认为1varary=;dir=dir|1;将值与节点(t元素)关联for(vari=0;ithis.rows.length;i+)ary.push(node:this.rowsi,value:getlnnerText(this.rowsi.cellscollndex);varheader=this.headerscollndex;ary.sort(function(afb)varheader=this.headerscollndex;ary.sortffunctionafb)returnheader.compare(a.valuefb.value,header.format)*dir;);varfrag=iocumer.createDocumentFragment();for(i=0;inumber:function(afb)if(ab)return1:if(ab)return1:return0;|if(ab)return-1:当前排序取反,在前面加负号就可以了。;10、排序的方法currentDiaddEvent(this.headersi,click,functionthis.sortCol(this.cetllndex,-this.11、HTML的表格SortableTable瓢城Web俱乐部第四届毕业生成绩表thsortableCol,fiijJavaPythonsortableColththththththsortableColsortableColJsortableColfsortableColI1sortableColf1valuetype=inumberRubyvaluetype=numberLinuxShellvaluetype=inumberVB.NETvaluetype=inumbervaluetype=dateparam=YmdB期v/thA要是这列不需要排序,则去掉class。H陈董v/tdAvtdA男v/tdA90655645803362009-lfl-1012、页面中只要表格的名字叫“sortableTable”就可以进行排序。addEvent(id,load11rfunction()vartables=getByClass(11sortableTable)for(vari=0;itables.:i+斗newSortableTable(tablesi|);(function()String.prototype.trim=function()returnthis.replace八s+/,).replace(/s+$/,);爲nthis.table=table;this.headers=getByClass(sortableCol,table.tHead.rows0);this.tBody=table.tBodies0;【this.rows=this.tBody.rows;|var_this=this;for(vari=0,vt,format;ithis.headers.length;i+)vt=this.headersi.getAttribute(valuetype)|string;this.headersi.format=this.headersi.getAttribute(11param)每个表头有旨己的compare(比较)方法
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号