laravel-datatables
laravel-datatables copied to clipboard
Order by Date
is it possible to order columns by date? I added a editColumn-function, where I format the date for my timezone ('d.m.Y'). Unfortunatley the sorting order is wrong, because laravel-datatables sorts by string. So I tried it by returning the date, formatted in my timezone & a timestamp which should be the sorting value:
->editColumn('created', function($col) {
return [
'display' =>($col->created && $col->created != '0000-00-00 00:00:00') ? with(new Carbon($col->created))->format('d.m.Y') : '',
'timestamp' =>($col->created && $col->created != '0000-00-00 00:00:00') ? with(new Carbon($col->created))->timestamp : ''
];
})
Inside the JS I added this:
{ 'name': 'created', 'data': 'created', 'type': 'num', 'render': { '_': 'display', 'order': 'timestamp' } }
Datatables displays everything correctly, but when I want to sort by date, I get 'undefined index: created'. Inside CollectionEngine->ordering() it fails while $cmp = strnatcasecmp($first[$column], $second[$column]); because column is now created.timestamp/created.display and not created anymore.
So, in the end, is it possible to sort by date and if so, can anyone provide an example?
Thank you very much :-)
- Ubuntu 16.04
- PHP 7.0.18
- Laravel 5.4.36
- Laravel-Datatables 7.10.1
Anyone has an example how to order by date?
This is the default behavior when using collection CollectionEngine
. Why not use query builder / eloquent instead? It seems like you are loading data from a table?
I've tried it with Query Builder and not ordering the date column. Change icon ordering but not reorder
Getting the same issue here, Any update ?
I did it with some workaround by setting data-sort attribute on the td. I setted the timestamp there - so 1.1.1800 is 0 in data-sort. With that workaround it was working for me. Maybe you can do something with that @yajra
Please refer this https://datatables.net/examples/ajax/orthogonal-data.html
undefined index: created to solve this error.
change this
{ 'name': 'created', 'data': 'created', 'type': 'num', 'render': { '_': 'display', 'order': 'timestamp' } }
to
{ 'name': 'created.timestamp', 'data': { '_': 'created.display', 'sort': 'created' } }
It's working for me with this
JS
{ 'name': 'created', 'data': { '_': 'created.display', 'sort': 'created.timestamp' } }
PHP
[ 'name' => 'created', 'data' => [ "_" => 'created.display', "sort" => 'created.timestamp' ] ]
undefined index: created to solve this error. change this
{ 'name': 'created', 'data': 'created', 'type': 'num', 'render': { '_': 'display', 'order': 'timestamp' } }
to{ 'name': 'created.timestamp', 'data': { '_': 'created.display', 'sort': 'created' } }
This one solves my problem. So you need to specify full path in the name for sorting portion and rest is the same.
It's working for me with this JS
{ 'name': 'created', 'data': { '_': 'created.display', 'sort': 'created.timestamp' } }
PHP
[ 'name' => 'created', 'data' => [ "_" => 'created.display', "sort" => 'created.timestamp' ] ]
thank you! this solve my problem too
It's working for me with this JS
{ 'name': 'created', 'data': { '_': 'created.display', 'sort': 'created.timestamp' } }
PHP[ 'name' => 'created', 'data' => [ "_" => 'created.display', "sort" => 'created.timestamp' ] ]
thank you! this solve my problem too
Can you filter/search the data in datatables ? when I use the method above, the data perfectly sorted with (d.m.Y) format, but when filtering it, the data that matches the display does not want to appear, it can only search for data with a timestamp format.
Hi, i have the same issue as @nugraha-a. any help will be appreciated
Thanks
i solved the issue by doing {data: { _: "created.display", sort: "created.timestamp", filter: 'created.display' }, name: 'created.display'},
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.
This issue was closed because it has been inactive for 7 days since being marked as stale.