jQuery Tablesorter的日期排序问题 [英] date Sorting Problem with Jquery Tablesorter

查看:53
本文介绍了jQuery Tablesorter的日期排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to sort a table which has column like 2009-12-17 23:59:59.0. I am using below to apply sort

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

但是它不适用于格式为yyyy-mm-dd的日期. 有人可以建议我如何应用这种格式进行排序吗?

But its not working for the dates of format yyyy-mm-dd. Can any one suggest how can i apply this format for sorting?

推荐答案

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

The right thing to do would be to add your own parser for this custom format.

选中此项以了解其工作原理.

Check this to get a grasp on how that works.

jQuery Tablesorter:添加您自己的解析器

然后查看表排序器源(搜索uslongdate,shortdate,然后观察表排序器如何在内部完成日期格式的解析器.现在,为您自己的日期构造一个类似的解析器.

Then take a look into the tablesorter source (search for uslongdate, shortdate and then watch how the parsers for date formats are internally done by tablesorter. Now construct your self a similar parser for your particular date format.

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列,因为列的数组是从零开始的)

Now just apply it to the column where you have this format (e.g. assuming the column your custom dates reside in are in column No. 7. (6 means column 7, because the array of the columns is zerobased)

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

这篇关于jQuery Tablesorter的日期排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆