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 :-)
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
Most helpful comment
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' } }