jquery - jQuery Tablesorter 的日期排序问题

标签 jquery tablesorter

我正在尝试对具有类似 2009-12-17 23:59:59.0 的列的表格进行排序。 我正在使用下面的方法来应用排序

$(document).ready(function() { 
    $("#dataTable").tablesorter();  
});

但它不适用于 yyyy-mm-dd 格式的日期。 谁能建议我如何应用这种格式进行排序?

最佳答案

正确的做法是为此自定义格式添加您自己的解析器。

检查此内容以了解其工作原理。

jQuery Tablesorter: Add your own parser

然后查看tablesorter源代码(搜索uslongdate、shortdate,然后观察tablesorter如何在内部完成日期格式的解析器。现在为您的特定日期格式构建一个类似的解析器。

jquery.tablesorter.js

这应该符合你的喜好

ts.addParser({
    id: "customDate",
    is: function(s) {
        //return false;
        //use the above line if you don't want table sorter to auto detected this parser
        //else use the below line.
        //attention: doesn't check for invalid stuff
        //2009-77-77 77:77:77.0 would also be matched
        //if that doesn't suit you alter the regex to be more restrictive
        return /\d{1,4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}\.\d+/.test(s);
    },
    format: function(s) {
        s = s.replace(/\-/g," ");
        s = s.replace(/:/g," ");
        s = s.replace(/\./g," ");
        s = s.split(" ");
        return $.tablesorter.formatFloat(new Date(s[0], s[1]-1, s[2], s[3], s[4], s[5]).getTime()+parseInt(s[6]));
    },
    type: "numeric"
});

现在只需将其应用于具有此格式的列(例如,假设您的自定义日期所在的列位于第 7 列中。(6 表示第 7 列,因为列的数组是从零开始的)

$(function() {
    $("table").tablesorter({
        headers: {
            6: { sorter:'customDate' }
        }
    });
});

关于jquery - jQuery Tablesorter 的日期排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1707840/

相关文章:

jQuery Tablesorter - 禁用排序和过滤

php - 使用 PHP 对表进行排序

javascript - jQuery 表排序器 : How to detect if column is already sorted?

jquery - tablesorter jquery 获取过滤后数字列的总和

php - 插入 SQL 时保留 JSON 数据的顺序

javascript - 'load.tds' DOM 事件类型和 jQuery .bind()

javascript - 我可以在变量上创建任何触发器吗?

javascript - Tablesort 使用 Ajax 在单击时重新加载表数据?

javascript - 将\n 换行符转换为 HTML <BR> 换行符

javascript - 在我打开开发者工具之前,IE 不加载 XML 内容?