过滤器范围是过滤器使用的范围定义,通常与列表结合使用。与列表列类似,它们被以下区域引用:
所有过滤作用域都被标识为其各自的 type 属性。
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');
}