如何使用jquery或javascript快速排序1000行的html表

时间:2018-04-15 11:28:26

标签: javascript jquery sorting

我想用JavaScript对1000行HTML表进行排序。它必须对文本框,数字等进行排序。

它适用于100行,但当行数较大时,它会停止工作并关闭我的应用程序。

这是我的代码:

  var Customtable;
    Customtable = document.getElementById('ExampleTable');
    //alert('custom table: ' + Customtable);

    function SortCustomTable(n, Isnormal) {
        var a, b, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
        switching = true;

        //Set the sorting direction to ascending:
        dir = "asc";
        /*Make a loop that will continue until
        no switching has been done:*/
        while (switching) {
            //start by saying: no switching is done:
            switching = false;

            //rows = table.getElementsByTagName("TR");

            /*Loop through all table rows (except the
            first, which contains table headers):*/

            var rowsCount = Customtable.rows.length - 1;
            for (i = 1; i < rowsCount ; i++) {

                //start by saying there should be no switching:
                shouldSwitch = false;
                /*Get the two elements you want to compare,
                one from current row and one from the next:*/

                //x = rows[i].getElementsByTagName("TD")[n];
                //y = rows[i + 1].getElementsByTagName("TD")[n];
                /*check if the two rows should switch place,
                based on the direction, asc or desc:*/
                if (Customtable.rows[0].cells[n].className.includes("Input")) {

                    a = Customtable.rows[i].cells[n].getElementsByTagName("Input")[0].value
                    b = Customtable.rows[i + 1].cells[n].getElementsByTagName("Input")[0].value
                }
                else {
                    a = Customtable.rows[i].cells[n].textContent;
                    b = Customtable.rows[i + 1].cells[n].textContent;
                }

                if (Customtable.rows[0].cells[n].className.includes("Number")) {
                    x = Number(a)
                    y = Number(b)
                }
                else if (Customtable.rows[0].cells[n].className.includes("Text")) {
                    x = a.toLowerCase()
                    y = b.toLowerCase()
                }
                //else {
                //    x = (table.rows[i].cells[n]).innerHTML.toLowerCase();
                //    y = (table.rows[i + 1].cells[n]).innerHTML.toLowerCase();
                //}

                if (dir == "asc") {
                    if (a > b) {
                        //if so, mark as a switch and break the loop:
                        shouldSwitch = true;
                        break;
                    }
                } else if (dir == "desc") {
                    if (a < b) {
                        //if so, mark as a switch and break the loop:
                        shouldSwitch = true;
                        break;
                    }
                }
            }
            if (shouldSwitch) {
                /*If a switch has been marked, make the switch
                and mark that a switch has been done:*/
                Customtable.rows[i].parentNode.insertBefore(Customtable.rows[i + 1], Customtable.rows[i]);
                switching = true;
                //Each time a switch is done, increase this count by 1:
                switchcount++;
            } else {
                /*If no switching has been done AND the direction is "asc",
                set the direction to "desc" and run the while loop again.*/

                if (switchcount == 0 && dir == "asc" && Isnormal == 1) {
                    dir = "desc";
                    switching = true;
                }
            }
        }
        alert('Done');
    }
</script>`

1 个答案:

答案 0 :(得分:0)

正如提到的@BrahmaDev,服务器端可能是好的(假设你有权访问它)。操作和更新DOM是很昂贵的,但是如果你自己从JSON数据或其他东西渲染行(客户端模板),那么只要先对数据集进行排序并创建,那么对1000行的JSON对象进行排序应该很好而且快速。新行作为表示所有行或DOM片段的连接字符串,并一次更新DOM。

这是一个解释我在说什么的链接: https://coderwall.com/p/o9ws2g/why-you-should-always-append-dom-elements-using-documentfragments