Extend query of the filter options lists

This commit is contained in:
Nicolas Lescop 2014-12-23 14:57:28 +01:00
parent 832a92e925
commit 99d34789a4
2 changed files with 23 additions and 2 deletions

View File

@ -216,6 +216,13 @@ class ListController extends ControllerBehavior
return $widget->onRefresh();
});
/*
* Extend the query of the list of options
*/
$filterWidget->bindEvent('filter.extendQuery', function($query, $scope) {
$this->controller->listFilterExtendQuery($query, $scope);
});
// Apply predefined filter values
$widget->addFilter([$filterWidget, 'applyAllScopesToQuery']);
@ -350,6 +357,16 @@ class ListController extends ControllerBehavior
{
}
/**
* Controller override: Extend the query used for populating the filter
* options before the default query is processed.
* @param October\Rain\Database\Builder $query
* @param array $scope
*/
public function listFilterExtendQuery($query, $scope)
{
}
/**
* Returns a CSS class name for a list row (<tr class="...">).
* @param Model $record The populated model used for the column

View File

@ -208,12 +208,16 @@ class Filter extends WidgetBase
protected function getOptionsFromModel($scope, $searchQuery = null)
{
$model = $this->scopeModels[$scope->scopeName];
$query = $model->newQuery();
$this->fireEvent('filter.extendQuery', [$query, $scope]);
if (!$searchQuery) {
return $model->all();
return $query->get();
}
$searchFields = [$model->getKeyName(), $this->getScopeNameColumn($scope)];
return $model->searchWhere($searchQuery, $searchFields)->get();
return $query->searchWhere($searchQuery, $searchFields)->get();
}
/**