Adding a Filter to the GridView Widget

I am trying to add a filter to the gridview widget included in _form.php. The grid displays the exact, even the filter field is displayed, but the filter does not work.

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'],
        ],
    ]); ?>   
+4
source share
1 answer

I have found a solution. Before attaching it to a GridView, you need to find a regular model. So I just need to add one line to make it work:

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

All code would like this:

<?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'],
        ],
    ]); ?>   
+4
source

All Articles