Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Sorting or ordering the drop down field


Go to End


3 Posts   1727 Views

Avatar
macka

Community Member, 33 Posts

6 November 2014 at 3:09pm

Hi Guys,

Is it possible to alphabetically sort the drop down list? or is it even done already?

I have ..

	if ($member = DataObject::get('Member')) {
		$member = $member->map('ID', 'getName', '(Select one)');		
		$ListOfMembers = new DropdownField('User', 'Member: ', $member);
		$ListOfMembers->setEmptyString('Select user');				
		$fields->push($ListOfMembers);
	}

I don't think the drop down list is being sorted, so can i actually put a sort in there somewhere?

Thanks
Grant

Avatar
zenmonkey

Community Member, 545 Posts

6 November 2014 at 4:52pm

You just need to aply a sort to your DataObject::get(); I also notices you're using the old get pattern

if ($member = Member::get()) {
    $ListOfMember = DropdownField::create( 'User', 'Member:', $member->sort('Surname', 'ASC')->map('ID', 'getName') )->setEmptyString('Select user');
    $fields->push($ListOfMembers);
}

Using the SS3+ patterns you can chain a bunch of functions, and in the case of the member list you're not overwriting the object with an array so you can you can reuse it.

Avatar
macka

Community Member, 33 Posts

7 November 2014 at 9:22am

Thanks Zenmonkey! works a treat. most helpful!

Grant