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 one Model only in ModelAdmin


Go to End


2 Posts   939 Views

Avatar
vwd

Community Member, 166 Posts

25 September 2013 at 1:47pm

Edited: 25/09/2013 3:38pm

Hi,

I've set up a ModelAdmin to manage multiple models. Only one of these requires drag-and-drop sorting.

When I add add sorting capability to the ModelAdmin, it complains because the non sorted Model doesn't have a SortOrder field. How can I specify that GridFieldSortableRows is added only to the Model which is to be sorted?

My code is:

class DAdmin extends ModelAdmin {
 
	public static $managed_models = array(
		'PCategory',
		'AItem'
	);
 
	static $url_segment = 'd-admin';
	static $menu_title = 'dAdmin';
    
	public function getEditForm($id = null, $fields = null) {
		$form = parent::getEditForm($id, $fields);
		$cName = $this->sanitiseClassName($this->modelClass);
		$gridField = $form->Fields()->fieldByName($cName);
		$gridField->getConfig()->addComponent(new GridFieldSortableRows('SortOrder'));
		 return $form;
	}    
}

Thanks.
VWD.

Avatar
vwd

Community Member, 166 Posts

25 September 2013 at 3:36pm

Edited: 25/09/2013 3:39pm

It was fairly simple in the end:

class VAdmin extends ModelAdmin {
 
	public static $managed_models = array(
		'PCategory',
		'AItem'
	);
 
	static $url_segment = 'd-admin';
	static $menu_title = 'dAdmin';
    
	public function getEditForm($id = null, $fields = null) {
		$form = parent::getEditForm($id, $fields);

		if ($this->modelClass == 'AItem') {   // add GridFieldSortableRows for AItem GridField only
			$gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass));
			$gridField->getConfig()->addComponent(new GridFieldSortableRows('SortOrder'));
		}
		return $form;
	}    
}