0
我正在使用datatable plugin版本1.8.2在我的网页上显示表。jquery datatable日期排序问题
它工作正常除了。 日期对象未正确排序,它显示“无效日期”。下面是我的代码片段。
$(document).ready(function() {
jQuery.fn.dataTableExt.oSort['usdate-asc'] = function(a,b) {
/*
a and b are <div> tag with date
*/
var texta = ($(a).text()).toString(); // Here I am able to see my date in ' 03-17-2015 12:25:21 AM ' format
var textb = ($(b).text()).toString();// Here I am able to see my date in ' 03-17-2015 12:25:21 AM ' format
var usDatea = new Date(Date.parse(texta)); // Here it is showing "invalid date"
var usDateb = new Date(Date.parse(textb));
return ((usDatea < usDateb) ? -1 : ((usDatea > usDateb) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['usdate-desc'] = function(a,b) {
/*
a and b are <div> tag with date
*/
var texta = ($(a).text()).toString(); //same as above
var textb = ($(b).text()).toString(); //same as above
var usDatea = new Date(Date.parse(texta)); //same as above
var usDateb = new Date(Date.parse(textb)); //same as above
return ((usDatea < usDateb) ? 1 : ((usDatea > usDateb) ? -1 : 0));
};
$('#tablegridname').dataTable({
"sPaginationType": 'full_numbers',
"bJQueryUI": true,
"iDisplayLength": 50,
"aLengthMenu":[50,100,500,1000],
"aaSorting": [[ 4, 'desc' ]],
"aoColumns": [null, null, null, null, {"sType": "usdate"}]
});
});
});
日期如何?这听起来像是你有一些日期格式为'Data.parse()'不能识别。顺便说一句 - 你应该考虑将你的数据表版本升级到至少1.9.4版本。 – davidkonrad 2015-04-01 12:34:17
你的问题是你的日期(03-17-2015 12:25:21 AM)没有被接受的格式。你的日期是01-02-2015你期望什么行为? 1月2日(美国)或2月1日(世界大部分地区)?看看这里,并尝试以这种格式得到你的日期:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse – 2015-04-01 16:13:25
@davidkonrad我不能这样做。因为一切工作正常,除了日期排序和客户端不会除了:( – user3035305 2015-04-01 17:45:49