![](/img/trans.png)
[英]Sorting for format (Y-m-d H:i:s) is not working in Datatables.net Plugin
[英]Datatables sort date (d/m/Y H:i A)
我還是數據表的新手。 我的日期格式是這樣的01/10/2011 10:10 AM ,我想對它進行排序。 我已經閱讀了有關可用的插件但我仍然無法正確排序,因為它只根據日期而不是月份和年份進行排序。
<script type="text/javascript" src="datatables/datatables.net/js/jquery.dataTables.js"></script>
<script type="text/javascript" src="cdn.datatables.net/plug-ins/1.10.13/sorting/date-euro.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#test').dataTable({
"columnDefs": [{
"type": "date-euro",
targets: 1
}]
});
});
</script>
<?php $filetime = date("d/m/Y H:i A",filemtime($dir.'/'.$basename)); ?>
<table id="test">
<thead>
<tr>
<th>Name</th>
<th>Date Modified</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test</td>
<td><?php echo $filetime; ?></td>
</tr>
</tbody>
您可以使用HTML5數據屬性。 https://www.datatables.net/examples/advanced_init/html5-data-attributes.html
只需將data-order元素添加到td元素即可。
<table class="table" id="exampleTable">
<thead>
<tr>
<th>Name</th>
<th>Date Modified</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test</td>
<td data-order="<?php echo date( "Y-m-d H:i", strtotime( $filetime) ); // PHP: 2015-11-13 12:00; ?>"><?php echo $filetime; ?></td>
</tr>
</tbody>
<script>
$(document).ready(function() {
$('#exampleTable').DataTable();
});
</script>
默認情況下,DataTables似乎不包含date-euro
類型,因此請確保在代碼中的某處包含了定義 :
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"date-euro-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[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1;
}
else {
x = Infinity;
}
return x;
},
"date-euro-asc": function ( a, b ) {
return a - b;
},
"date-euro-desc": function ( a, b ) {
return b - a;
}
} );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.