jQuery Tablesorter 无法处理日期
jQuery Tablesorter is not working with date
我的代码:
<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"
});
有人能帮帮我吗?
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'
});
});
我的代码:
<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"
});
有人能帮帮我吗?
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'
});
});