zoukankan      html  css  js  c++  java
  • 《JavaScript高级程序设计》阅读笔记(十九):表格排序

    排序代码

    function SortTable(sTableID, iCol, sDataType){
        this.oTable=document.getElementById(sTableID);
        this.oTBody=this.oTable.tBodies[0];
        this.colDataRows=this.oTBody.rows;
        this.aTRs=[];
        this.iCol=iCol;
        this.sDataType=sDataType;
    }
    
    SortTable.prototype={
        convert:function(sValue, sDataType){
            switch(sDataType){
                case "int":
                    return parseInt(sValue);
                case "float":
                    return parseFloat(sValue);
                case "date":
                    return new Date(sValue);
                default:
                    return sValue.toString();
            }
        },
        generateCompareTRs:function(iCol, sDataType, that){
            return function compareTRs(oTR1,oTR2){
                        var vValue1= that.convert(oTR1.cells[iCol].firstChild.nodeValue, sDataType),
                            vValue2= that.convert(oTR2.cells[iCol].firstChild.nodeValue, sDataType);
                        if(vValue1 < vValue2){
                            return -1;
                        } else if(vValue1 > vValue2){
                            return 1;
                        } else{
                            return 0;
                        }
                   };
        },
        sort:function(){
            for(var i=0,l=this.colDataRows.length;i<l;i++){
                this.aTRs.push(this.colDataRows[i]);
            }
            if(this.oTable.sortCol === this.iCol){
                this.aTRs.reverse();
            } else {
                this.aTRs.sort(this.generateCompareTRs(this.iCol, this.sDataType, this));
            }
    
            var oFragment=document.createDocumentFragment();
            for(var i=0,l=this.aTRs.length;i<l;i++){
                oFragment.appendChild(this.aTRs[i]);
            }
    
            this.oTBody.appendChild(oFragment);
            this.oTable.sortCol = this.iCol;
        }
    }

    调用示例

    function bindSortTable(sTableID, iCol, sDataType){
        var table=document.getElementById(sTableID),
            ftr=table.tHead.rows[0],
            tds=ftr.cells;
        if(tds[iCol]){
            tds[iCol].onclick=function(){
                var sortTable=new SortTable(sTableID, iCol, sDataType);
                sortTable.sort();
            }
        }
    }
    
    window.onload=function(){
        bindSortTable("tblSort",0);
        bindSortTable("tblSort",1);
        bindSortTable("tblSort",2,"int");
        bindSortTable("tblSort",3,"float");
        bindSortTable("tblSort",4,"date");
    }

    将table中的td顺序颠倒

    function reverseTD(tbid){
        var table=document.getElementById(tbid),
            tbody=table.tBodies[0],
            trs=tbody.rows,
            cols=[];
        for(var i=0,j=trs.length;i<j;i++){
            cols = trs[i].cells;
            trs[i]=document.createElement("tr");
            for(var m=cols.length-1;m>=0;m--){
                trs[i].appendChild(cols[m]);
            }
        }
    }
    
    //Test
    reverseTD("tableid");
  • 相关阅读:
    将数字转换千分位分隔形式
    根据字符分割字符串的三种写法
    取出字符串中的汉字、字母或是数字
    生成n位随机字符串
    python中类的继承
    汇编语言与计算机体系结构
    DMA(direct memory access)直接内存访问
    数学归纳法证明时间复杂度
    具体名词的理解、单词的深意
    python的类和对象
  • 原文地址:https://www.cnblogs.com/artwl/p/2614447.html
Copyright © 2011-2022 走看看