在Silverstripe 3中使用Dataobject
管理ModelAdmin
时,我想知道如何将自定义操作按钮添加到列表视图以及编辑页面。
我已设法通过使用以下代码设置ItemRequest
s ModelAdmin
类的GridField
,为编辑页面添加操作按钮:
class MyModelAdmin extends ModelAdmin
{
//...
public function getEditForm($id = null, $fields = null)
{
$form = parent::getEditForm($id, $fields);
$form
->Fields()
->fieldByName($this->sanitiseClassName($this->modelClass))
->getConfig()
->getComponentByType('GridFieldDetailForm')
->setItemRequestClass('MyModelGridFieldDetailForm_ItemRequest');
return $form;
}
}
MyModelGridFieldDetailForm_ItemRequest.php
class MyModelGridFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest
{
function ItemEditForm()
{
$form = parent::ItemEditForm();
$formActions = $form->Actions();
$button = FormAction::create('myAction');
$button->setTitle('My Custom Action');
$button->addExtraClass('ss-ui-action-constructive');
$formActions->push($button);
$form->setActions($formActions);
return $form;
}
function myAction($data, $form)
{
//do things
$form->sessionMessage('My Action has been successful', 'good');
if ($this->gridField->getList()->byId($this->record->ID)) {
return $this->edit(Controller::curr()->getRequest());
} else {
$noActionURL = Controller::curr()->removeAction($data['url']);
Controller::curr()->getRequest()->addHeader('X-Pjax', 'Content');
return Controller::curr()->redirect($noActionURL, 302);
}
}
}
所以问题是:
我可以在ModelAdmin
GridField
列表视图中找到相同操作的按钮吗?
因此它出现在编辑和删除按钮旁边:
答案 0 :(得分:6)
您可以通过创建自己的GridFieldComponent
实施GridField_ColumnProvider
和GridField_ActionProvider
来完成此操作。
在您的情况下,您几乎可以复制GridFieldDeleteAction
课程并使用自己的getColumnContent()
修改GridField_FormAction
并修改handleAction()
以实现您的魔力。