我有一个数据对象,我想在页面上的GridField上使用,但我想限制显示的列。我使用setQueriedColumns()
列出了我想要的字段,但它仍然显示数据对象的默认$summary_fields
。
MyActivity数据对象:
class MyActivity extends DataObject{
private static $db = array(
'Title' => 'Varchar(255)',
'URLSegment' => 'Varchar(512)',
'IsPublished' => 'Boolean',
'IsPublic' => 'Boolean',
'IsBooked' => 'Boolean',
'MaxDuration' => 'Int',
'PricePoint' => 'Int',
'Summary' => 'HTMLText',
'Body' => 'HTMLText',
'Sort' => 'Int'
);
private static $has_one = array(
'FileAttachment' => 'File'
);
private static $summary_fields = array(
'Title' => 'Name',
'URLSegment' => 'URLSegment',
'IsPublished' => 'Published',
'IsBooked' => 'Booked',
'Events.Count' => 'List of Events',
'Categories.Count' => ' of Categories'
);
static $has_many = array(
'Events' => 'MyEvent'
);
static $belongs_many_many = array(
'Categories' => 'MyCategory'
);
...
}
MyActivityPage:
class MyActivityPage extends Page{
public function getCMSFields(){
$fields = parent::getCMSFields();
$GridFieldConfig = GridFieldConfig_RecordEditor::create();
$fields->addFieldToTab('Root.Courses',
GridField::create(
'FileAttachment',
'Activity List',
MyActivity::get()->filter(['IsPublished' => 1])
->setQueriedColumns([
'Title',
'URLSegment',
'IsPublished'
]),
$GridFieldConfig
)
);
return $fields;
}
...
}
答案 0 :(得分:1)
经过彻底的搜索,我得到了我正在寻找的东西。显然,我们可以使用GridFieldConfig
设置列,然后使用$summary_fields
对象中的setDisplayFields()
方法覆盖GridFieldDataColumns
来限制字段。
对于正在寻找类似解决方案的人来说,这可能很方便。
$gridField = GridField::create(
'FileAttachment',
'Activity List',
MyActivity::get()->filter(['IsPublished' => 1]),
$GridFieldConfig
)
$gridField->getConfig()
->getComponentByType('GridFieldDataColumns')
->setDisplayFields([
'Title' => 'Title',
'URLSegment' => 'URLSegment',
'IsPublished' => 'IsPublished'
]);
$fields->addFieldToTab('Root.Courses',$gridField);`