laravel-datatables icon indicating copy to clipboard operation
laravel-datatables copied to clipboard

Order by Date

Open piccard21 opened this issue 6 years ago • 13 comments

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

piccard21 avatar Sep 22 '17 14:09 piccard21

Anyone has an example how to order by date?

piccard21 avatar Sep 25 '17 08:09 piccard21

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?

yajra avatar Sep 26 '17 01:09 yajra

I've tried it with Query Builder and not ordering the date column. Change icon ordering but not reorder

carlituxman avatar Nov 24 '17 09:11 carlituxman

Getting the same issue here, Any update ?

oscarbatlle avatar Feb 14 '18 16:02 oscarbatlle

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

PrimeGhostDE avatar Apr 05 '18 16:04 PrimeGhostDE

Please refer this https://datatables.net/examples/ajax/orthogonal-data.html

ajaypatidarlipl avatar Jun 27 '19 15:06 ajaypatidarlipl

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

ajaypatidarlipl avatar Jun 28 '19 06:06 ajaypatidarlipl

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

huuloc4193 avatar Feb 06 '20 09:02 huuloc4193

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.

whysoawesome0207 avatar May 14 '20 12:05 whysoawesome0207

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

francescotaioli avatar Jul 13 '20 09:07 francescotaioli

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.

nugraha-a avatar Nov 19 '21 08:11 nugraha-a

Hi, i have the same issue as @nugraha-a. any help will be appreciated

Thanks

sys-auditing avatar Dec 02 '21 15:12 sys-auditing

i solved the issue by doing {data: { _: "created.display", sort: "created.timestamp", filter: 'created.display' }, name: 'created.display'},

sys-auditing avatar Dec 02 '21 17:12 sys-auditing

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.

github-actions[bot] avatar Dec 06 '23 00:12 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale.

github-actions[bot] avatar Dec 13 '23 00:12 github-actions[bot]