Yii Framework Forum: CGridView Filtering related tables - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CGridView Filtering related tables Rate Topic: -----

#1 User is offline   Matheus Nunes 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 08-February 11
  • Location:Curitiba , Brazil

Posted 08 February 2011 - 06:38 AM

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
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,338
  • Joined: 12-October 09
  • Location:Croatia

Posted 08 February 2011 - 06:45 AM

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...
Find more about me.... btw. Do you know your WAN IP?
1

#3 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 08 February 2011 - 06:49 AM

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.
4

#4 User is offline   Matheus Nunes 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 08-February 11
  • Location:Curitiba , Brazil

Posted 08 February 2011 - 08:31 AM

Thanks zaccaria and mdomba

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

#5 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 385
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 08 February 2011 - 09:24 AM

View Postzaccaria, on 08 February 2011 - 06:49 AM, said:

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.

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',
                                        ),    

Posted Image
1

#6 User is offline   reader 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 07-March 12

Posted 07 March 2012 - 07:08 AM

View Postfouss, on 08 February 2011 - 09:24 AM, said:

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.....
0

#7 User is offline   Neil Bardos 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 16-November 12

Posted 18 November 2012 - 09:39 PM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users