Silverstripe DataObject - 拖放排序

时间:2015-03-04 00:58:20

标签: drag-and-drop silverstripe

Silverstripe DataObject - 我想将拖放排序添加到当前类,该类扩展了像页面一样的数据对象。因此,当它被删除时,它会更新该视图中所有数据对象的OrderBy字段。我创建了这个类,并且可以一次自由编辑一个项目,但是简单的拖放排序会使它变得如此简单,但我目前只能在Pages上看到Dataobjects上的任何此类扩展。

1 个答案:

答案 0 :(得分:6)

在SilverStripe 3.1中,有一些优秀的模块可以为您提供这种功能。其中两个模块是SortableGridFieldGridFieldExtensions

要实现此功能,您需要安装其中一个模块,向自定义DataObject类添加排序字段,并将模块排序对象组件添加到GridFieldConfig

SortableGridField

SortableGridField模块专门用于允许GridField上对象的排序功能。

要实现此功能,您需要在自定义DataObject课程中添加排序字段,并将GridFieldSortableRows()作为组件添加到GridField

对于以下示例,我将使用HomePage作为与Slide DataObject具有has_many关系的页面。

幻灯片

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

}

主页

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

GridFieldExtensions模块包含GridFieldOrderableRows来控制GridField上的排序顺序,就像SortableGridField模块一样。它还有其他有用的GridField工具。

要实现此功能,您需要在自定义DataObject课程中添加排序字段,并将GridFieldOrderableRows()作为组件添加到GridField

您的代码与上面的示例类似,只是您添加到GridFieldConfig的组件为GridFieldOrderableRows

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

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

        ...
    }