2012-03-05 202 views
2

我正在使用JQuery tablesorter插件。该表有一列显示格式为05 Mar 2012的日期。该tablesorter插件似乎把这个列文本,因为它的顺序排序,排序日期字段与tablesorter

  • 2012年3月5日
  • 2012年1月6日
  • 2012年12月7日

我如何排序这些按照时间顺序排列?

回答

3

将日期字符串解析为日期,然后将其转换为毫秒。让tablesorter将该列作为数字进行排序。

$.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' 
      } 
     } 
    }); 
}); 

如果您在使用Date.parse,see my answer to this question时遇到问题。

0

有不在一段时间内使用tablesorter,但我似乎记得与英国日期有类似的问题。

您可以使用您的自定义日期格式将dateformat参数添加到tablesorter插件。

dateFormat: 'dd MMM yyyy' 
+0

我试过了,但没有区别。我是否需要以某种方式指出哪些列包含日期? – 2012-03-05 15:28:20

3

您还可以在日期之前以数字格式(yyyymmdd)添加隐藏的span标签。该文本将首先出现并用于排序,但它将被隐藏起来,只显示您想要的格式。

<td><span style="display:none">20130923</span>23rd September 2013</td> 
+0

这不适合我。但我添加了一个班级,而不是与这些属性:位置:绝对; \t top:0px; \t知名度:隐藏;它仍然允许分拣机读取文本,而不显示给最终用户。 – user417669 2013-12-20 03:03:28

+0

这是一个不错的简单解决方案+1 – karel 2016-06-03 20:29:15