Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Sorting one Model only in ModelAdmin

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

Page: 1
Go to End
Author Topic: 241 Views
  • vwd
    Avatar
    Community Member
    158 Posts

    Sorting one Model only in ModelAdmin Link to this post

    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.

  • vwd
    Avatar
    Community Member
    158 Posts

    Re: Sorting one Model only in ModelAdmin Link to this post

    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;
       }
    }

    241 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.