diff --git a/docs/guide-ru/output-data-widgets.md b/docs/guide-ru/output-data-widgets.md index 1f46664029..8e5caac12c 100644 --- a/docs/guide-ru/output-data-widgets.md +++ b/docs/guide-ru/output-data-widgets.md @@ -421,6 +421,86 @@ echo GridView::widget([ ]); ``` +### Отдельная форма фильтрации + +Фильтров в шапке GridView достаточно для большинства задач, но добавление отдельной формы фильтрации не представляет +особой сложности. Она бывает полезна в случае необходимости фильтрации по полям, которые не отображаются в GridView +или особых условий фильтрации, например по диапазону дат. + +Создайте частичное представление `_search.php` со следующим содержимым: + +```php + + +
+ ['index'], + 'method' => 'get', + ]); ?> + + field($model, 'title') ?> + + field($model, 'creation_date') ?> + +
+ 'btn btn-primary']) ?> + 'btn btn-default']) ?> +
+ + +
+``` + +и добавьте его отображение в `index.php` таким образом: + +```php +render('_search', ['model' => $searchModel]) ?> +``` + +> Note: если вы используете Gii для генерации CRUD кода, отдельная форма фильтрации (`_search.php`) +генерируется по умолчанию, но закомментирована в представлении `index.php`. Вам остается только раскомментировать +эту строку и форма готова к использованию! + +Для фильтра по диапазону дат мы можем добавить дополнительные атрибуты `createdFrom` и `createdTo` в поисковую модель +(их нет в соответствующей таблице модели): + +```php +class PostSearch extends Post +{ + /** + * @var string + */ + public $createdFrom; + + /** + * @var string + */ + public $createdTo; +} +``` + +Расширим условия запроса в методе `search()`: + +```php +$query->andFilterWhere(['>=', 'creation_date', $this->createdFrom]) + ->andFilterWhere(['<=', 'creation_date', $this->createdTo]); +``` + +И добавим соответствующие поля в форму фильтрации: + +```php +field($model, 'creationFrom') ?> + +field($model, 'creationTo') ?> +``` ### Отображение зависимых моделей diff --git a/docs/guide/output-data-widgets.md b/docs/guide/output-data-widgets.md index 4f13d2fe10..8479af6739 100644 --- a/docs/guide/output-data-widgets.md +++ b/docs/guide/output-data-widgets.md @@ -421,6 +421,83 @@ echo GridView::widget([ ]); ``` +### Separate filter form + +Most of the time using GridView header filters is enough, but in case you need a separate filter form, +you can easily add it as well. You can create partial view `_search.php` with the following contents: + +```php + + +
+ ['index'], + 'method' => 'get', + ]); ?> + + field($model, 'title') ?> + + field($model, 'creation_date') ?> + +
+ 'btn btn-primary']) ?> + 'btn btn-default']) ?> +
+ + +
+``` + +and include it in `index.php` view like so: + +```php +render('_search', ['model' => $searchModel]) ?> +``` + +> Note: if you use Gii to generate CRUD code, the separate filter form (`_search.php`) is generated by default, +but is commented in `index.php` view. Uncomment it and it's ready to use! + +Separate filter form is useful when you need to filter by fields, that are not displayed in GridView +or for special filtering conditions, like date range. For filtering by date range we can to add non DB attributes +`createdFrom` and `createdTo` to the search model: + +```php +class PostSearch extends Post +{ + /** + * @var string + */ + public $createdFrom; + + /** + * @var string + */ + public $createdTo; +} +``` + +Extend query conditions in the `search()` method like so: + +```php +$query->andFilterWhere(['>=', 'creation_date', $this->createdFrom]) + ->andFilterWhere(['<=', 'creation_date', $this->createdTo]); +``` + +And add the representative fields to the filter form: + +```php +field($model, 'creationFrom') ?> + +field($model, 'creationTo') ?> +``` ### Working with model relations