How to exclude a CActiveRecord property from filtering with CGridView in Yii?

146 views Asked by At

In a Yii 1.1 application I'm displaying the data a table using the CGridView.

$dataProvider = $model->search();
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'my-grid',
    'dataProvider' => $dataProvider,
    'filter' => $model,
    'columns' => array(
        'id',
        'user.firstname',
        'user.lastname',
        'user.email',
        'type',
        'foo', // <-- on-the-fly added attribute
        'bar', // <-- on-the-fly added attribute
        ...
    ),
));

Curretly all columns of model's table (id, type) and even the on-the-fly added attributes (foo, bar) get a filter input field. (The filters of the additional properties don't work.)

I want to have filters for only some of the fields, e.d. id and tipe. How can I enable only the filters I need / disable the filters I don't need?

1

There are 1 answers

0
automatix On

Here is a solution, that looks like a workaround -- just do define the not filtered properties explicitly and set the filter parameter for each one to false:

...
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'my-grid',
    'dataProvider' => $dataProvider,
    'filter' => $model,
    'columns' => array(
        'id',
        ...
        'type',
        array(
            'name' => 'foo',
            'filter' => false,
        ),
        array(
            'name' => 'bar',
            'filter' => false,
        ),
        ...
    ),
));

It works, but I think, there should be a more elegant solution...