2013-08-21 119 views
3

當在Silverstripe 3中管理Dataobject s和ModelAdmin時,我想知道如何將自定義操作的按鈕添加到列表視圖以及編輯頁面。將自定義操作的按鈕添加到ModelAdmin

我已經成功地添加一個按鈕的動作到編輯頁面用下面的代碼在ModelAdmin小號GridField類設置ItemRequest

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

} 

所以問題是:

我可以有一個按鈕爲同樣動作ModelAdminGridField Listview? 因此,它旁邊會出現編輯和刪除按鈕:

ModelAdmin GridField actions

回答

5

您可以通過創建自己的GridFieldComponent實施GridField_ColumnProviderGridField_ActionProvider做到這一點。

在你的情況下,你幾乎可以複製GridFieldDeleteAction類和編輯getColumnContent()與你自己的GridField_FormAction和編輯handleAction()做你的魔法。

+0

這樣做 - 謝謝:)我是一種像getCMSActions()我希望有一種神奇的方法,我只需要重寫,以改變這兩個領域... –

相關問題