mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
API CHANGE: Added DataList::filterByCallback() to allow filtering by PHP code.
This commit is contained in:
parent
70d5ffefdd
commit
0821f021cc
@ -256,6 +256,24 @@ class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortab
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter this DataList by a callback function.
|
||||||
|
* The function will be passed each record of the DataList in turn, and must return true for the record to be included.
|
||||||
|
* Returns the filtered list.
|
||||||
|
*
|
||||||
|
* Note that, in the current implementation, the filtered list will be an ArrayList, but this may change in a future
|
||||||
|
* implementation.
|
||||||
|
*/
|
||||||
|
public function filterByCallback($callback) {
|
||||||
|
if(!is_callable($callback)) throw new LogicException("DataList::filterByCallback() must be passed something callable.");
|
||||||
|
|
||||||
|
$output = new ArrayList;
|
||||||
|
foreach($this as $item) {
|
||||||
|
if($callback($item)) $output->push($item);
|
||||||
|
}
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Translates a Object relation name to a Database name and apply the relation join to
|
* Translates a Object relation name to a Database name and apply the relation join to
|
||||||
* the query. Throws an InvalidArgumentException if the $field doesn't correspond to a relation
|
* the query. Throws an InvalidArgumentException if the $field doesn't correspond to a relation
|
||||||
|
Loading…
x
Reference in New Issue
Block a user