使用表格排序日期字段 [英] sort date field with tablesorter

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

问题描述

我正在使用JQuery tablesorter插件。该表格有一列显示格式为 2012年3月5日的日期。 tablesorter插件似乎将此列视为文本,因为它按顺序排序




  • 2012年3月5日

  • 06 Jan 2012

  • 2012年12月07日



如何对这些日期进行排序按时间顺序排列?

解决方案

将日期字符串解析为Date,然后将其转换为毫秒。我们将列表列为数字。

  $。tablesorter.addParser({
id:'my_date_column',
是:function(s){
// return false所以这个解析器不是自动检测的
return false;
},
格式:function(s){
var timeInMillis = new Date.parse(s);
return timeInMillis;
},
//设置类型,数字或文本
类型:'numeric'
});

$(function(){
$(table)。tablesorter({
headers:{
6:{//将此更改为您的列位置
sorter:'my_date_column'
}
}
});
});

如果您遇到Date.parse,请参阅我对这个问题的回答


I'm using the JQuery tablesorter plugin. The table has a column that shows dates in the format 05 Mar 2012. The tablesorter plugin seems to treat this column as text, because it sorts it in the order

  • 05 Mar 2012
  • 06 Jan 2012
  • 07 Dec 2012

How can I sort these dates in chronological order instead?

解决方案

Parse the date string to a Date, then convert it to milliseconds. Let tablesorter sort the column as numeric.

$.tablesorter.addParser({ 
    id: 'my_date_column', 
    is: function(s) { 
        // return false so this parser is not auto detected 
        return false; 
    }, 
    format: function(s) { 
        var timeInMillis = new Date.parse(s);
        return timeInMillis;         
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 

$(function() { 
    $("table").tablesorter({ 
        headers: { 
            6: {       // Change this to your column position
                sorter:'my_date_column' 
            } 
        } 
    }); 
});

If you have trouble with Date.parse, see my answer to this question.

这篇关于使用表格排序日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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