我正在尝试对具有类似 2009-12-17 23:59:59.0
的列的表格进行排序。
我正在使用下面的方法来应用排序
$(document).ready(function() {
$("#dataTable").tablesorter();
});
但它不适用于 yyyy-mm-dd 格式的日期。 谁能建议我如何应用这种格式进行排序?
最佳答案
正确的做法是为此自定义格式添加您自己的解析器。
检查此内容以了解其工作原理。
jQuery Tablesorter: Add your own parser
然后查看tablesorter源代码(搜索uslongdate、shortdate,然后观察tablesorter如何在内部完成日期格式的解析器。现在为您的特定日期格式构建一个类似的解析器。
这应该符合你的喜好
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 列,因为列的数组是从零开始的)
$(function() {
$("table").tablesorter({
headers: {
6: { sorter:'customDate' }
}
});
});
关于jquery - jQuery Tablesorter 的日期排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1707840/