JavaScript實現表格排序

來源:互聯網
上載者:User

<SCRIPT LANGUAGE="JavaScript">  
    var dom = (document.getElementsByTagName) ? true : false;  
    var ie5 = (document.getElementsByTagName && document.all) ? true : false;  
    var arrowUp, arrowDown;  
    if (ie5 || dom)  
        initSortTable();  
    function initSortTable() {  
        arrowUp = document.createElement("SPAN");  
        var tn = document.createTextNode("↑");  
        arrowUp.appendChild(tn);  
        arrowUp.className = "arrow";  
        arrowDown = document.createElement("SPAN");  
        var tn = document.createTextNode("↓");  
        arrowDown.appendChild(tn);  
        arrowDown.className = "arrow";  
    }  
    function sortTable(tableNode, nCol, bDesc, sType) {  
        var tBody = tableNode.tBodies[0];  
        var trs = tBody.rows;  
        var trl= trs.length;  
        var a = new Array();  
        for (var i = 0; i < trl; i++) {  
            a[i] = trs[i];  
        }  
        var start = new Date;  
        window.status = "Sorting data...";  
        a.sort(compareByColumn(nCol,bDesc,sType));  
        window.status = "Sorting data done";  
        for (var i = 0; i < trl; i++) {  
            tBody.appendChild(a[i]);  
            window.status = "Updating row " + (i + 1) + " of " + trl +  
                " (Time spent: " + (new Date - start) + "ms)";  
        }  
        // check for onsort  
        if (typeof tableNode.onsort == "string")  
            tableNode.onsort = new Function("", tableNode.onsort);  
        if (typeof tableNode.onsort == "function")  
            tableNode.onsort();  
    }  
    function CaseInsensitiveString(s) {  
        return String(s).toUpperCase();  
    }  
    function parseDate(s) {  
        return Date.parse(s.replace(/-/g, /));  
    }  
    function toNumber(s) {  
        return Number(s.replace(/[^0-9.]/g, ""));  
    }  
    function Percent(s) {  
        return Number(s.replace("%", ""));  
    }  
    function compareByColumn(nCol, bDescending, sType) {  
        var c = nCol;  
        var d = bDescending;  
        var fTypeCast = String;  
        if (sType == "Number")  
            fTypeCast = Number;  
        else if (sType == "Date")  
            fTypeCast = parseDate;  
        else if (sType == "CaseInsensitiveString")  
            fTypeCast = CaseInsensitiveString;  
        else if (sType == "Percent")  
            fTypeCast = Percent;  
        return function (n1, n2) {  
            if (fTypeCast(getInnerText(n1.cells[c])) < fTypeCast(getInnerText(n2.cells[c])))  
                return d ? -1 : +1;  
            if (fTypeCast(getInnerText(n1.cells[c])) > fTypeCast(getInnerText(n2.cells[c])))  
                return d ? +1 : -1;  
            return 0;  
        };  
    }  
    function sortColumnWithHold(e) {  
        var el = ie5 ? e.srcElement : e.target;  
        var table = getParent(el, "TABLE");  
        var oldCursor = table.style.cursor;  
        var oldClick = table.onclick;  
        table.style.cursor = "wait";  
        table.onclick = null;  
        var fakeEvent = {srcElement : e.srcElement, target : e.target};  
        window.setTimeout(function () {  
            sortColumn(fakeEvent);  
            table.style.cursor = oldCursor;  
            table.onclick = oldClick;  
        }, 100);  
    }  
    function sortColumn(e) {  
        var tmp = e.target ? e.target : e.srcElement;  
        var tHeadParent = getParent(tmp, "THEAD");  
        var el = getParent(tmp, "TD");  
        if (tHeadParent == null)  
            ret

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.