Merge branch 'master' into docs-ja-0203

This commit is contained in:
Nobuo Kihara
2016-02-03 22:49:22 +09:00
2 changed files with 157 additions and 0 deletions

View File

@@ -421,6 +421,86 @@ echo GridView::widget([
]); ]);
``` ```
### Отдельная форма фильтрации
Фильтров в шапке GridView достаточно для большинства задач, но добавление отдельной формы фильтрации не представляет
особой сложности. Она бывает полезна в случае необходимости фильтрации по полям, которые не отображаются в GridView
или особых условий фильтрации, например по диапазону дат.
Создайте частичное представление `_search.php` со следующим содержимым:
```php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\models\PostSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="post-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'creation_date') ?>
<div class="form-group">
<?= Html::submitButton('Искать', ['class' => 'btn btn-primary']) ?>
<?= Html::submitButton('Сбросить', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
```
и добавьте его отображение в `index.php` таким образом:
```php
<?= $this->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
<?= $form->field($model, 'creationFrom') ?>
<?= $form->field($model, 'creationTo') ?>
```
### Отображение зависимых моделей ### Отображение зависимых моделей

View File

@@ -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
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\models\PostSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="post-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'creation_date') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::submitButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
```
and include it in `index.php` view like so:
```php
<?= $this->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
<?= $form->field($model, 'creationFrom') ?>
<?= $form->field($model, 'creationTo') ?>
```
### Working with model relations ### Working with model relations