数据表日期排序 dd/mm/yyyy 问题 [英] Datatable date sorting dd/mm/yyyy issue
问题描述
我正在使用一个名为 datatables
太棒了,但是我无法根据 dd/mm/yyyy 格式正确排序日期.
Its fantastic, however I cannot get the dates to sort correctly according to the dd/mm/yyyy format.
我查看了他们的支持格式,但这些修复似乎都不起作用.
I have looked at their support formats but none of these fixes seem to work.
这里有人可以帮我吗?
推荐答案
更新 2020:HTML 解决方案
由于 HTML 5 得到了如此多的发展并且几乎所有主要浏览器都支持它.所以现在更简洁的方法是使用 HTML5 数据属性 (maxx777 提供了一个 PHP 解决方案,我使用的是简单的 HTML).对于我们场景中的非数字数据,我们可以使用 data-sort
或 data-order
属性并为其分配一个可排序的值.
Update 2020: HTML Solution
Since HTML 5 is so much developed and almost all major browser supporting it. So now a much cleaner approach is to use HTML5 data attributes (maxx777 provided a PHP solution I am using the simple HTML). For non-numeric data as in our scenario, we can use data-sort
or data-order
attribute and assign a sortable value to it.
HTML
<td data-sort='YYYYMMDD'>DD/MM/YYYY</td>
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"date-uk-pre": function ( a ) {
var ukDatea = a.split('/');
return (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
},
"date-uk-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"date-uk-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );
将上述代码添加到脚本中,并使用 { "sType": "date-uk"; 设置具有日期值的特定列.}
和其他为空,见下文:
Add the above code to script and set the specific column with Date values with { "sType": "date-uk" }
and others as null, see below:
$(document).ready(function() {
$('#example').dataTable( {
"aoColumns": [
null,
null,
null,
null,
{ "sType": "date-uk" },
null
]
});
});
这篇关于数据表日期排序 dd/mm/yyyy 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!