JS表格排序

    var employees = []
    employees[0] = {
        name: "George",
        age: 32,
        retiredate: "March 12, 2014"
    }
    employees[1] = {
        name: "Edward",
        age: 17,
        retiredate: "June 2, 2023"
    }
    employees[2] = {
        name: "Christine",
        age: 58,
        retiredate: "December 20, 2036"
    }
    employees[3] = {
        name: "Sarah",
        age: 62,
        retiredate: "April 30, 2020"
    }

    //如何对上述数据排序呢?
    //实际上是利用arr.sort(function...)
    //而不是将某一列的数据都找出来排好序后  再去找对应的行

    function sortBy(arr, prop, descend) {
        //descend表示升序还是降序
        descend = descend ? 1 : -1;
        arr.sort(function(a, b) {
            if (a[prop] < b[prop]) {
                return -1 * descend;
            } else if (a[prop] > b[prop]) {
                return 1 * descend;
            } else return 0;
        });
    }
    sortBy(employees, 'name', true);
    console.log(employees);

    ;
    (function($) {
        $.fn.extend({
            tableSort: function() {
                console.log(this); //jQ对象的table
                var colHeads = this.find('thead');
                var tbody = this.find('tbody');
                var rows = tbody.find('tr');
                var desend = [];
                colHeads.on('click', 'td', function(e) {
                    var idx = $(this).index();
                    desend[idx] = !desend[idx]; //每一列都有一个变量 经过一次排序后下次再排序是反序
                    var descendflag = desend[idx] ? 1 : -1;
                    rows.sort(function(v1, v2) {
                        if (v1.cells[idx].innerText < v2.cells[idx].innerText) {
                            return -1 * descendflag;
                        } else if (v1.cells[idx].innerText > v2.cells[idx].innerText) {
                            return 1 * descendflag;
                        } else {
                            return 0;
                        }
                    });
                    rows.each(function(idx, item) {
                        tbody.append(item);
                    })
                });
            }
        })
    })(jQuery);

    $('#tableTest').tableSort();

    function sortOrigin() {
        var oTable = document.getElementById('tableTest');
        var oHead = oTable.tHead;
        var colHeads = oHead.getElementsByTagName('td');
        var oTbody = oTable.tBodies[0];
        var oBtn = document.getElementById('sort');
        var arr = []; //用来存放每一个tr
        var isAsc = true; //用来判断升序,还是降序

        var colHeadsArr = [];
        for (var i = 0, len = colHeads.length; i < len; i++) {
            colHeadsArr.push(colHeads[i]);
        }
        //表头事件代理
        oHead.addEventListener('click', function(e) {
            var target = e.target;
            if (target.tagName.toLowerCase() === 'td') {
                var idx = colHeadsArr.indexOf(target); //对第几列进行排序
                //和colHeads一样 oTbody.rows只是NodeList不是数组
                for (var i = 0; i < oTbody.rows.length; i++) {
                    arr[i] = oTbody.rows[i];
                }
                //此时arr是行构成的数组
                arr.sort(function(v1, v2) {
                    if (v1.cells[idx].innerText < v2.cells[idx].innerText) {
                        return -1;
                    } else if (v1.cells[idx].innerText > v2.cells[idx].innerText) {
                        return 1;
                    } else {
                        return 0;
                    }
                });
                for (var j = 0; j < arr.length; j++) {
                    oTbody.appendChild(arr[j]);
                }
            }
        }, false);

    }

 

所谓表格排序  实际上是 对象数组排序 --> []中的自定义sort函数

参考 http://www.cnblogs.com/xiao-t/archive/2012/12/27/2836248.html

<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <title>table排序</title>
</head>

<body>
    <table id="tableTest" width="400" border="1">
        <thead>
            <tr>
                <td>ID</td>
                <td>Name</td>
                <td>col1</td>
                <td>COL2</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>2</td>
                <td>bbb</td>
                <td>MANU</td>
                <td>Znbsp;</td>
            </tr>
            <tr>
                <td>5</td>
                <td>eee</td>
                <td>COLS</td>
                <td>Ssp;</td>
            </tr>
            <tr>
                <td>3</td>
                <td>ccc</td>
                <td>KK</td>
                <td>bsp;</td>
            </tr>
            <tr>
                <td>4</td>
                <td>ddd</td>
                <td>Dnbsp;</td>
                <td>nbsp;</td>
            </tr>
            <tr>
                <td>1</td>
                <td>aaa</td>
                <td>Pnbsp;</td>
                <td>Mnbsp;</td>
            </tr>
        </tbody>
    </table>
    <input type="button" id="sort" value="表格排序" />
    <script>
    window.onload = function() {
        var oTable = document.getElementById('tableTest');
        var oHead = oTable.tHead;
        var colHeads = oHead.getElementsByTagName('td');
        var oTbody = oTable.tBodies[0];
        var oBtn = document.getElementById('sort');
        var arr = []; //用来存放每一个tr
        var isAsc = true; //用来判断升序,还是降序

        var colHeadsArr = [];
        for (var i = 0, len = colHeads.length; i < len; i++) {
            colHeadsArr.push(colHeads[i]);
        }
        //表头事件代理
        oHead.addEventListener('click', function(e) {
            var target = e.target;
            if (target.tagName.toLowerCase() === 'td') {
                var idx = colHeadsArr.indexOf(target); //对第几列进行排序
                //和colHeads一样 oTbody.rows只是NodeList不是数组
                for (var i = 0; i < oTbody.rows.length; i++) {
                    arr[i] = oTbody.rows[i];
                }
                //此时arr是行构成的数组
                arr.sort(function(v1, v2) {
                    if (v1.cells[idx].innerText < v2.cells[idx].innerText) {
                        return -1;
                    } else if (v1.cells[idx].innerText > v2.cells[idx].innerText) {
                        return 1;
                    } else {
                        return 0;
                    }
                });
                for (var j = 0; j < arr.length; j++) {
                    oTbody.appendChild(arr[j]);
                }
            }
        }, false);

    }
    </script>
</body>

</html>

接下来做成插件的样子

 

posted @ 2015-05-09 13:41  cart55free99  阅读(179)  评论(0编辑  收藏  举报