jquery tablesorter最后排序空表单元格

时间:2012-02-16 14:39:53

标签: jquery tablesorter

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

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

像这样:

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

或者像这样:

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

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

删除行不是一种选择。

3 个答案:

答案 0 :(得分:7)

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

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();
    });

据我所知,我的解决方案没有记录,但工作正常。

答案 1 :(得分:2)

使用更高版本的tablesorter jQuery插件,你可以在标题中添加一种不同类型的分类器来自动放入空/ null或字符串(例如,如果你输入一个“ - ”代替null)你想要的地方(顶部/底部)。

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

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

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

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

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

答案 2 :(得分:0)

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

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起作用