CGridView and filter from relation

Hi,

First, i know that this subject has already been covered in some of its aspects, but there are points which still seem quite obscur to me (i’m a newbie in Yii).

Let’s consider a Project model related to a ProjectCategory model : a Projet belongs to 1 ProjectCategory, so it contains id_category

  • i managed to have the title of the ProjectCategory inside the data shown, using the relation in the columns list

  • i also managed to have a dropdown with the related vales, instead of an integer text input, for the advanced search form, using a dropdownlist in the _form.php, instead of the textfield.

  • BUT i can’t find a way to have either a dropdownlist, or a text input (but which would perform the search in the title of the related table) in the filter zone, just on top of the grid. I think it works with the filter, but i don’t see how.

I’m really interested in the generic way of doing this, for consistency upon all the application.

Thanks

(and please apologize if i made some mistakes : english is not my native language)

My grid view call :


$this->widget('zii.widgets.grid.CGridView', array(

  'id'=>'projets-grid',

  'dataProvider'=>$model->search(),

  'filter'=>$model,

  'columns'=>array(

    'id',

    'col1',

    'col2',

    ...

    array(

      'name' => 'id_categorie',

      'value' => '$data->idCategorie->titre'),

    array(

      'class'=>'CButtonColumn',

    ),

  ),

));

My dropdown in the advanced search form


echo $form->dropDownList($model,'id_categorie', CHtml::listData(ProjetsCategories::model()->findAll(), 'id', 'titre'));

Check this if it helps in your case: Searching and sorting by related model in CGridView

Hi,

First, thanks.

Well, it helps if i want to have a text input field, but how to have the exact values in a dropdown (like in the advanced search), and then a classic search by value (taken from the dropdown value) ?

just define your column in CGridView like this:




   array(

      'name' => 'id_categorie',

      'value' => '$data->idCategorie->titre',

      'filter' => CHtml::listData( ProjetsCategories::model()->findAll(), 'id', 'titre' )

   ),



@redguy : you just made my day, thanks a lot :D