gpt4 book ai didi

javascript - JQuery 数据表中带有时间戳排序问题的日期

转载 作者:行者123 更新时间:2023-11-29 23:52:06 26 4
gpt4 key购买 nike

我正在使用 jquery 数据表插件列出我的 java spring MVC web 应用程序中的数据。我在表中的字段之一包含带有时间戳的日期。在那方面,我一直在尝试对数据元素进行排序。当我尝试对包含带有时间戳的日期的字段中的元素进行排序时,排序不起作用。

我正在使用以下 jquery 进行数据表初始化

 $('.swcm-dt-basic').dataTable( {
"responsive": true,
"order": \[\],
"language": {
"paginate": {
"previous": '<i class="swcm-psi-arrow-left"></i>',
"next": '<i class="swcm-psi-arrow-right"></i>'
}
}
} );

下图显示了问题 Error in Sorting

最佳答案

解决方案 1

为包含可排序时间戳的 td 元素使用 data-order 属性。例如:

<td data-order="2016-12-02 21:28:41">12/02/2016 21:28:41</td>

参见 this example用于代码和演示。

解决方案 2

使用sorting plugins喜欢datetime-moment对于任何日期格式或 date-euro如果您的日期是 DD/MM/YYYY HH:MM:SS 格式。

如果您的日期是MM/DD/YYYY HH:MM:SS,您可以使用以下代码:

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"date-us-pre": function ( a ) {
var x;

if ( $.trim(a) !== '' ) {
var frDatea = $.trim(a).split(' ');
var frTimea = (undefined != frDatea[1]) ? frDatea[1].split(':') : [00,00,00];
var frDatea2 = frDatea[0].split('/');
x = (frDatea2[2] + frDatea2[0] + frDatea2[1] + frTimea[0] + frTimea[1] + frTimea[2]) * 1;
}
else {
x = Infinity;
}

return x;
},

"date-us-asc": function ( a, b ) {
return a - b;
},

"date-us-desc": function ( a, b ) {
return b - a;
}
} );

$(document).ready(function(){
$('#example').dataTable( {
columnDefs: [
{ type: 'date-us', targets: 0 }
]
} );
}

关于javascript - JQuery 数据表中带有时间戳排序问题的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42563449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com