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.

General Questions /

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

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

Sorting one Model only in ModelAdmin


Reply


2 Posts   256 Views

Avatar
vwd

Community Member, 159 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, 159 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;
   }
}