mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
MINOR Setting existing GridState from client during request handling to allow altering it there
MINOR Parameter namespacing for GridState to avoid clashes with multiple GridField instances in same form MINOR GridField->index() to render the field, unify with gridFieldAlterAction() to support state changes
This commit is contained in:
parent
4437f9db4f
commit
47ac047454
@ -25,6 +25,7 @@ class GridField extends FormField {
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public static $allowed_actions = array(
|
public static $allowed_actions = array(
|
||||||
|
'index',
|
||||||
'gridFieldAlterAction'
|
'gridFieldAlterAction'
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -98,8 +99,10 @@ class GridField extends FormField {
|
|||||||
|
|
||||||
|
|
||||||
$this->addExtraClass('ss-gridfield');
|
$this->addExtraClass('ss-gridfield');
|
||||||
$this->requireDefaultCSS();
|
}
|
||||||
|
|
||||||
|
function index($request) {
|
||||||
|
return $this->gridFieldAlterAction(array(), $this->getForm(), $request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -356,6 +359,7 @@ class GridField extends FormField {
|
|||||||
$this->getAttributes(),
|
$this->getAttributes(),
|
||||||
array('value' => false, 'type' => false, 'name' => false)
|
array('value' => false, 'type' => false, 'name' => false)
|
||||||
);
|
);
|
||||||
|
$attrs['data-name'] = $this->Name();
|
||||||
$tableAttrs = array(
|
$tableAttrs = array(
|
||||||
'id' => isset($this->id) ? $this->id : null,
|
'id' => isset($this->id) ? $this->id : null,
|
||||||
'class' => "field CompositeField {$this->extraClass()}",
|
'class' => "field CompositeField {$this->extraClass()}",
|
||||||
@ -370,6 +374,10 @@ class GridField extends FormField {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAttributes() {
|
||||||
|
return array_merge(parent::getAttributes(), array('data-url' => $this->Link()));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the columns of this GridField, they are provided by attached GridField_ColumnProvider
|
* Get the columns of this GridField, they are provided by attached GridField_ColumnProvider
|
||||||
*
|
*
|
||||||
@ -499,25 +507,30 @@ class GridField extends FormField {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function gridFieldAlterAction($data, $form, SS_HTTPRequest $request) {
|
public function gridFieldAlterAction($data, $form, SS_HTTPRequest $request) {
|
||||||
$id = $data['StateID'];
|
$html = '';
|
||||||
|
$data = $request->requestVars();
|
||||||
|
$fieldData = @$data[$this->Name()];
|
||||||
|
|
||||||
|
// Update state from client
|
||||||
|
$state = $this->getState(false);
|
||||||
|
if(isset($fieldData['GridState'])) $state->setValue($fieldData['GridState']);
|
||||||
|
|
||||||
|
// Try to execute alter action
|
||||||
|
foreach($data as $k => $v) {
|
||||||
|
if(preg_match('/^action_gridFieldAlterAction\?StateID=(.*)/', $k, $matches)) {
|
||||||
|
$id = $matches[1];
|
||||||
$stateChange = Session::get($id);
|
$stateChange = Session::get($id);
|
||||||
$gridName = $stateChange['grid'];
|
|
||||||
$grid = $form->Fields()->dataFieldByName($gridName);
|
|
||||||
|
|
||||||
$state = $grid->getState(false);
|
|
||||||
if(isset($data['GridState'])) $state->setValue($data['GridState']);
|
|
||||||
|
|
||||||
$actionName = $stateChange['actionName'];
|
$actionName = $stateChange['actionName'];
|
||||||
$args = isset($stateChange['args']) ? $stateChange['args'] : array();
|
$args = isset($stateChange['args']) ? $stateChange['args'] : array();
|
||||||
$html = $grid->handleAction($actionName, $args, $data);
|
$html = $this->handleAction($actionName, $args, $data);
|
||||||
|
// A field can optionally return its own HTML
|
||||||
if($html) {
|
if($html) return $html;
|
||||||
return $html;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch($request->getHeader('X-Get-Fragment')) {
|
switch($request->getHeader('X-Get-Fragment')) {
|
||||||
case 'CurrentField':
|
case 'CurrentField':
|
||||||
return $grid->FieldHolder();
|
return $this->FieldHolder();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'CurrentForm':
|
case 'CurrentForm':
|
||||||
@ -566,6 +579,9 @@ class GridField extends FormField {
|
|||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
$this->setModel($model);
|
$this->setModel($model);
|
||||||
|
|
||||||
|
$fieldData = $this->request->requestVar($this->Name());
|
||||||
|
if($fieldData && $fieldData['GridState']) $this->getState(false)->setValue($fieldData['GridState']);
|
||||||
|
|
||||||
foreach($this->components as $component) {
|
foreach($this->components as $component) {
|
||||||
if(!($component instanceof GridField_URLHandler)) {
|
if(!($component instanceof GridField_URLHandler)) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -39,7 +39,7 @@ class GridState extends HiddenField {
|
|||||||
|
|
||||||
if ($value) $this->setValue($value);
|
if ($value) $this->setValue($value);
|
||||||
|
|
||||||
parent::__construct('GridState');
|
parent::__construct($grid->Name() . '[GridState]');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user