Modeladmin: Sorting gridfield automatically
Sometimes you want to sort your gridfield in ModelAdmin by default when you have a sorting column in your DataObject. In my case the column is called SortOrder, so overwriting getEditForm() (which generates the grid) with this snippet in the ModelAdmin subclass does the trick:
public function getEditForm($id = null, $fields = null) { $form=parent::getEditForm($id, $fields); $model = singleton($this->modelClass); /** add sorting if we have a field for... */ if (class_exists('GridFieldSortableRows') && $model->hasField('SortOrder') && $gridField=$form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) { if($gridField instanceof GridField) { $gridField->getConfig()->addComponent(new GridFieldSortableRows('SortOrder')); } } return $form; }
The GridFieldSortableRows class is part of the SortableGridField module.
Post your comment
Comments
No one has commented on this page yet.
RSS feed for comments on this page | RSS feed for all comments