MedicOneSystems / livewire-datatables

Advanced datatables using Laravel, Livewire, Tailwind CSS and Alpine JS
https://livewire-datatables.com/
MIT License
1.19k stars 259 forks source link

Sorting not working properly #391

Open ItsJoshDaly opened 2 years ago

ItsJoshDaly commented 2 years ago

I've been trying to sort the datatables on my project by a certain hidden column - in this case it was "created_at". I had attempted a number of methods and strangely, the only thing that seemed to ever behave correctly was the flag for "asc" or "desc".

For instance, I tried to use the props...

<livewire:tables.companies :excludeTrashed=true sort="created_at|asc"/>

Whether or not the column was visible, it would simply sort by name ascending or descending, although it did change depending on if I used "asc" or "desc" in the props. I also tried setting a public $sort which ended up with similar results. I even tried using the sortBy column method to absolutely no avail.

DateColumn::name('created_at') ->label('Created At') ->sortBy('DATE_FORMAT') ->hide()

I also tried making it searchable and simply just referring to it as if it were a regular column, none of which worked. I can also verify that even filtering by name didn't return the desired data because I had cross-compared what the datatable returned against DataGrip only to find that the ordering was wrong.

Any help would be muchly appreciated, thanks!

breizhwave commented 2 years ago

I found some sorting issues too, and solved by hacking the code vendor/mediconesystems/livewire-datatables/src/Http/Livewire/LivewireDatatable.php at line 727 : replace quote with back ticks ... what do you think

  : new Expression("\`" . $column['name'] . "`");
richmarisa commented 2 years ago

Using back ticks in this line solved this for me, too.

shaneburrell commented 2 years ago

That breaks many other database types. Someone has started doing proper database typing but I don't believe it was ever finished.