我使用Jquery tablesorter并且需要对日期时间进行排序。
格式: - 2016年5月2日星期一12:31:00
目前,我正在使用" time"分拣机,但这不起作用。
2:{
sorter: 'time'
}
我可以使用现有的分拣机吗?
答案 0 :(得分:0)
如果你正在使用我的fork of tablesorter,这应该可以在没有任何额外代码的情况下使用demo):
$(function() {
$('table').tablesorter({
headers: {
0: {
sorter: 'time'
}
}
});
});
如果你正在使用原始的tablesorter,那么请包含这个解析器(demo):
$(function() {
var ts = $.tablesorter,
dateReplace = /(\S)([AP]M)$/i,
// match 24 hour time & 12 hours time + am/pm - see http://regexr.com/3c3tk
timeTest = /^([1-9]|1[0-2]):([0-5]\d)(\s[AP]M)$|^((?:[01]\d|[2][0-4]):[0-5]\d)$/i,
timeMatch = /([1-9]|1[0-2]):([0-5]\d)(\s[AP]M)|((?:[01]\d|[2][0-4]):[0-5]\d)/i;
ts.addParser({
id: 'time2',
is: function(str) {
return timeTest.test(str);
},
format: function(str, table) {
// isolate time... ignore month, day and year
var temp,
timePart = (str || '').match(timeMatch),
orig = new Date(str),
// no time component? default to 00:00 by leaving it out, but only if
// str is defined
time = str && (timePart !== null ? timePart[0] : '00:00 AM'),
date = time ? new Date('2000/01/01 ' + time.replace(dateReplace, '$1 $2')) : time;
if (date instanceof Date && isFinite(date)) {
temp = orig instanceof Date && isFinite(orig) ? orig.getTime() : 0;
// if original string was a valid date, add it to the decimal so the column
// sorts in some kind of order; luckily new Date() ignores the decimals
return temp ? parseFloat(date.getTime() + '.' + orig.getTime()) : date.getTime();
}
return str;
},
type: 'numeric'
});
$('table').tablesorter({
headers: {
0: {
sorter: 'time2'
}
}
});
});