1
我正在一個項目中,我收到一個jQuery DataTable的日期作爲字符串格式(mm/dd/yyyy)。當我進行排序時,它會將它排序爲一個字符串,導致它按月份順序分組,這是不正確的。我想用'sType:date'來處理排序,但因爲它不是日期對象,所以不會對它排序。有沒有辦法將它添加到數據表中時將其解析爲日期?jQuery DataTable排序美國日期字符串
我發現這樣做的,正確排序它的另一種方式,但它只能在比IE8等瀏覽器上運行(該表有〜1000行,它會導致腳本超時錯誤,Chrome,但是它即刻)
jQuery.fn.dataTableExt.oSort['us_date-asc'] = function(a,b) {
if($(a).text()=="" && $(b).text()==""){
return 0;
}
else if($(a).text()=="" || $(b).text()==""){
return $(a).text()=="" ? -1 : 1;
}
else{
var x = new Date($(a).text());
var y = new Date($(b).text());
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
};
jQuery.fn.dataTableExt.oSort['us_date-desc'] = function(a,b) {
if($(a).text()=="" && $(b).text()==""){
return 0;
}
else if($(a).text()=="" || $(b).text()==""){
return $(a).text()=="" ? 1 : -1;
}
else{
var x = new Date($(a).text());
var y = new Date($(b).text());
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}
};
我可以再與
"sType": 'us_date'
使用上有什麼我可以做任何建議嗎?
+1,是的 - 這是一個很好的解決方案。你應該接受你自己的答案,可能在將來幫助其他人,也將其標記爲已解決。 – davidkonrad