频道栏目
首页 > 程序开发 > web前端 > JavaScript > 正文
JavaScript实现表格排序
2011-05-28 10:26:41           
收藏   我要投稿

<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

点击复制链接 与好友分享!回本站首页
相关TAG标签 表格
上一篇:javascript设计模式第二章接口
下一篇:鼠标滑上去后图片放大浮出效果的实现
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站