mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Update 04_Create_a_GridField_ActionProvider.md
This commit is contained in:
parent
4268db069d
commit
9f7aad3b8f
@ -1,7 +1,9 @@
|
||||
# How to add a custom action to a GridField row
|
||||
|
||||
You can add an action to the row(s) of a [GridField](/developer_guides/forms/field_types/gridfield), such as the built in edit or delete actions.
|
||||
|
||||
In a [GridField](/developer_guides/forms/field_types/gridfield) instance each table row can have a
|
||||
number of actions located the end of the row such as edit or delete actions.
|
||||
number of actions located the end of the row.
|
||||
Each action is represented as a instance of a specific class
|
||||
(e.g [GridFieldEditButton](api:SilverStripe\Forms\GridField\GridFieldEditButton)) which has been added to the `GridFieldConfig`
|
||||
for that `GridField`
|
||||
@ -10,7 +12,14 @@ As a developer, you can create your own custom actions to be located alongside
|
||||
the built in buttons.
|
||||
|
||||
For example let's create a custom action on the GridField to allow the user to
|
||||
perform custom operations on a row.
|
||||
perform custom operations on a row:
|
||||
|
||||
1. Create a custom action
|
||||
2. [Add your custom action to the current GridFieldConfig](#add-the-gridfieldcustomaction-to-the-current-gridfieldconfig)
|
||||
|
||||
_To create a custom action follow the [Basic GridFieldCustomAction boilerplate](#basic-gridfieldcustomaction-boilerplate)
|
||||
below, and if you would like to create a custom action in the **GridField action menu** follow the
|
||||
[Basic GridFieldCustomAction boilerplate implementing GridField_ActionMenuItem](#basic-gridfieldcustomaction-boilerplate-implementing-gridfield_actionmenuitem)_
|
||||
|
||||
## Basic GridFieldCustomAction boilerplate
|
||||
|
||||
@ -167,9 +176,43 @@ use SilverStripe\Forms\GridField\GridField_ActionMenuItem;
|
||||
use SilverStripe\Forms\GridField\GridField_FormAction;
|
||||
use SilverStripe\Control\Controller;
|
||||
|
||||
class GridFieldDeleteAction implements GridField_ColumnProvider, GridField_ActionProvider, GridField_ActionMenuItem
|
||||
class GridFieldCustomAction implements GridField_ColumnProvider, GridField_ActionProvider, GridField_ActionMenuItem
|
||||
{
|
||||
|
||||
public function getTitle($gridField, $record, $columnName)
|
||||
{
|
||||
return 'Custom action';
|
||||
}
|
||||
|
||||
public function getCustomAction()
|
||||
{
|
||||
if(!$record->canEdit()) return;
|
||||
|
||||
return GridField_FormAction::create(
|
||||
$gridField,
|
||||
'CustomAction'.$record->ID,
|
||||
'Custom action',
|
||||
"docustomaction",
|
||||
['RecordID' => $record->ID]
|
||||
);
|
||||
}
|
||||
|
||||
public function getExtraData($gridField, $record, $columnName)
|
||||
{
|
||||
$field = $this->getCustomAction($gridField, $record);
|
||||
|
||||
if (!$field) {
|
||||
return;
|
||||
}
|
||||
|
||||
return $field->getAttributes();
|
||||
}
|
||||
|
||||
public function getGroup($gridField, $record, $columnName)
|
||||
{
|
||||
return GridField_ActionMenuItem::DEFAULT_GROUP;
|
||||
}
|
||||
|
||||
public function augmentColumns($gridField, &$columns)
|
||||
{
|
||||
if(!in_array('Actions', $columns)) {
|
||||
@ -177,26 +220,52 @@ class GridFieldDeleteAction implements GridField_ColumnProvider, GridField_Actio
|
||||
}
|
||||
}
|
||||
|
||||
public function getTitle($gridField, $record, $columnName)
|
||||
public function getColumnAttributes($gridField, $record, $columnName)
|
||||
{
|
||||
return _t(__CLASS__ . '.Delete', "Delete");
|
||||
return ['class' => 'grid-field__col-compact'];
|
||||
}
|
||||
|
||||
public function getGroup($gridField, $record, $columnName)
|
||||
public function getColumnMetadata($gridField, $columnName)
|
||||
{
|
||||
return GridField_ActionMenuItem::DEFAULT_GROUP;
|
||||
if($columnName == 'Actions') {
|
||||
return ['title' => ''];
|
||||
}
|
||||
}
|
||||
|
||||
public function getExtraData($gridField, $record, $columnName)
|
||||
public function getColumnsHandled($gridField)
|
||||
{
|
||||
if ($gridField) {
|
||||
return $gridField->getAttributes();
|
||||
return ['Actions'];
|
||||
}
|
||||
|
||||
public function getColumnContent($gridField, $record, $columnName)
|
||||
{
|
||||
$field = $this->getCustomAction($gridField, $record);
|
||||
|
||||
if (!$field) {
|
||||
return;
|
||||
}
|
||||
|
||||
return null;
|
||||
return $field->Field();
|
||||
}
|
||||
|
||||
// ...rest of boilerplate code
|
||||
public function getActions($gridField)
|
||||
{
|
||||
return ['docustomaction'];
|
||||
}
|
||||
|
||||
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
|
||||
{
|
||||
if($actionName == 'docustomaction') {
|
||||
// perform your action here
|
||||
|
||||
// output a success message to the user
|
||||
Controller::curr()->getResponse()->setStatusCode(
|
||||
200,
|
||||
'Do Custom Action Done.'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Related
|
||||
|
Loading…
Reference in New Issue
Block a user