mirror of
https://github.com/symbiote/silverstripe-gridfieldextensions.git
synced 2024-10-22 15:05:39 +00:00
GridFieldOrderableRows: Support extra sort fields
This change allows for the specification of extra sort fields that are to be applied before the user-defined sort field's order. This is useful in scenarios where a GridField is used with one or more "fixed" columns which eg. categorize the entries and whose order is important but the user should still be allowed to modify the order within a category. This change affects the PHP code only, therefore the Javascript code will still let the user drag rows into foreign "categories". However, after dropping it the GridField will reload and the extra sort order will be enforced again.
This commit is contained in:
parent
9ff5de80d4
commit
79972af8a2
@ -25,6 +25,14 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||
*/
|
||||
protected $sortField;
|
||||
|
||||
/**
|
||||
* Extra sort fields to apply before the sort field.
|
||||
*
|
||||
* @see setExtraSortFields()
|
||||
* @var string|array
|
||||
*/
|
||||
protected $extraSortFields = null;
|
||||
|
||||
/**
|
||||
* @param string $sortField
|
||||
*/
|
||||
@ -50,6 +58,24 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|array
|
||||
*/
|
||||
public function getExtraSortFields() {
|
||||
return $this->extraSortFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets extra sort fields to apply before the sort field.
|
||||
*
|
||||
* @param string|array $fields
|
||||
* @return GridFieldOrderableRows $this
|
||||
*/
|
||||
public function setExtraSortFields($fields) {
|
||||
$this->extraSortFields = $fields;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the table which contains the sort field.
|
||||
*
|
||||
@ -128,7 +154,18 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||
$state->GridFieldOrderableRows->enabled = !$sorted;
|
||||
|
||||
if(!$sorted) {
|
||||
return $list->sort($this->getSortField());
|
||||
$sortterm = '';
|
||||
if ($this->extraSortFields) {
|
||||
if (is_array($this->extraSortFields)) {
|
||||
foreach($this->extraSortFields as $col => $dir) {
|
||||
$sortterm .= "$col $dir, ";
|
||||
}
|
||||
} else {
|
||||
$sortterm = $this->extraSortFields.', ';
|
||||
}
|
||||
}
|
||||
$sortterm .= $this->getSortField();
|
||||
return $list->sort($sortterm);
|
||||
} else {
|
||||
return $list;
|
||||
}
|
||||
@ -150,7 +187,18 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||
$this->httpError(400);
|
||||
}
|
||||
|
||||
$items = $list->byIDs($ids)->sort($field);
|
||||
$sortterm = '';
|
||||
if ($this->extraSortFields) {
|
||||
if (is_array($this->extraSortFields)) {
|
||||
foreach($this->extraSortFields as $col => $dir) {
|
||||
$sortterm .= "$col $dir, ";
|
||||
}
|
||||
} else {
|
||||
$sortterm = $this->extraSortFields.', ';
|
||||
}
|
||||
}
|
||||
$sortterm .= $field;
|
||||
$items = $list->byIDs($ids)->sort($sortterm);
|
||||
|
||||
// Ensure that each provided ID corresponded to an actual object.
|
||||
if(count($items) != count($ids)) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user