jQuery表排序仅适用于FireFox

时间:2017-05-15 14:15:55

标签: javascript php jquery sorting

我对表格列进行排序的功能在Firefox中运行良好,但仅适用于Firefox。我测试过Safari,IE11,MS Edge等等。

你们中的任何人都可以帮助我,并且可能会告诉我的代码有什么问题吗?

浏览器控制台未显示任何JavaScript错误...

    $(".sortable").click(function()
    {
        var order = ($(this).hasClass("asc") ? 'desc' : 'asc');
        var table_id = $(this).closest("table").attr('id');
        var column_id = $(this).attr('id');

        if(table_id != null && column_id != null)
        {
            $.post( "test.php", { site: getUrlVars("site"), table: $(this).closest("table").attr('id'), column: $(this).attr('id'), sort: order } );
        }

        $(this).closest("table").find(".sortable").removeClass("asc").removeClass("desc");
        $(this).addClass(order);

        var colIndex = $(this).prevAll().length;
        var tbod = $(this).closest("table").find("tbody");
        var rows = tbod.find("tr");

        rows.sort(function(a,b)
        {
            var A = $(a).find("td").eq(colIndex).text();
            var B = $(b).find("td").eq(colIndex).text();

            if (!isNaN(A)) A = Number(A);
            if (!isNaN(B)) B = Number(B);

            return (order == "asc" ? A > B : B > A);
        });

        $.each(rows, function(index, ele)
        {
            tbod.append(ele);
        });
    });

1 个答案:

答案 0 :(得分:0)

@parlad neupane

是的,它解决了我的问题。谢谢!



// old

return (order == "asc" ? A > B : B > A);

// new

return (order == "asc" ? (A < B ? -1 : B < A ? 1 : 0) : (A > B ? -1 : B > A ? 1 : 0));
&#13;
&#13;
&#13;