在GridField上使用setQueriedColumns

时间:2018-05-17 00:00:04

标签: php silverstripe

我有一个数据对象,我想在页面上的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;
}
...
}

1 个答案:

答案 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);`