-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default ordering resource #156
Comments
I’m on mobile now but can you try overriding indexQuery method on the resource and put it there? |
@crnkovic is correct @franzdumfart This can be accomplished by overloading the Unfortunately the solution is slightly more fugly than probably desired, because the order by id beats the request to the query. But this works fine.
I should note, that the UI state and url state does not know sorting has been changed. So if you are sorting by a column that is visible by default, the UI will not reflect that. See suggestion from @jasonlav, this is a bit more desirable. |
Thanks so much, this helps! Will extend all my Nova resources from a Base class and add a helper method there, so I can use it on every Resource with just setting one property. Hopefully this will be implemented someday in Nova itself. Need this very often. |
Slightly, updated code to make it one line per Resource. In
In any resource where you want to change the sort:
|
Thanks @loren138, works perfect! We also should add another static variable like |
I added I hate that PHP doesn't have tuples, god damn. Nice solution, though. :) |
@franzdumfart Probably shouldn't close it until it is a feature request or a pull request. |
I've ended with this code in
Thanks for all the suggestions! 🙌 |
How 'bout sorting on a relation field...? For example, activities belong to an event and an event has a date. If you view activities, you may want to sort on their event dates. |
This repository is for tracking bugs. Feature requests may be emailed to Nova customer support. |
@taylorotwell Is the customer support email listed somewhere on the Nova website? The only email I've been able to find so far is your email from the |
@taylorotwell Is it possible to open a nova-feature-requests repository, so we can all submit to that? |
The |
It's seems that this approach does not works on relationship models. So I put a public function comments()
{
return $this->belongsToMany('App\Posts')
->withPivot('order')
->orderBy('order');
} |
I'm trying to resolve this myself at the moment, could you provide a more detailed example please @jasonlav? Attempting to set the default sort order for a HasMany-related resource. e.g. Town resource has many Roads, which appear as a relationship when viewing a Town. Attempting to simply sort the Roads panel by name ascending, without success. Perhaps my brain is just broken at the moment :) |
The
|
This code is working. But i have several questions.
This is showing users own data. Other users informations are not displaying. I made this. But when sort some column. All datas showing up again...
How can i solve this. How can i use order and display limit (each user) together? |
Using this approach I ended up with a simple overwrite in my base protected static function applyOrderings($query, array $orderings)
{
if (empty($orderings) && property_exists(static::class, 'orderBy')) {
$orderings = static::$orderBy;
}
return parent::applyOrderings($query, $orderings);
} Now in any Resource I can simple add public static $orderBy = ['name' => 'asc']; Works like a charm |
I used sorting by overriding the indexQuery method. But when I try to apply filters they do not work. What could be the reason? |
I have my resource like :
The sorting doesn't work on the seller and buyer. What is the way to implement it ? |
For anyone else still coming back to this issue, Nova v3.19.1 introduced changes that (at least for me) broke @rasmuscnielsen's awesome workaround. Nova now explicitly orders all index queries by a resource model's primary key by default, so to get the snippet above working again, I had to remove the protected static function applyOrderings($query, array $orderings)
{
if (empty($orderings) && property_exists(static::class, 'orderBy')) {
+ $query->reorder();
$orderings = static::$orderBy;
}
return parent::applyOrderings($query, $orderings);
} |
If you sort a field and cycle through until you're not sorting any more (click the field name three times), you can end up with a To make sure you're still applying your custom default sort, filter the orderings array:
|
@rdarcy1 That's fixed by now (3.29 anyway). It now looks like this (I have changed protected static function applyOrderings($query, array $orderings)
{
$orderings = array_filter($orderings);
if (empty($orderings)) {
return empty($query->getQuery()->orders) && ! static::usesScout()
? $query->oldest($query->getModel()->getQualifiedKeyName())
: $query;
}
foreach ($orderings as $column => $direction) {
$query->orderBy($column, $direction);
}
return $query;
} |
Is this still the best way with Nova 4? |
^ +1, Nova 4 still the best way? Seems strange it wasn't implemented if so |
Still the same solution, nothing implemented in Nova 4. |
Found nothing in the docs, so here is my question:
How can I set the default ordering of a Resources items on list view? At the moment it looks like it is
orderBy('id', 'desc')
. Setting the first visible and sortable field would be a great indicator for the planned behavior. Or is possible the set it via a property or method?The text was updated successfully, but these errors were encountered: