How to have default sorting for table? #261
Replies: 6 comments 23 replies
-
Hello, |
Beta Was this translation helpful? Give feedback.
-
Can you check this code : |
Beta Was this translation helpful? Give feedback.
-
So adding to this really old discussion, the non-panel defaults are documented here: https://filamentphp.com/docs/2.x/tables/columns/getting-started#sorting Using it with filament 2, I notice the default ordering works to order the table, but the table heading does not reflect the ordering with the downward chevron (⌄) remaining greyed out to sugnify "order not chosen". I've not raised this as an issue, but can if it is a real issue and not just my app. |
Beta Was this translation helpful? Give feedback.
-
I can't see how that would work from the docs (v2 or v3), but I'll have a dig through the code to see how. |
Beta Was this translation helpful? Give feedback.
-
For v2: https://filamentphp.com/docs/2.x/admin/resources/listing-records#customizing-the-eloquent-query I was able to change the default ordering thanks to this doc. |
Beta Was this translation helpful? Give feedback.
-
Customising the eloquent query is good for an underlying sorting, that the filamant table sorting sits on top of. Just be aware that the sorting column the user then selects in the UI does not replace the default sorting you add to the eloquent query. So if you order by post date on your eloquent query, and the user selects to order by the post title, then the result will be post titles ordered within post dates. I've found this works quite well for my purposes: public function mount()
{
if ($this->getTableSortColumn() === null) {
$this->sortTable('created_at', 'desc');
}
} This is a filament table component in a Livewire app, not an admin page. What this does is check whether the user has arrived on the page with a default sorting column. If they have not, then set the column and order to sort by. This has the advantage that it orders the table, shows the ordering on the column being sorted (so it's not hidden from the user), and sets the URI query string, assuming you have set them up. I'm not sure how it interacts with serssion-based saving of table filters - it probably overrides those. Doing the following instead has the same effect except the sorting is not shown to the user in the table UI; it's just "magically" sorted, plus it probably interacts with session-saved table filters differently: protected function getDefaultTableSortColumn(): ?string
{
return 'created_at';
}
protected function getDefaultTableSortDirection(): ?string
{
return 'desc';
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Say I have a blog post resource with
created_at
.How do I make sure that in the table view for the resource (
/admin/resources/posts
) I sort by default withcreated_at > desc
?sortable()
does not solve it as you have to manually click on it.Edit: Just a related PR to this: #231
Beta Was this translation helpful? Give feedback.
All reactions