Merge pull request #4079 from assertchris/add-row-and-cell-generator-methods

Add row and cell generator methods
This commit is contained in:
Damian Mooyman 2015-04-20 10:01:05 +12:00
commit f76ba9afc9

View File

@ -1,4 +1,5 @@
<?php
/**
* Displays a {@link SS_List} in a grid format.
*
@ -31,18 +32,21 @@ class GridField extends FormField {
/**
* The datasource
*
* @var SS_List
*/
protected $list = null;
/**
* The classname of the DataObject that the GridField will display. Defaults to the value of $this->list->dataClass
*
* @var string
*/
protected $modelClassName = '';
/**
* the current state of the GridField
*
* @var GridState
*/
protected $state = null;
@ -114,6 +118,7 @@ class GridField extends FormField {
* this modelclass $summary_fields
*
* @param string $modelClassName
*
* @see GridFieldDataColumns::getDisplayFields()
*/
public function setModelClass($modelClassName) {
@ -149,6 +154,7 @@ class GridField extends FormField {
/**
* @param GridFieldConfig $config
*
* @return GridField
*/
public function setConfig(GridFieldConfig $config) {
@ -165,6 +171,7 @@ class GridField extends FormField {
*
* @param $value
* @param $castingDefinition
*
* @todo refactor this into GridFieldComponent
*/
public function getCastedValue($value, $castingDefinition) {
@ -345,26 +352,18 @@ class GridField extends FormField {
$rowContent = '';
foreach($this->getColumns() as $column) {
$colContent = $this->getColumnContent($record, $column);
// A return value of null means this columns should be skipped altogether.
if($colContent === null) continue;
$colAttributes = $this->getColumnAttributes($record, $column);
$rowContent .= FormField::create_tag('td', $colAttributes, $colContent);
if($colContent === null) {
continue;
}
$classes = array('ss-gridfield-item');
if ($idx == 0) $classes[] = 'first';
if ($idx == $total-1) $classes[] = 'last';
$classes[] = ($idx % 2) ? 'even' : 'odd';
$row = FormField::create_tag(
'tr',
array(
"class" => implode(' ', $classes),
'data-id' => $record->ID,
// TODO Allow per-row customization similar to GridFieldDataColumns
'data-class' => $record->ClassName,
),
$rowContent
);
$rows[] = $row;
$colAttributes = $this->getColumnAttributes($record, $column);
$rowContent .= $this->newCell($this, $total, $idx, $record, $colAttributes, $colContent);
}
$rows[] = $this->newRow($this, $total, $idx, $record, array(), $rowContent);
}
$content['body'] = implode("\n", $rows);
}
@ -422,6 +421,58 @@ class GridField extends FormField {
);
}
/**
* @param GridField $gridfield
* @param int $total
* @param int $index
* @param DataObject $record
* @param array $attributes
* @param string $content
*
* @return string
*/
protected function newCell($gridfield, $total, $index, $record, $attributes, $content) {
return FormField::create_tag(
'td',
$attributes,
$content
);
}
/**
* @param GridField $gridfield
* @param int $total
* @param int $index
* @param DataObject $record
* @param array $attributes
* @param string $content
*
* @return string
*/
protected function newRow($gridfield, $total, $index, $record, $attributes, $content) {
$classes = array('ss-gridfield-item');
if($index == 0) {
$classes[] = 'first';
}
if($index == $total - 1) {
$classes[] = 'last';
}
$classes[] = ($index % 2) ? 'even' : 'odd';
return FormField::create_tag(
'tr',
array(
'class' => implode(' ', $classes),
'data-id' => $record->ID,
'data-class' => $record->ClassName,
),
$content
);
}
public function Field($properties = array()) {
return $this->FieldHolder($properties);
}
@ -452,6 +503,7 @@ class GridField extends FormField {
*
* @param DataObject $record
* @param string $column
*
* @return string
* @throws InvalidArgumentException
*/
@ -512,6 +564,7 @@ class GridField extends FormField {
*
* @param DataObject $record
* @param string $column
*
* @return array
* @throws LogicException
* @throws InvalidArgumentException
@ -546,6 +599,7 @@ class GridField extends FormField {
* Get metadata for a column, example array('Title'=>'Email address')
*
* @param string $column
*
* @return array
* @throws LogicException
* @throws InvalidArgumentException
@ -610,6 +664,7 @@ class GridField extends FormField {
* This is the action that gets executed when a GridField_AlterAction gets clicked.
*
* @param array $data
*
* @return string
*/
public function gridFieldAlterAction($data, $form, SS_HTTPRequest $request) {
@ -661,6 +716,7 @@ class GridField extends FormField {
* @param string $actionName
* @param mixed $args
* @param arrray $data - send data from a form
*
* @return type
* @throws InvalidArgumentException
*/
@ -723,7 +779,8 @@ class GridField extends FormField {
}
if($this !== $result && !$request->isEmptyPattern($rule) && is_object($result)
&& $result instanceof RequestHandler) {
&& $result instanceof RequestHandler
) {
$returnValue = $result->handleRequest($request, $model);
@ -860,6 +917,7 @@ class GridField_FormAction extends FormAction {
* Calculate the name of the gridfield relative to the Form
*
* @param GridField $base
*
* @return string
*/
protected function getNameFromParent() {