Laravel-datatables: Order by Date

Created on 22 Sep 2017  路  10Comments  路  Source: yajra/laravel-datatables

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
need feedback question

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' } }

All 10 comments

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahmadbadpey picture ahmadbadpey  路  3Comments

ghost picture ghost  路  3Comments

FilipeBorges1993 picture FilipeBorges1993  路  3Comments

Mopster picture Mopster  路  3Comments

sangnguyenplus picture sangnguyenplus  路  3Comments