这是代码..
$.fn.dataTableExt.oSort['us_date-asc'] = function (a, b) {
var x = new Date(a),
y = new Date(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
$.fn.dataTableExt.oSort['us_date-desc'] = function (a, b) {
var x = new Date(a),
y = new Date(b);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
var oTable = $('#example').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
//"bSortClasses": false,
"aoColumns": [
null, null,
{
"sType": "us_date"
},
null,
{
"bSortable": false
}],
"aaSorting": [
[2, "desc"]
]
});我使用datatable的这段代码来使第三列可排序。我想显示的日期在6-06-2012格式的.The排序工作时,我使用06-06-2012格式...但是当按字母顺序表示月份时,排序不起作用(它在chrome中起作用,但在其他浏览器中不起作用)。我该如何解决这个问题呢?任何帮助我们都将不胜感激。
发布于 2012-07-03 14:41:40
jQuery.fn.dataTableExt.oSort['shortdate-asc'] = function(x,y) {
var months = {};
months["JAN"] = "01";
months["FEB"] = "02";
months["MAR"] = "03";
months["APR"] = "04";
months["MAY"] = "05";
months["JUN"] = "06";
months["JUL"] = "07";
months["AUG"] = "08";
months["SEP"] = "09";
months["OCT"] = "10";
months["NOV"] = "11";
months["DEC"] = "12";
x = (x=="")? 0 : x.split('-');
y = (y=="")? 0 : y.split('-');
if(x.length){
x = x[2] + months[x[0].toUpperCase()] + x[1];
}
if(y.length){
y = y[2] + months[y[0].toUpperCase()] + y[1];
}
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['shortdate-desc'] = function(x,y) {
var months = {};
months["JAN"] = "01";
months["FEB"] = "02";
months["MAR"] = "03";
months["APR"] = "04";
months["MAY"] = "05";
months["JUN"] = "06";
months["JUL"] = "07";
months["AUG"] = "08";
months["SEP"] = "09";
months["OCT"] = "10";
months["NOV"] = "11";
months["DEC"] = "12";
x = (x=="")? 0 : x.split('-');
y = (y=="")? 0 : y.split('-');
if(x.length){
x = x[2] + months[x[0].toUpperCase()] + x[1];
}
if(y.length){
y = y[2] + months[y[0].toUpperCase()] + y[1];
}
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}; 发布于 2012-07-03 15:02:00
您可以使用像datejs这样的库将arbit字符串转换为实际的Date对象,并对它们进行比较。这个想法是将你正在显示的内容与你实际比较的内容分开。
示例代码:
var x = Date.parse('06-jun-2012');
var y = Date.parse('06-jul-2012');
return Date.compare(x, y);可以使用date对象的toString()方法并指定自定义格式字符串。请查看文档http://code.google.com/p/datejs/wiki/APIDocumentation
如果这是一个一次性的任务,你可能不想依赖另一个库。然而,如果你碰巧在你的应用程序中操作日期,你会想要使用这个库。
https://stackoverflow.com/questions/11305202
复制相似问题