【问题标题】:jQuery table sort code only working in Mozilla FirefoxjQuery 表格排序代码仅适用于 Mozilla Firefox
【发布时间】:2016-08-02 06:40:22
【问题描述】:

我使用 charlietfl 对 StackOverflow 中this 问题的回答对我的 HTML 表中从 1 月到 12 月的月份列进行排序。我只是修改了一些东西,所以我可以在我的网站上使用它。我也在使用tablesorter,所以我可以按字母顺序对其他表格列进行排序。问题是它只适用于 Mozilla Firefox。其他浏览器不会对月份列进行排序。有时 包裹表头的 tr 跳出 thead 标记并进入 tbody 标记。这是我的桌子:http://makzel.com/problems/

这可能是什么原因造成的?

这是我的完整 jquery 代码:

(function($) {

    'use strict';

    var $window = $(window);

    $window.ready(function(){

        // Enable sorting for tables
        $(".tablesort").tablesorter({
            headers: {
                0: {
                    sorter: false
                }
            }
        }); 

        // Sort by month
        $('.tablesort th').click(function(){
            var sorters =['January','February','March','April','May','June','July','August','September','October','November','December']

                    var $rows = $('tr:gt(0)').sort(function(a, b){
                        var aSrcIdx =sorters.indexOf( $(a).find('td:first').text() );
                        var bSrcIdx = sorters.indexOf( $(b).find('td:first').text());

                        return aSrcIdx >  bSrcIdx;    
                    });

            $(this).closest('.tablesort').append($rows);
        });

        // Tablesort header background change on click
        $(".tablesort th").click(function(){
            $('.tablesort th').not(this).removeClass('sorted');
            $(this).toggleClass('sorted');
         });

        function postOverflow() {
            if ($window.width() < 768) {
                $('.tablesort').closest('.post').css("overflow","scroll");

            } else{
                $('.tablesort').closest('.post').css("overflow","none");
            }
        }
        postOverflow();
        $window.resize(postOverflow);

    }); // Ready

})(jQuery);

这里是它的链接,它显示了 tr 标签跳出 thead 标签:http://cms5.revize.com/revize/haddonfield/departments/table_sort_test/index.php

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    根据documentation of array.sort,尝试返回-1 将a 置于b 之下,0 保持不变,或1 将b 置于a 之下。 尝试替换:

    return aSrcIdx > bSrcIdx;    
    

    return aSrcIdx < bSrcIdx ? -1 : bSrcIdx < aSrcIdx ? 1 : 0;    
    

    【讨论】:

    • 您指的是 tr:gt(0) 代码中的 0 吗?我对 Javascript 还不是很流利,所以我不知道它的作用。我会尝试将其更改为 -1
    • 是的,return aSrcIdx &gt; bSrcIdx; 应该更改。
    • 好的。我应该用什么替换它?
    • @Chad 我已经编辑了我的答案以提供 sn-p。
    猜你喜欢
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多