6

在 Silverstripe 3 中管理Dataobjects 时ModelAdmin,我想知道如何将自定义操作按钮添加到列表视图以及编辑页面。

通过使用以下代码设置 s 类,ItemRequestModelAdmin设法向编辑页面添加了一个用于操作的按钮: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这样它就会出现在编辑和删除按钮旁边:

ModelAdmin GridField 操作

4

1 回答 1

6

您可以通过创建自己的GridFieldComponent实现GridField_ColumnProviderGridField_ActionProvider.

在您的情况下,您几乎可以复制GridFieldDeleteAction课程并getColumnContent()使用您自己的GridField_FormAction编辑和编辑handleAction()来发挥您的魔力。

于 2013-08-21T11:50:10.457 回答