drsvw88664 2013-08-21 05:21
浏览 27
已采纳

将自定义操作的按钮添加到ModelAdmin

When managing Dataobjects with ModelAdmin in Silverstripe 3, i was wondering how you can add buttons for custom action to the list view as well as to the edit-page.

I've managed to add a button for a action to the edit-page by setting the ItemRequest on the ModelAdmins GridField class with the code below:

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);
        }
    }

}

So the question is:

Can i have a button for the same action in the ModelAdmin GridField Listview? So that it appears next to the edit and delete button:

ModelAdmin GridField actions

  • 写回答

1条回答 默认 最新

  • dongzan1970 2013-08-21 11:50
    关注

    You can do this by creating your own GridFieldComponent implementing GridField_ColumnProvider and GridField_ActionProvider.

    In your case you can pretty much copy the GridFieldDeleteAction class and edit getColumnContent() with your own GridField_FormAction and edit handleAction() to do your magic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maixpy训练模型,模型训练好了以后,开发板通电会报错,不知道是什么问题
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容