4

I am trying to add filter to GridView widget included in _form.php. The grid is showing fine, even filter field is shown, but the filter is not working.

Here is my code:

<?php  
   $searchModel = New CitySearch(); ?>


    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

          //  'id',
            'city_name',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>   

1 Answer 1

4

I have found the solution. The search model must actually be searched before attaching it to the GridView. Therefore, I just needed to add one line to get it to work:

$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

The entire code would like like that:

<?php  
   $searchModel = New CitySearch(); 
   $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
?>


<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

          //  'id',
            'city_name',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.