【问题标题】:jquery tablesorter sorting empty table cells lastjquery tablesorter 最后对空表单元格进行排序
【发布时间】:2012-02-16 14:39:52
【问题描述】:

我有一个表格,其中有一列的数字大约在 -10 到 10 之间,还有一些空列。

我希望 tablesorter 始终将空列放在表格的底部。即使我按升序或降序排序。

像这样:

-1
0
2
3
<empty cell>
<empty cell>

或者像这样:

3
2
0
-1
<empty cell>
<empty cell>

请注意,我的问题与问题4792426 相同,只是在排序顺序为降序和升序时我希望底部有空单元格。

删除行不是一种选择。

【问题讨论】:

    标签: jquery tablesorter


    【解决方案1】:

    我找到了解决问题的方法。

    ssell 提供的解决方案无效。表数据只解析一次,因此不能使用全局变量来控制它。不错的尝试! :-)

    这是所需的代码。如您所见,我重载了 formatFloat 和 formatInt 函数,以便它们返回 null 而不是默认值:0。 tablesorter 中的排序功能为我神奇地完成了剩下的工作。

            $(document).ready(function () {
            $.tablesorter.formatInt = function (s) {
                var i = parseInt(s);
                return (isNaN(i)) ? null : i;
            };
            $.tablesorter.formatFloat = function (s) {
                var i = parseFloat(s);
                return (isNaN(i)) ? null : i;
            };
            $("#table").tablesorter();
        });
    

    据我所知,我的解决方案未记录在案,但效果很好。

    【讨论】:

    • 请注意,我已经复制了tablesorter on github 的副本,在最新版本中,它会自动将空表格单元格排序到底部 - demo
    【解决方案2】:

    使用更高版本的 tablesorter jQuery 插件,您可以在标题中添加不同类型的排序器,以自动将空/null 或字符串(例如,如果您输入“-”代替 null)放在您想要的位置(顶部/底部)。

    <th class="{'sorter': 'digit', 'string': 'bottom'}">Count</th>
    

    这是一种将其直接嵌入到 html/模板中的方法。但是,如果您的列数保持一致,则可以在初始 tablesorter 函数中执行此操作。

    $('table').tablesorter(
        headers: {
           1: { sorter: "digit", empty : "top" },
           2: { sorter: "digit", string: "bottom"}
        }
    )
    

    无论哪种方式都可以正常工作,但它基本上会强制您作为标题对象的第二个 k,v 放置的任何内容优先于正常方式的排序。

    更多信息可以在the tablesorter docs找到。

    【讨论】:

      【解决方案3】:

      如果它可以帮助任何人,这就是我对文本的处理方式。

      function emptyAtBottomTextSorter(a, b, direction, column, table) {
          if (a == "") {
              a = direction ? "ZZZZZ" : "";
          }
          if (b == "") {
              b = direction ? "ZZZZZ" : "";
          }
          return ((a < b) ? -1 : ((a > b) ? 1 : 0));
      }
      
      $("#myTable").tablesorter({
          headers: {
              1 : {'sorter' : 'text'}
          },
          textSorter: {
              1 : emptyAtBottomTextSorter
          }
      });
      

      从 Tablesorter v2.26.2 开始工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-15
        相关资源
        最近更新 更多