1

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

        $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 的情况下解决此问题?

提前谢谢

4

2 回答 2

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);
于 2013-09-18T09:18:23.737 回答
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());
于 2013-09-18T09:20:23.807 回答