P
P
Petr Fronin2016-03-14 10:37:12
Yii
Petr Fronin, 2016-03-14 10:37:12

Yii2 gridviews (SqlDataProvider) how to organize search?

In view:

GridView::widget([
         'dataProvider' => $dataProvider,
         'filterModel' => $modelSearch,
         'columns' => [
                'id',
                [
                    'label' => 'Логин',
                    'attribute' => 'username',
                ],
                [
                    'label' => 'Имя',
                    'attribute' => 'firstname',
                ],
                [
                    'label' => 'Фамилия',
                    'attribute' => 'lastname',
                ],
                [
                    'label' => 'E-Mail',
                    'attribute' => 'email',
                ],
         ],
]);

In controller:
$totalCount = Yii::$app->db->createCommand('SELECT COUNT(*) 
                                                                           FROM Users 
                                                                           Where validation_email=:status', [':status' => 'confirmed'])
                                             ->queryScalar();
$sql = 'SELECT * FROM Users Where validation_email=:status';
$dataProvider = new SqlDataProvider([
'sql' => $sql,
'params' => [':status' => 'confirmed'],
'totalCount' => (int)$totalCount,
'sort' => [
         'attributes' => [
                        'id',
                        'username',
                        'lastname',
                        'firstname',
                        'email',
            ],
],
'pagination' => [
              'pageSize' => 10,
],
]);

Sorting works, how to organize the search? The view says 'filterModel' => $modelSearch

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2016-03-14
@amar_std

Through modelsearch. When generating crud through gii, even a file with filters should have been created (If serchmodel was specified). The _search.php file in the views folder
with something like the following code:

<?php $form = ActiveForm::begin([
        'action' => ['index'],
        'method' => 'get',
    ]); ?>

    <div class="col-xs-3"><?= $form->field($model, 'text') ?></div>
    <div class="col-xs-3"><?= $form->field($model, 'cat') ?></div>
    <div class="col-xs-3"><?= $form->field($model, 'status')->dropDownList($model->getStatusList(),['prompt'=>''])
     ?></div>
    <div class="col-xs-3"><?= $form->field($model, 'email') ?></div>

    <div class="form-group col-xs-12">
        <?= Html::submitButton(Yii::t('app', 'Search'), ['class' => 'btn btn-primary']) ?>
        <?= Html::resetButton(Yii::t('app', 'Reset'), ['class' => 'btn btn-default']) ?>
        <?= Html::a(Yii::t('app', 'Создать Запрос'), ['create'], ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

And the action where the form goes, by default, is index approximately the following:
public function actionIndex()
    {
        $searchModel = new RequestSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

At the same time, all the logic of filtering and searching is placed in RequestSearch, which is inherited from Request. Again, gii does all this automatically.
Read:
www.yiiframework.com/doc-2.0/guide-output-data-wid...

Y
yura_born, 2020-02-18
@yura_born

The same problem of creating a search in the SQLdataProvider+GridViews bundle. I
reviewed a bunch of articles, but I still don't understand how to organize a search or a filter.
The controller and the view were redone after gii by hand, there is a _search file.
Maybe someone will show a normal, working example (controller view model). I so understood with SQLdataProvider in general the filter cannot be made.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question