How to add dropdown filter to GridView

So I have this status column in GridView.

What I would like to do is to allow users to search by status, but I want them to be able to use dropdown menu with 2 options to select ( Active, Not Active ). Of course dropdown will replace this input field where you have to type values by default.

Dropdown would be simple as this:




<select>

  <option value="10">Active</option>

  <option value="1">Not active</option>

</select>



So when user select Active, Search model will filter by status in user table with the value of 10.

In my GridView widget I already have this:




// other columns

[

    'attribute'=>'status',

    'value' => function ($data) {

        return $data->statusName;

    },

    'contentOptions'=>function($model, $key, $index, $column) {

        return ['class'=>CssHelper::statusCss($model->statusName)];

    }

],

//other columns



And in search model :




$query->andFilterWhere([

    'id' => $this->id,

    'status' => $this->status,

]);



Do anyone know how this can be done ?

I looked into this one not so long ago, so:

GridVew are made of “datacolomns”. Datacolumn has a ‘filter’ attribute that can either be a boolean (to suppress the filter) or an array of list of values.

If you give it a list of value, it will build a drop down selector on it.

Have a look here.

/p

Thanks a lot.

Hello,

Maybe it’s worth a try. It works for me.

In you index.php GridView





            [

                'attribute' => 'status',

                'format' => 'raw',

                'value' => function ($model) {                      

                        return $model->ststus== '10' ? '<span style="color:green;">Active</span>' : '<span style="color:red;">Inactive</span>';

                },

                'filter'=> Html::dropDownList('SearchUser[status]','',array('' => 'Select Status') + common\models\User::getStatus(),['prompt'=>'-- LIST ALL --','class' => 'form-control']),

            ],



In your model

const STATUS_DELETED = 0;


const STATUS_ACTIVE  = 10;

‘’

‘’’ ‘’’

‘’’

‘’’




    public static function getStatus()

    {

        return array(

            self::STATUS_DELETED => 'Inactive',

            self::STATUS_ACTIVE => 'Active',

            );

    }



To add filter dropdown:

array(

'name'=&gt;'status', //name of the column present in table


'filter'=&gt;array('0'=&gt;'active', '1'=&gt;'not active'), //key=&gt;value pair where 0 &amp; 1 are the values present in table column.

),

add the above code in your admin.php file

For more precise understanding check the below code:

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(

'id'=&gt;'comments-grid',


'dataProvider'=&gt;&#036;model-&gt;search(),


'filter'=&gt;&#036;model,


'columns'=&gt;array(


	'intId',


	array(


		'name'=&gt;'status', //name of the column present in table


		'filter'=&gt;array('0'=&gt;'active', '1'=&gt;'not active'), //key=&gt;value pair where 0 &amp; 1 are the values present in table column.


	), //adding filter dropdown


	array(


		'class'=&gt;'CButtonColumn',


	),


),

)); ?>