我正在使用 JQuery 表排序器插件。该表有一列显示日期格式05 Mar 2012
。 tablesorter 插件似乎将此列视为文本,因为它按顺序对其进行排序
- 2012 年 3 月 5 日
- 2012 年 1 月 6 日
- 2012 年 12 月 7 日
我怎样才能按时间顺序对这些日期进行排序?
将日期字符串解析为日期,然后将其转换为毫秒。让 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 时遇到问题,看我对这个问题的回答 https://stackoverflow.com/questions/5802461/javascript-which-browsers-support-parsing-of-iso-8601-date-string-with-date-par/5803041#5803041.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)