How to sort DataObject by title in a dropdown on single page

Silverstripe Version:
4.2

Question:

Hi all, I’m super new to SilverStripe since 2 weeks back and trying to figure out the sorting functionality. I’ve been following all the Lessons but tweaked it a bit to a single page with multiple DataObjects, in this case 50 types of different cars, all results showing with ajax pagination.

I would like to sort all cars by title in a dropdown but can’t figure out how to implement the sorting functionality to it without using a submit form. I’ve searched everywhere but can’t find a solution, I’ve probably missed something on the way.

What is the best approach for this? Any help is appreciated.

A couple of possibilities:

Add the following to your data object:

private static $default_sort = 'Title';

And run a dev/build. That will ensure that any time the cars are retrieved, they’ll be sorted by the title unless otherwise specified.

Alternatively, you can use the sort method of the ORM, eg.: $cars = Cars::get()->sort('Title');

If they’re not what you’re after, it’s probably worth posting some of your code so we can get a better idea of what you need.

1 Like