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.

Customising the CMS /

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

Sortable Table in ModelAdmin


Go to End


2 Posts   1604 Views

Avatar
Dig

Community Member, 33 Posts

20 April 2016 at 12:25pm

I have a model that has a boolean 'Approved'. If I have that as part of the summary field then its sortable by clicking on the header, but it shows 1 or 0 in the column. If I use Approved.Nice in the summary fields then obviously it calls a method and as a result it isn't searchable but it looks better saying 'Yes' or 'No'.

Seems like other people might've had other similar cases where a method is needed to display things nicely (date, names etc.,) but still sorting is important. Is there a solution or an add-on that can help with this?

Cheers,
Nick

Avatar
mspacemedia

Community Member, 12 Posts

29 September 2016 at 7:22am

Hi Dig,

Resurrecting your post and providing an answer courtesy of iRC:

class SomeModelAdmin extends ModelAdmin {
	private static $managed_models = array(
		"MyDataObject"
	);
	
	public function getEditForm($id = null, $fields = null) {
		$form = parent::getEditForm($id, $fields);
		
		$gridField = $form->Fields()->dataFieldByName('MyDataObject');
		$dataColumns = $gridField->getConfig()->getComponentByType('GridFieldDataColumns');
		$dataColumns->setDisplayFields(array(
			'Field' => array(
				'title' => 'Field name',
				'callback' => function($record) {
					return $record->dbObject('Field')->Nice();
				}
			)
		));
		
		return $form;
	}