Dev
Dev

Reputation: 305

yii2 gridview filter with custom value

I have displayed the values as follows :

1 => "Active", 0 => "Inactive"

1,0 are the db values.

This is my column.

[
            'label' => 'Status',
            'attribute' => 'activeStatus',
            'format' => 'html',
            'value'=>function ($data){return $data->activeStatus ? '<span class="label label-success">Active</span>' : '<span class="label label-danger">Inactive</span>';} 
        ],

How to enable grid filter with Active/Inactive? It can be a dropdownlist or just by typing Active/Inactive.

Upvotes: 0

Views: 2526

Answers (1)

Insane Skull
Insane Skull

Reputation: 9368

GridView

[
    'label' => 'Status',
    'attribute' => 'activeStatus',
    'format' => 'html',
    'value' => function ($data){
        return $data->activeStatus ? '<span class="label label-success">Active</span>' : '<span class="label label-danger">Inactive</span>';
    },
    'filter' => [1 => 'Active', 0 => 'Inactive'],
],

SearchModel

public function rules()
{
    return [
        [['activeStatus'], 'integer'],
        .
        .
        .
    ];
}

// grid filtering conditions
$query->andFilterWhere([
     'activeStatus' => $this->activeStatus,
]);

Upvotes: 1

Related Questions