1
votes

I wanted to make my ModelAdmin GridField to be sortable with drag and drop. I tried adding this code:

$gridFieldName = 'Destributors';
    $gridField = $form->Fields()->fieldByName($gridFieldName);

    if ($gridField) {
        $gridField->getConfig()->addComponent(new GridFieldFilterHeader(),new GridFieldSortableRows('SortOrder'));
    }

But it seems it is not working. Any idea on how can I implement it?

Any help will greatly appreciated

1

1 Answers

3
votes

This is code that I have used on an SS3 project. You should be able to easily update it to SS4.

<?php
class MealsModelAdmin extends ModelAdmin {

    private static $managed_models = array(
        'MealAttribute',
        'MenuCategory'
    );

    private static $url_segment = 'configuration';
    private static $menu_title = 'Configuration';

    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('GridFieldOrderableRows')
            && $model->hasField('SortOrder')
            && $gridField=$form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) {
            if($gridField instanceof GridField) {
                $gridField->getConfig()->addComponent(new GridFieldOrderableRows('SortOrder'));
            }
        }

        return $form;
    }

}