Silverstripe ModelAdmin拖放Sortable

时间:2019-04-01 02:10:31

标签: php silverstripe silverstripe-4

我想让我的ModelAdmin GridField可以通过拖放进行排序。我尝试添加以下代码:

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

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

但是似乎没有用。关于如何实施它的任何想法?

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

这是我在SS3项目中使用的代码。您应该能够轻松将其更新为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;
    }

}