筛选器范围是筛选器使用的范围定义,通常与列表结合使用。与列表列相似,它们被以下区域引用:
所有过滤器作用域都以其各自的类型属性进行标识。
scopes:
myscope:
type: date
# ...以下过滤器作用域可用:
对于每个作用域,您可以指定这些属性(如适用)。
| Property | Description |
|---|---|
| label | a name when displaying the filter scope to the user. |
| type | defines how this scope should be displayed. Default: group. |
| conditions | enables or disables condition functions or specifies a raw where query statement to apply to each condition, see the scope type definition for details. |
| modelClass | class of the model to use as a data source and reference for local method calls. |
| modelScope | specifies a model query scope method defined in the list model to apply to the list query. The first argument will contain the query object (as per a regular scope method) and the second argument will contain the scope definition, including its populated value(s). |
| options | options to use if filtering by multiple items, supplied as an array. |
| optionsMethod | request options from a method name defined on the model or as a static method call, eg Class::method. |
| emptyOption | an optional label for an intentional empty selection. |
| default | supply a default value for the filter, as either array, string or integer depending on the filter value. |
| permissions | the permissions that the current backend user must have in order for the filter scope to be used. Supports either a string for a single permission or an array of permissions of which only one is needed to grant access. |
| dependsOn | a string or an array of other scope names that this scope depends on. When the other scopes are modified, this scope will reset. |
| nameFrom | a model attribute name used for displaying the filter label. Default: name. |
| valueFrom | defines a model attribute to use for the source value. Default comes from the scope name. |
| order | a numerical weight when determining the display order, default value increments at 100 points per scope. |
| after | place this scope after another existing scope name using the display order (+1). |
| before | place this scope before another existing scope name using the display order (-1). |
大多数过滤器将使用合理的默认值应用它们的作用域约束。 modelScope 属性可以用于使用 模型作用域方法定义 对过滤器查询应用自定义约束。
myfilter:
label: My Filter
type: group
modelScope: applyMyFilter在上面的例子中,我们期望在主模型上有一个名为myfilter的关系,以及在关联模型上定义了一个名为scopeApplyMyFilter的方法。第二个参数包含第二个参数将包含作用域定义,包括其已填充的值。
public function scopeApplyMyFilter($query, $scope)
{
return $query->whereIn('my_filter_attribute', (array) $scope->value);
}该 modelScope 属性也可以定义为一个静态的PHP类方法 (Class::method)。
myfilter:
label: My Filter
type: group
modelScope: "App\MyCustomClass::applyMyFilter"在这个例子中,该方法应该在指定的类名上声明为静态。
namespace App;
class MyCustomClass
{
public static function applyMyFilter($query, $scope)
{
return $query->whereIn('my_filter_attribute', (array) $scope->value);
}
}dependsOn 属性允许您将多个过滤器关联起来。当依赖项被修改时,过滤器范围将重置以允许新的条件。请看以下包含两个范围定义的示例。
country:
label: Country
type: group
state:
label: State
type: group
dependsOn: country
optionsMethod: getCityOptionsForFilter州作用域取决于国家作用域的值,并且选项在 PHP 中使用 getCityOptionsForFilter 方法进行过滤。此方法的第一个参数将包含完整的作用域定义集,包括它们的当前值。
public function getCityOptionsForFilter($scopes = null)
{
if ($scopes->country && ($countryIds = $scopes->country->value)) {
return self::whereIn('country_id', $countryIds)->lists('name', 'id');
}
return self::lists('name', 'id');
}