在 Silverstripe 3 中管理Dataobject
s 时ModelAdmin
,我想知道如何将自定义操作按钮添加到列表视图以及编辑页面。
通过使用以下代码设置 s 类,ItemRequest
我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);
}
}
}
所以问题是:
我可以在Listview中有一个用于相同操作的按钮吗?ModelAdmin
GridField
这样它就会出现在编辑和删除按钮旁边: