Permalink
Browse files

Merge pull request #154 from silverstripe-scienceninjas/pull/gridfiel…

…d-bugfixes

BUGFIX GridField_Actions did not work in more complex Forms with tabsets...
  • Loading branch information...
2 parents 93eb2dd + 3937249 commit ce795b6d8a34f66f7998fecf74b742d4424e6784 @sminnee sminnee committed Jan 9, 2012
Showing with 52 additions and 28 deletions.
  1. +20 −6 forms/Form.php
  2. +21 −17 forms/gridfield/GridField.php
  3. +1 −1 forms/gridfield/GridFieldFilter.php
  4. +3 −0 forms/gridfield/GridState.php
  5. +7 −4 javascript/GridField.js
View
@@ -361,18 +361,32 @@ function httpSubmission($request) {
// Otherwise, try a handler method on the form object.
} elseif($this->hasMethod($funcName)) {
return $this->$funcName($vars, $this, $request);
- } else {
- // Finally try to find a field that could handle that action, ie GridField
- foreach ($this->Fields() as $field){
- if (!$field->hasMethod($funcName)) continue;
- return $field->$funcName($vars, $this, $request);
- }
+ } elseif($field = $this->checkFieldsForAction($this->Fields(), $funcName)) {
+ return $field->$funcName($vars, $this, $request);
}
return $this->httpError(404);
}
/**
+ * Fields can have action to, let's check if anyone of the responds to $funcname them
+ *
+ * @return FormField
+ */
+ protected function checkFieldsForAction($fields, $funcName) {
+ foreach($fields as $field){
+ if(method_exists($field, 'FieldList')) {
+ if($field = $this->checkFieldsForAction($field->FieldList(), $funcName)) {
+ return $field;
+ }
+ } elseif (!$field->hasMethod($funcName)) {
+ continue;
+ }
+ return $field;
+ }
+ }
+
+ /**
* Handle a field request.
* Uses {@link Form->dataFieldByName()} to find a matching field,
* and falls back to {@link FieldList->fieldByName()} to look
@@ -337,9 +337,11 @@ public function FieldHolder() {
'class' => "field CompositeField {$this->extraClass()}"
);
return
- implode("\n", $content['before']) .
- $this->createTag('table', $attrs, $head."\n".$foot."\n".$body) .
- implode("\n", $content['after']);
+ $this->createTag('fieldset', array('id'=>$this->ID(),'class'=>'ss-gridfield'),
+ implode("\n", $content['before']) .
+ $this->createTag('table', $attrs, $head."\n".$foot."\n".$body) .
+ implode("\n", $content['after'])
+ );
}
function getColumns() {
@@ -420,30 +422,32 @@ protected function buildColumnDispatch() {
* @param array $data
* @return string
*/
- public function gridFieldAlterAction($data, $form, $request) {
+ public function gridFieldAlterAction($data, $form, SS_HTTPRequest $request) {
$id = $data['StateID'];
$stateChange = Session::get($id);
+ $gridName = $stateChange['grid'];
+ $grid = $form->Fields()->dataFieldByName($gridName);
- $state = $this->getState(false);
+ $state = $grid->getState(false);
$state->setValue($data['GridState']);
- $gridName = $stateChange['grid'];
- $grid = $form->Fields()->fieldByName($gridName);
$actionName = $stateChange['actionName'];
$args = $stateChange['args'];
$grid->handleAction($actionName, $args, $data);
- // Make the form re-load it's values from the Session after redirect
- // so the changes we just made above survive the page reload
- // @todo Form really needs refactoring so we dont have to do this
- if (Director::is_ajax()) {
- return $form->forTemplate();
- } else {
- $data = $form->getData();
- Session::set("FormInfo.{$form->FormName()}.errors", array());
- Session::set("FormInfo.{$form->FormName()}.data", $data);
- Controller::curr()->redirectBack();
+ switch($request->getHeader('X-Get-Fragment')) {
+ case 'CurrentField':
+ return $grid->FieldHolder();
+ break;
+
+ case 'CurrentForm':
+ return $form->forTemplate();
+ break;
+
+ default:
+ return $form->forTemplate();
+ break;
}
}
@@ -79,7 +79,7 @@ public function getHTMLFragments($gridField) {
if(isset($filterArguments[$columnField])) {
$value = $filterArguments[$columnField];
}
- $field = new TextField('filter['.$columnField.']', 'filter['.$columnField.']', $value);
+ $field = new TextField('filter['.$columnField.']', '', $value);
$field->addExtraClass('ss-gridfield-sort');
} else {
$field = new LiteralField('', '');
@@ -68,6 +68,9 @@ public function getList() {
/** @return string */
public function Value() {
+ if(!$this->gridStateData) {
+ return json_encode(array());
+ }
return json_encode($this->gridStateData->toArray());
}
View
@@ -1,18 +1,21 @@
jQuery(function($){
- $('.ss-gridfield .action').entwine({
+ $('fieldset.ss-gridfield .action').entwine({
onclick: function(e){
- button = this;
+ var button = this;
e.preventDefault();
var form = $(this).closest("form");
+ var field = $(this).closest("fieldset.ss-gridfield");
form.addClass('loading');
$.ajax({
+ headers: {"X-Get-Fragment" : 'CurrentField'},
type: "POST",
url: form.attr('action'),
data: form.serialize()+'&'+escape(button.attr('name'))+'='+escape(button.val()),
dataType: 'html',
success: function(data) {
- form.replaceWith(data);
+ // Replace the grid field with response, not the form.
+ field.replaceWith(data);
form.removeClass('loading');
},
error: function(e) {
@@ -35,7 +38,7 @@ jQuery(function($){
* ToDo ensure filter-button state is maintained after filtering (see resetState param)
* ToDo get working in IE 6-7
*/
- $('.ss-gridfield input.ss-gridfield-sort').entwine({
+ $('fieldset.ss-gridfield input.ss-gridfield-sort').entwine({
onfocusin: function(e) {
// Dodgy results in IE <=7
if($.browser.msie && $.browser.version <= 7) {

0 comments on commit ce795b6

Please sign in to comment.