I faced a very similar issue with a Laravel-based project of mine. Bogdan's answer got me on the right track; however, this link managed to help me completely solve my particular issue - https://www.gyrocode.com/articles/laravel-datatables-and-sortable-datetime-carbon-objects/.
The author is using yajra/laravel-datatables package for back-end processing - it is a fantastic package, and one I enjoy using. The official link for the DataTables JQuery plugin is https://datatables.net (you can also install this with Bower and Node Package Manager). I am yet to explore DataTables in depth; however, there are numerous add-ons that DataTables also provides for extra functionality.
In the author's example, he wanted to display his dates in a human-readable format, yet be able to sort it by a sortable value (e.g. a numerical value for a timestamp).
Below are code samples taken directly from source:
PHP Code in his Laravel controller
public function indexData()
{
$users = User::select(['id', 'name', 'email', 'created_at']);
return Datatables::of($users)
->editColumn('created_at', function ($user) {
return [
'display' => e(
$user->created_at->format('m/d/Y')
),
'timestamp' => $user->created_at->timestamp
];
})
->filterColumn('created_at', function ($query, $keyword) {
$query->whereRaw("DATE_FORMAT(created_at,'%m/%d/%Y') LIKE ?", ["%$keyword%"]);
})
->make(true);
}
Here, the author is adding 'display' and 'timestamp' values to be included with each 'created_at' data value. For the 'display' value, he is altering the created_at data value in a human-friendly format. The 'timestamp' value is a numerical representation of the created_at data value - this is the value the author plans to sort the data on.
Example JSON-formatted data
The below sample of data is an example of what could be returned from the above controller:
{
"draw": 1,
"recordsTotal": 1,
"recordsFiltered": 1,
"data": [
{
"id": "1",
"name": "Tiger Nixon",
"email": "[email protected]",
"created_at": {
"display": "12/31/2016",
"timestamp": "1483142400"
}
}
]
}
Here, you can see 'display' and 'timestamp' values for the 'created_at' piece of data. As in Bogdan's answer, and mentioned earlier, the display value is for viewing by the user, and the 'timestamp' value is for sorting by the DataTables front-end.
DataTables Plugin Usage
To process the JSON-formatted data, the author has set-up the plugin as follows:
$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: '/app/users',
columns: [
{ data: 'id', name: 'id' },
{ data: 'name', name: 'name' },
{ data: 'email', name: 'email' },
{
data: 'created_at',
type: 'num',
render: {
_: 'display',
sort: 'timestamp'
}
}
]
});
Note: the author is using an AJAX call to retrieve his data in this example. You can replace ajax: '/app/users'
to data: yourJsonData
- using a variable which holds JSON data you want to process. The JSON data has been formatted specifically for use with DataTables.
Orthogonal data, with respect to DataTables, has solved quite a few future problems for me in a project I'm working on. I can see this as being very powerful, especially for usability and being practical. I hope Bogdan's answer and this example helps any future readers out there - I'm certainly glad I came across them :).
data
is a working jQuery function :) Nice to hear that it works. – Debouch