GridFieldSortableHeader now allows composite fields to be sorted based db fields (see #7601)

This commit is contained in:
Saophalkun Ponlu 2012-08-28 21:15:46 +12:00
parent abbce151d9
commit e595b8fc75

View File

@ -13,6 +13,9 @@ class GridFieldSortableHeader implements GridField_HTMLProvider, GridField_DataM
* See {@link setThrowExceptionOnBadDataType()} * See {@link setThrowExceptionOnBadDataType()}
*/ */
protected $throwExceptionOnBadDataType = true; protected $throwExceptionOnBadDataType = true;
/** @var array */
public $fieldSorting = array();
/** /**
* Determine what happens when this component is used with a list that isn't {@link SS_Filterable}. * Determine what happens when this component is used with a list that isn't {@link SS_Filterable}.
@ -48,6 +51,24 @@ class GridFieldSortableHeader implements GridField_HTMLProvider, GridField_DataM
return false; return false;
} }
} }
/**
* Specify sortings with fieldname as the key, and actual fieldname to sort as value.
* Example: array("MyCustomTitle"=>"Title", "MyCustomBooleanField" => "ActualBooleanField")
*
* @param array $casting
*/
public function setFieldSorting($sorting) {
$this->fieldSorting = $sorting;
return $this;
}
/**
* @return array
*/
public function getFieldSorting() {
return $this->fieldSorting;
}
/** /**
* Returns the header row providing titles with sort buttons * Returns the header row providing titles with sort buttons
@ -65,6 +86,7 @@ class GridFieldSortableHeader implements GridField_HTMLProvider, GridField_DataM
$currentColumn++; $currentColumn++;
$metadata = $gridField->getColumnMetadata($columnField); $metadata = $gridField->getColumnMetadata($columnField);
$title = $metadata['title']; $title = $metadata['title'];
if(isset($this->fieldSorting[$columnField]) && $this->fieldSorting[$columnField]) $columnField = $this->fieldSorting[$columnField];
if($title && $gridField->getList()->canSortBy($columnField)) { if($title && $gridField->getList()->canSortBy($columnField)) {
$dir = 'asc'; $dir = 'asc';
if($state->SortColumn == $columnField && $state->SortDirection == 'asc') { if($state->SortColumn == $columnField && $state->SortDirection == 'asc') {