gpt4 book ai didi

javascript - 使用 tablesorter 对日期字段进行排序

转载 作者:行者123 更新时间:2023-12-01 02:34:06 26 4
gpt4 key购买 nike

我正在使用 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 时遇到问题,see my answer to this question .

关于javascript - 使用 tablesorter 对日期字段进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9568473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com