我想将多个网格字段添加到一个页面类型。目前我正在这样做
$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对象"标题。
如何在不使用多个网格字段配置的情况下解决此问题?
提前致谢
似乎如果您使用相同的配置,则详细信息由两者共享。但是,您可以使用一些默认配置设置来代替。
- GridFieldConfig_RelationEditor
- GridFieldConfig_RecordEditor
- GridFieldConfig_RecordViewer
- GridFieldConfig_Base
所有这些都扩展了网格场配置。
因此,您可以这样做,例如:
$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());
问题是配置对象在您添加后立即绑定到网格字段。这意味着您需要 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);