tablesorter 对其中包含 <br> 的列进行排序

tablesorter to sort columns which has <br> within them

我有一个 table,其中一个列作为日期时间:例如:1/11/2011 12:34 PM

不幸的是,列的宽度不允许我在一行中显示全长的日期时间,因此我在两行中显示内容,例如

   1/11/2011
   12:34 PM

但是如果列内容中有 <br>,则 tablesorter 将不起作用。知道如何通过 tablesorter 对这个问题进行排序吗?我有 tablesorter 修订版 2.0.5b。我无法升级到较新的版本,因为它可能会破坏 rails 应用程序的现有功能。

tablesorter 是 jquery 插件

您可能需要自定义解析器来删除回车符 return;老实说,如果允许文本换行并且您为该列设置了宽度,我认为不需要添加 <br>

无论如何,试试这个代码 (demo)

$(function () {

    $.tablesorter.addParser({
        // set a unique id
        id: 'date',
        is: function (s, table, cell) {
            // return false so this parser is not auto detected
            return false;
        },
        format: function (s, table, cell, cellIndex) {
            // replace extra spacing/carriage returns
            var str = s.replace(/\s+/g," "),
                date = new Date( str );
            return date instanceof Date && isFinite(date) ? date.getTime() : s;
        },
        // set type, either numeric or text
        type: 'numeric'
    });

    $('table').tablesorter({
        theme: 'blue',
        headers: {
            7: { sorter: 'date' }
        }
    });
});