diff --git a/forms/gridfield/GridField.php b/forms/gridfield/GridField.php index d5bcaf96d23..bf13cc30f36 100755 --- a/forms/gridfield/GridField.php +++ b/forms/gridfield/GridField.php @@ -25,6 +25,7 @@ class GridField extends FormField { * @var array */ public static $allowed_actions = array( + 'index', 'gridFieldAlterAction' ); @@ -98,8 +99,10 @@ public function __construct($name, $title = null, SS_List $dataList = null, Grid $this->addExtraClass('ss-gridfield'); - $this->requireDefaultCSS(); - + } + + function index($request) { + return $this->gridFieldAlterAction(array(), $this->getForm(), $request); } /** @@ -356,6 +359,7 @@ public function FieldHolder() { $this->getAttributes(), array('value' => false, 'type' => false, 'name' => false) ); + $attrs['data-name'] = $this->Name(); $tableAttrs = array( 'id' => isset($this->id) ? $this->id : null, 'class' => "field CompositeField {$this->extraClass()}", @@ -370,6 +374,10 @@ public function FieldHolder() { ); } + 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 * @@ -491,7 +499,7 @@ protected function buildColumnDispatch() { } } } - + /** * This is the action that gets executed when a GridField_AlterAction gets clicked. * @@ -499,25 +507,30 @@ protected function buildColumnDispatch() { * @return string */ public function gridFieldAlterAction($data, $form, SS_HTTPRequest $request) { - $id = $data['StateID']; - $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']; - $args = isset($stateChange['args']) ? $stateChange['args'] : array(); - $html = $grid->handleAction($actionName, $args, $data); - - if($html) { - return $html; + $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); + $actionName = $stateChange['actionName']; + $args = isset($stateChange['args']) ? $stateChange['args'] : array(); + $html = $this->handleAction($actionName, $args, $data); + // A field can optionally return its own HTML + if($html) return $html; + } } switch($request->getHeader('X-Get-Fragment')) { case 'CurrentField': - return $grid->FieldHolder(); + return $this->FieldHolder(); break; case 'CurrentForm': @@ -565,6 +578,9 @@ function handleRequest(SS_HTTPRequest $request, DataModel $model) { $this->request = $request; $this->setModel($model); + + $fieldData = $this->request->requestVar($this->Name()); + if($fieldData && $fieldData['GridState']) $this->getState(false)->setValue($fieldData['GridState']); foreach($this->components as $component) { if(!($component instanceof GridField_URLHandler)) { diff --git a/forms/gridfield/GridState.php b/forms/gridfield/GridState.php index f71fd689907..173d01c523f 100644 --- a/forms/gridfield/GridState.php +++ b/forms/gridfield/GridState.php @@ -39,7 +39,7 @@ public function __construct($grid, $value = null) { if ($value) $this->setValue($value); - parent::__construct('GridState'); + parent::__construct($grid->Name() . '[GridState]'); } /**