I wish to show the records using datatables with default ordering based on one of my rows with date & time in descending order. Please help me in editing the jquery stru
I had same problem. I used date-eu sorting plugin to sort dates in the format DD/MM/YY and I included the following JS file :
<script src="//cdn.datatables.net/plug-ins/1.10.11/sorting/date-eu.js" type="text/javascript"></script>
This worked for me.
$('#exemple').DataTable({
"order": [[ 3, "desc" ]], //or asc
"columnDefs" : [{"targets":3, "type":"date-eu"}],
});
Read also this post on stackoverflow: Sorting date in datatable
Please refer to this pen: https://codepen.io/arnulfolg/pen/MebVgx
It uses //cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js and //cdn.datatables.net/plug-ins/1.10.12/sorting/datetime-moment.js for sorting datatable
To sort the table by default use:
$.fn.dataTable.moment('DD/MM/YY');
$('#example').DataTable({
"order": [[ 3, "desc" ]]
});
This was the answer for me:
<td data-order=<fmt:formatDate pattern = "yyyy-MM-dd" value = "${myObject.myDate}" />>${myObject.myDate}</td>
more details, here in the html5 section: https://datatables.net/manual/data/
<td class="sorting_1">
<span style="display:none;">201909010930</span>09/01/2019 09:30
</td>
Format your date in yyyyMMddHHmm. This will be your sortable timestamp. Then hide the formatted date using display none. This is actually a further explanation of the answer of joan16v
//working here code
$('#table').DataTable({
columnDefs: [ { type: 'date', 'targets': [3] } ],
order: [[ 3, 'desc' ]],
});
Here the code:
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));
}
});