Silverstripe DataObject - drag and drop ordering

…衆ロ難τιáo~ 提交于 2019-12-06 03:27:43

In SilverStripe 3.1 there are a few excellent modules that give you this sort of functionality. Two of these modules are SortableGridField and GridFieldExtensions.

To get this working you need to install one of these modules, add a sort field to your custom DataObject class and add the module sort object component to your GridFieldConfig.

SortableGridField

The SortableGridField module is specifically to allow sorting functionality for objects on a GridField.

To get this working you need to add a sort field to your custom DataObject class and add GridFieldSortableRows() as a component to your GridField.

For the following examples I will use HomePage as the page with a has_many relationship to a Slide DataObject.

Slide

class Slide extends DataObject
{
    private static $db = array (
        'Title' => 'HTMLText',
        'SortOrder' => 'Int'
    );

    private static $has_one = array (
        'HomePage' => 'HomePage'
    );

    private static $summary_fields = array( 
        'Title' => 'Title'
    );

    private static $default_sort = 'SortOrder ASC';
    private static $singular_name = 'Slide';
    private static $plural_name = 'Slides';

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->removeByName('SortOrder');

        return $fields;
    }

}

HomePage

class HomePage extends Page {

    private static $has_many = array (
        'Slides' => 'Slide'
    );

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $slidesFieldConfig = GridFieldConfig_RecordEditor::create();
        $slidesFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));

        $slidesField = GridField::create(
            'Slides',
            'Slide',
            $this->Slides(),
            $slidesFieldConfig
        ); 
        $fields->addFieldToTab('Root.Slides', $slidesField);

        return $fields;
    }

}

GridFieldExtensions

The GridFieldExtensions module contains GridFieldOrderableRows to control the sort order on a GridField, just like the SortableGridField module. It also has other useful GridField tools.

To get this working you need to add a sort field to your custom DataObject class and add GridFieldOrderableRows() as a component to your GridField.

Your code would be just like the above example except the component you add to your GridFieldConfig is GridFieldOrderableRows:

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $slidesFieldConfig = GridFieldConfig_RecordEditor::create();
        $slidesFieldConfig->addComponent(new GridFieldOrderableRows('SortOrder'));

        ...
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!