CRUD
CRUD copied to clipboard
Provide a way to define a default ordering inside ListOperation
Feature Request
What's the feature you think Backpack should have?
A way to specify a default sort column for a list operation, eg.

Have you already implemented a prototype solution, for your own project?
I've tried a couple methods but they don't work, eg.
- https://stackoverflow.com/a/63831852
- https://stackoverflow.com/a/57293048
- Even various combinations of both.
These don't work at all for me, possibly because tables are too "sticky". Even when I reset filters and manually set persistent-table=false
in the URL, the last sorted column is always what's sorted by. AJAX requests seem to always have order[0][column]: 1
and order[0][dir]: desc
set.
I've even tried deleting my domain cookies and logging back in again to a "fresh" table, and it's still sticky.
Even if these methods did work, they feel very hacky. For the first example especially, since we have to find the index of the column we want to sort by (we can't reference it by name).
Do you see this as a core feature or an add-on?
Core.
Thanks. 🙏
Hello there! Thanks for opening your first issue on this repo!
Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.
Backpack communication channels:
- Bug Reports, Feature Requests - Github Issues (here);
- Quick help (How do I do X) - Gitter Chatroom;
- Long questions (I have done X and Y and it won't do Z wtf) - Stackoverflow, using the
backpack-for-laravel
tag; - Showing off something you've made, asking for opinion on Backpack/Laravel matters - Reddit;
Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.
Thank you!
-- Justin Case The Backpack Robot
Hmm... I had no idea this is so difficult... thanks @joelmellon - I agree, we should make this a lot easier. I've tagged this appropriately so we do it.
In the meantime, I see in ListOperation
we are applying the DataTable ordering if (request()->input('order'))
, so how about doing the opposite here? You should be able to do something like this, right?
// set a default orderBy clause (will be overridden by DataTable ordering)
if (!request()->has('order')) {
$this->crud->orderBy('updated_at', 'DESC');
}
Let me know if that works for you. Cheers!
I see in
ListOperation
we are applying the DataTable orderingif (request()->input('order'))
, so how about doing the opposite here?// set a default orderBy clause (will be overridden by DataTable ordering) if (!request()->has('order')) { $this->crud->orderBy('updated_at', 'DESC'); }
Yeah, that's sort of the main technique used by developers who try this. It doesn't really work for me, or maybe it's just really hard to test since tables are so sticky.
Turning off table persistence in the config seems to help see what's going on a little better, but it would be really helpful if the Reset
button reset the sort too.
it would be really helpful if the Reset button reset the sort too.
Hmm... I agree but... in my experience the reset button does reset the sorting:
https://user-images.githubusercontent.com/1032474/153448549-83280487-6341-47fd-8ac4-a988cf12f51d.mp4
I get the same behavior when I have persistentTables=false
in my config, but when it's set to true, the last sort column stays. I'd expect Reset
to clear the sort, regardless of that setting. Maybe it's not the case, but to me, it looks like you have table persistence off.
I'm running 4.1 still. I have a mess of dependencies I need to upgrade.
Anyway, as I've worked through this feature request I've started to understand the current functionality better, so I'm very grateful for that, thanks again. 🙏
Edit: Accidentally switched between my accounts here too, lol. Same one person here, sorry.
Hello @joelmellon sorry for the time it took to get back here.
Usually column 0
it's the model primary column that Backpack automatically adds when developer don't manually specify it.
By default, if you don't apply any order to the table, Backpack will apply the "default order" that is primaryKey => DESC
so that you get the last created items first.
You can change the order by applying your custom orders to any other columns, so Backpack will apply your orders first, and on top of that, the primary key order. You can also add your custom primary key orderBy, to override Backpack defaults.
The orderBy's are applied on database level so they cannot be "model attributes", but must be "table columns".
The reset button does not reset query orders
, only resets table orders/filters
, so if you don't apply any order, like I told you Backpack will apply the "orderBy primary key" in the query orders
.
If in you controller you have something like:
public function setupListOperation()
{
$this->crud->query->where('active', true)->orderBy('id', 'asc');
}
This is a query order
, the reset button will not reset this order.
I hope I made myself clear enough here, so I will be closing this, let me know if I missed something so that I can re-open if needed.
Cheers
@pxpm is this documented? if not, could the team add a 'default sort option' in the section of api doc about sorting?
I don't think it's properly documented, no. @pxpm could you please create a first draft for docs for this, and tag me to take a second look? 🙏