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
Just add "type":"date"
directly in the columns like { "data": "MyDateField", "type":"date" }
.
Try this, It works for me
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script src="https://cdn.datatables.net/plug-ins/1.10.21/sorting/datetime-moment.js"></script>
<script>
$(document).ready(function () {
$.fn.dataTable.moment( 'DD/MM/YYYY HH:mm' );
$('#example').DataTable({"order": [[ 3, "desc" ]]});
});
</script>
Default sorting in Datatables:
$(document).ready(function() {
$('#example').DataTable({
"order": [[ 3, "desc" ]]
});
});
You can use asc for ascending order. And 3 means, 4th column is going to be ordered default.
I got the solution with the sorting of date. Just add type as 'date' and in targets, you have pass column number(count start from 0) with datatable options. And set 'order' with column number and type of format. See below code,
columnDefs: [ { type: 'date', 'targets': [4] } ],
order: [[ 4, 'desc' ]]
Perfect solution that I could implement is this:
$rows[] =
[
"name" => $name,
"date" => [
"display" => $date, // Ex: '31.12.2020'
"timestamp" => strtotime($date), // Timestamp value for ordering, Ex: 1609372800
]
]
{
"name": "Vasya Pupkin",
"date": {
"display": "31.12.2020",
"timestamp": "1609372800"
},
}
{
"data": "date",
render: {
_: 'display',
sort: 'timestamp'
}
},
The simpliest way is to add a hidden timestamp before the date in every TD tag of the column, for example:
<td class="sorting_1">
<span style="display:none;">1547022615</span>09/01/2019 09:30
</td>
With the default string ordering, a timestamp would order the column the way you want and it will not be shown when rendered in the browser.