CGridView Filtering related tables

Hi

I have a problem with CGridView, the grid show the textbox filter but when i tip something and then press confirm, the grid doesn’t filter the data.

here is my code


/*the Controller*/

$dataProvider=new CActiveDataProvider('DMSenha',

				array(

						'criteria'=>array(

							'select'=>'t.id_vida, t.id_senha, count(id_senha) as total_senhas',

							'join'=>'LEFT JOIN vida as v ON  (v.id_vida = t.id_vida)',

							'group'=>'t.id_vida',

							'order'=>'v.nome_vida',	

							'with'=>array('vida'),

							'together'=>'true',				

						),

					)

			);

		$this->render('index',array(

			'dataProvider'=>$dataProvider,

		));






/*the View*/

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

				'dataProvider'=> $dataProvider,

				'filter'=>DMSenha::model(),

				'columns'=>array(

					'vida.cod_vida',

					array(

						'name'=>'vida',

						'filter' => CHtml::textField('vida.nome_vida'), 

           				 'value' => 'DMVida::Model()->FindByPk($data->id_vida)->nome_vida',

					),	

					'total_senhas',		

				),

				'id'=>'gridsenha',

				'selectableRows'=>1,

				'selectionChanged'=>'function(id){

					$id_senha = $(\'#gridsenha\').yiiGridView.getSelection(id);

					

					$(\'#hload\').ajaxStart(function(){$(this).show();});

					$(\'#hload\').ajaxStop(function(){$(this).hide();});

					$(\'#diagDialog\').remove();

					$(\'#detailAjax\').load("'.$this->createUrl('/GestaoIntercorrencias/dCSenha/detailItem').'/id/"+$id_senha);

					}'	,	

				));




/*The Relation in DMSenha*/

'vida' => array(self::BELONGS_TO, 'DMVida', 'id_vida'),

I need to filter the vida.nome_vida and vida.cod_vida columns.

I have already looked at the forum and over the internet, but i didnt find any solution.

Can anyone help me with that ?

Thanks

Hi and welcome to the forum…

I would advise you to generate a model and CRUD with Gii… and then inspect the generated code…

so that you get a grasp about how the filtering in CGridView works… mainly using the $model->search() function

If you need more info just ask here in the forum…

I would be better to start once again with the code genrated by Gii.

You miss all conditions, and also the filter sould be an active text field.

In the view:




array(

            'name'=>'vida',

            'filter' => CHtml::activeTextField($model, 'nome_vida'), 

            'value' => '$data->vida->nome_vida',

                                        ),    



In the model you should add:




public $nome_vida;

....


//rules

array('nome_vida', 'safe', 'on'=>'search')


// public function search()


$criteria->compare('nome_vida', $this->nome_vida, true);


$criteria->with=array('vida')




All other parts you can leave as Gii generated.

Thanks zaccaria and mdomba

regenerating the model and the crud, and adding the code that zac has passed, the filter now is working.

This is a very clear step by step tutorial on how to make related tables Filter. work with CGridView .

Those who want it with dropdownlist should do like this:




array(

            'name'=>'vida',

            'filter'=>CHtml::listData(Model::model()->findAll(), 'nome_vida','nome_vida'),

            'value' => '$data->vida->nome_vida',

                                        ),    



Hi all,

I have problem in filtering with dropDownList. I have 2 models Servicecall and JobStatus,

relation between these models is

‘jobStatus’ => array(self::BELONGS_TO, ‘JobStatus’, ‘job_status_id’),

My code in search() is,

$criteria->with = array( ‘customer’,‘jobStatus’);

$criteria->compare( ‘jobStatus.name’, $this->job_status, true );

My code in admin view is,

array(‘name’=>‘job_status’,

  'filter'=> CHtml::listData(JobStatus::model()->findAll(), 'id', 'name'),


  'value'=>'$data->jobStatus->name',


 ),

I am able to get the dropDown in view bur its not filtering.I am able to filter without dropDown, I am not able to figure out the mistake, Please help…

@zaccaria - thanks a lot! that’s what I need. I had the same problem and it works now. Thanks man!