我的代码:
<th class="sorter-shortDate">Date</th> $('table').tablesorter({dateFormat: "yyyymmdd"});
排序适用于数字,但不适用于日期,我的日期格式是y-m-d H:i,我还尝试添加自定义解析器:
$.tablesorter.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}/.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]).getTime()+parseInt(s[6]));
},
type: "numeric"
});有谁能帮帮我吗?
发布于 2020-03-06 11:45:33
shortDate解析器中有一个小错误;它将所有.替换为/,同时将日期转换为内置JS日期解析器可以理解的内容(例如,2009-12-31 08:09:10.1变为12/31/2009 08:09:10/1 -将最后一个小数位改为斜杠将破坏所有内容。
您可以通过修改短日期解析器的正则表达式demo来解决此问题
$.tablesorter.regex.shortDateReplace = /-/g;
$(function() {
$('table').tablesorter({
theme: 'blue',
dateFormat: 'yyyymmdd'
});
});https://stackoverflow.com/questions/60548333
复制相似问题