Silverstripe 3 - 多页网格域在一页上

时间:2013-09-18 08:27:40

标签: silverstripe data-objects

我想将多个网格字段添加到一个页面类型。 目前我正在这样做

        $gridFieldConfig = GridFieldConfig::create()->addComponents(
        new GridFieldToolbarHeader(),
        new GridFieldAddNewButton('toolbar-header-right'),
        new GridFieldSortableHeader(),
        new GridFieldDataColumns(),
        new GridFieldPaginator(10),
        new GridFieldEditButton(),
        new GridFieldDeleteAction(),
        new GridFieldDetailForm()
    );

    $sliderField = new GridField('Slides', 'Slider', $this->Slides(), $gridFieldConfig);
    $fields->addFieldToTab('Root.Slider', $sliderField);

    $categoryField = new GridField('ShopCategories', 'Kategorien', $this->ShopCategories(), $gridFieldConfig);
    $fields->addFieldToTab('Root.Shop Kategorien', $categoryField);

它有效,但问题是我为两者都获得了相同的“添加blablabla对象”标题。

如何在不使用多个gridFieldConfigs的情况下解决此问题?

提前进行

2 个答案:

答案 0 :(得分:3)

问题是,只要添加配置对象,就会将其绑定到网格域。 这意味着你需要2个配置对象,目前只有一个。 您可以创建第二个,也可以克隆第一个:

$gridFieldConfig = GridFieldConfig::create()->addComponents(
    new GridFieldToolbarHeader(),
    new GridFieldAddNewButton('toolbar-header-right'),
    new GridFieldSortableHeader(),
    new GridFieldDataColumns(),
    new GridFieldPaginator(10),
    new GridFieldEditButton(),
    new GridFieldDeleteAction(),
    new GridFieldDetailForm()
);
$gridFieldConfig2 = clone $gridFieldConfig;

$sliderField = new GridField('Slides', 'Slider', $this->Slides(), $gridFieldConfig);
$fields->addFieldToTab('Root.Slider', $sliderField);

$categoryField = new GridField('ShopCategories', 'Kategorien', $this->ShopCategories(), $gridFieldConfig2);
$fields->addFieldToTab('Root.Shop Kategorien', $categoryField);

答案 1 :(得分:3)

似乎如果使用相同的配置,则两者共享详细信息。但是,您可以使用一些默认配置设置。

  • GridFieldConfig_RelationEditor
  • GridFieldConfig_RecordEditor
  • GridFieldConfig_RecordViewer
  • GridFieldConfig_Base

所有这些都扩展了gridfield配置。

所以你可以这样做,例如:

$sliderField = new GridField(
            'Slides', 
            'Slider', 
            $this->Slides(), 
            GridFieldConfig_RelationEditor::create()
);

如果您希望创建自己的自定义配置,可以编写一个以相同方式扩展GridFieldConfig的类:

class GridFieldConfig_Custom extends GridFieldConfig {
/**
 *
 * @param int $itemsPerPage - How many items per page should show up
 */
public function __construct($itemsPerPage=null) {

    $this->addComponent(new GridFieldToolbarHeader());
    $this->addComponent(new GridFieldAddNewButton('toolbar-header-right'));
    $this->addComponent(new GridFieldSortableHeader());
    $this->addComponent(new GridFieldDataColumns());
    $this->addComponent(new GridFieldPaginator(10));
    $this->addComponent(new GridFieldEditButton());
    $this->addComponent(new GridFieldDeleteAction());
    $this->addComponent(new GridFieldDetailForm());
}
}

然后:

$sliderField = new GridField(
            'Slides', 
            'Slider', 
            $this->Slides(), 
            GridFieldConfig_Custom::create());

$categoryField = new GridField(
           'ShopCategories', 
           'Kategorien', 
           $this->ShopCategories(), 
           GridFieldConfig_Custom::create());