我正在尝试在我的asp.net应用程序中使用TableSorter,但这不符合我的格式。 如果强制我的列为shortDate,我想编辑此解析器以使用我的格式
$(document).ready(function () {
// initialize stickyTableHeaders _after_ tablesorter
$(".tablesorter").tablesorter(
{ headers:
{
3: { sorter: "shortDate" },
4: { sorter: "shortDate" },
5: { sorter: "usLongDate" },
6: { sorter: "shortDate" },
7: { sorter: "shortDate" },
8: { sorter: "shortDate" },
9: { sorter: "shortDate" }
}
});
$("table").stickyTableHeaders();
});
我的解析器代码:
ts.addParser({
id: "shortDate",
is: function (s) {
return /\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/.test(s);
}, format: function (s, table) {
var c = table.config;
s = s.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 {
s = s.replace(".", "");
// s1 = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/, "$1/$2/$3");
var s1 = s.substr(3);
}
return $.tablesorter.formatFloat(new Date(s1).getTime());
}, type: "numeric"
});
答案 0 :(得分:2)
答案:
monthDict = {
'janv': '01', 'févr': '02', 'mars': '03',
'avr': '04', 'mai': '05', 'juin': '06',
'juil': '07', 'août': '08', 'sept': '09',
'oct': '10', 'nov': '11', 'déc': '12'
}
ts.addParser({
id: "shortDate",
is: function (s, table, cell) { return false; },
format: function (s, table, cell, cellIndex) {
var dat = s.split('.');
var dates = dat[1].trim().split(' ');
dat[0] = (/^\d{2}$/.test(dates[0])) ? dates[0] : '0' + dates[0];
return monthDict[dates[1]]+dat[0]
//dat[0] = (/^\d{2}$/.test(dat[0])) ? dat[0] : '0' + dat[0]; //If 'dd' format is single digit, append 0 to it.
//return dat[2] + monthDict[dat[1]] + dat[0]; //Sort in 'yyyymmdd' format as priority of sorting
},
type: "numeric"
});