我有一个包含四列的表,第四列是dd-mmm-yyyy格式的日期列。我在我的Java应用程序中安装了tablesorter插件。我已经重写了解析器的短期内容,如下所示,
ts.addParser({
id: "shortDate",
is: function (s) {
return (/^(\d{1,2}|\d{4})[\/\-\,\.\s+](\d{1,2}|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))[\/\-\.\,\s+](\d{1,2}|\d{4})$/).test(s);
}, format: function (s, table) {
var c = table.config;
//s = s.replace(/\-/g, "/");
s = s.replace(/\s+/g," ").replace(/[\-|\.|\,]/g, "/");
if (c.dateFormat == "us") {
// reformat the string in ISO format
s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$1/$2");
} else if (c.dateFormat == "uk") {
// reformat the string in ISO format
s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$2/$1");
} else if (c.dateFormat == "dd/mm/yy" || c.dateFormat == "dd-mm-yy") {
s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/, "$1/$2/$3");
} else if (c.dateFormat == "dd-mmm-yy") {
var months = {Jan:1, Feb:2, Mar:3, Apr:4,May:5,Jun:6,Jul:7,Aug:8,Sep:9,Oct:10,Nov:11,Dec:12};
s = s.replace(/(\d{1,2})[\/\s](Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\/\s](\d{2,4})/, function(str,p1,p2,p3) { if (parseInt(p3)>60) {p3='19'+p3;}else{p3='20'+p3;}return p3 + "/" + months[p2] + "/" + p1;});
}
return $.tablesorter.formatFloat(new Date(s).getTime());
}, type: "numeric"
});
解析器在Mozilla和Chrome中运行良好,但在IE 10中仅用于日期列。其他列在IE中进行排序。
我已经从我的JSP调用了我的表,如下面的Javascript onload,
$("#myTable").tablesorter({
headers: {
0: {sorter:"string"},
1: {sorter:"string"},
2: {sorter:"numeric"},
3: {sorter:"shortDate",dateFormat: "dd-mmm-yyyy"}
}
});
请告诉我我遇到问题的地方。
答案 0 :(得分:0)
只是快速查看 - 我实际上没有测试解析器 - 在tablesorter配置中设置dateFormat
而不是在标头定义中:
$("#myTable").tablesorter({
dateFormat: "dd-mmm-yy", // use "yy" not "yyyy"
headers: {
0: {sorter:"string"},
1: {sorter:"string"},
2: {sorter:"numeric"},
3: {sorter:"shortDate"}
}
});