js表頭排序實現方法,js表頭排序

來源:互聯網
上載者:User

js表頭排序實現方法,js表頭排序

本文執行個體講述了js表頭排序實現方法。分享給大家供大家參考。

具體實現方法如下:

複製代碼 代碼如下:
<script type="text/javascript">
    //是否遞減排序
    var isDescending = true;
    /*****************************************
    * 要排序的行必須放到<tbody></tbody>標籤中
    * tableId:排序表格ID
    * colNo:排序的列號,即第幾列,從0開始
    * startRowNo:排序的開始行號,從0開始
    * sortLength:要排序的行數,
    * type:排序列的類型
    */
    function sort(tableId, colNo ,startRowNo, sortLength, type)
    {
        //如果要排序的行數是1或是0,則不對其進行排序操作
        if(sortLength<=1){
            return;
        }
        var currTable = document.getElementById(tableId);
        var theHeader = currTable.outerHTML.substring(0, currTable.outerHTML.indexOf('<TBODY>')+7)
        var theFooter = currTable.outerHTML.substring(currTable.outerHTML.indexOf('</TBODY>')-8);
        //這裡的行數是去掉表頭表頭行和表位行的行數
        var theRows = new Array(sortLength);
        //對錶中的資料進行迴圈
        for(i=startRowNo; i<sortLength+startRowNo; i++)
        {
            theRows[i-startRowNo] = new Array(currTable.rows[i].cells[colNo].innerText.toLowerCase(), currTable.rows[i].outerHTML);
        }
        if(type.toUpperCase()=='NUMBER')
        {
            theRows.sort(compareNumber);
        }
        else if(type.toUpperCase()=='DATE')
            theRows.sort(compareDate);
        else if(type.toUpperCase()=='STRING')
            theRows.sort(compareString);
        var tableInfo=''
        for(j=0; j<theRows.length; j++)
        {
            tableInfo+=theRows[j][1];
        }
        isDescending = !isDescending;
        currTable.outerHTML= theHeader + tableInfo +theFooter;
        return ;
    }
    //對數字進行比較
    function compareNumber(x, y)
    {
        //對貨幣格式的資料進行轉化
        a = x[0].excludeChars(",").trim();
        b = y[0].excludeChars(",").trim();
 
        if(a==""){a=0;}
        if(b==""){b=0;}
            if(isDescending)
            {
                return parseFloat(b) - parseFloat(a);
            }
            else
            {
                return parseFloat(a) - parseFloat(b);
            }
    }
    //對字串進行比較
    function compareString(x, y)
    {
            if(isDescending)
            {
                if(x[0]>y[0]) return -1;
                else if(x[0]<y[0]) return 1;
                else return 0;
            }
            else
            {
                if(x[0]<y[0]) return -1;
                else if(x[0]>y[0]) return 1;
                else return 0;
            }
    }
    //對時間進行比較
    function compareDate(x,y){
        var arr=x[0].split("-");
        var starttime=new Date(arr[0],arr[1],arr[2]);
        var starttimes=starttime.getTime();
        var arrs=y[0].split("-");
        var lktime=new Date(arrs[0],arrs[1],arrs[2]);
        var lktimes=lktime.getTime();
        
        if(isDescending)
        {
            return lktimes - starttimes;
        }
        else
        {
            return starttimes - lktimes;
        }
    }
    //去除字串中所有指定的字串
    String.prototype.excludeChars = function(chars){
         var matching = new RegExp(chars , "g") ;
         return this.replace(matching , '') ;
    }
</script>

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.