Filter A Cgridview Using An External Dropdownlist

Hi everybody!

This is my first post and, in my case, that means I completely newbie in Yii world.

My problem is that I’m not able to filter a CGridView (created with Gii) using an external DropDownList. In the table ‘mytable’ there is a field date named ‘mydate’ but I need to filter just by the year. So I create a DropDownList with all the years in the datebase, being selected the present year, and when a new year y selected it has to change the data of the CGridView.

In my views/admin.php I’ve the DropDownList:


<?php

  $dateArray = CHtml::listData(Registros::model()->findAllBySql('SELECT mydate FROM mytable group by year(mydate)'),'year','year');

  echo CHtml::DropDownList('year_list', date('Y'), $dateArray); 


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

    'id'=>'mytable-grid',

    ...

?>

In my models/Mytable.php I have the function to return the year:




public function getYear()

{

  $date_field = DateTime::createFromFormat('Y-m-d',$this->mydate);

  return $date_field->format('Y');

}



But I’ve no idea how can I use the selected value in the controller and the model to filter the values of my CGridView by year.

Can anybody post me any idea, please? Thanks.

try that :

in your vi3ew :




echo CHtml::DropDownList('freesearch', date('Y'), $dateArray); 



in your model :




 public $freeSearch;

public function rules()

	{

			return array(

			...

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

                          ...

		         );


	}


public function search()

	{

         ...

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

        ...



maybe you can add in your grid view a column with only the year of your date and you put this column invisible.

I give you the beggining of the solution(I Hope, I am newbie , too) but I don’t know how you have to do to compare only with the year.

Dear Andres

Nath-o has detailed everything needed in such scenario.

Here is one workaround based on that.

Add a virtual property in Model.

Make the property safe on search.

Modify the search method.




public $year;


public function rules()

	{

			return array(

			...

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

                          ...

		         );


	}

public function search()

	{

        $criteria->select="*,YEAR(mydate) AS year";

         ...

        $criteria->compare('YEAR(mydate)', $this->year);

        ...

        }




In admin.php add a column for year




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

	'id'=>'consultation-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'id',

		........

                ........

		'year',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



regards.

Thank you very much to you two!!!

I’ve tried your code and it works. I’m getting very close to my needs.

:D

Hi guys I have the same requirement, I need to filter a cgridview table using an external dropdownlist. I followed the steps listed above, but I failed to filter the table. I need to filter the table by the attribute: "Area Afectada".

This is the code

in admin.php




<?php $sql= "SELECT NombreArea FROM area WHERE razonsocial ='$em'";?>

<?php $areas= CHtml::listdata (Area::model()->findAllbySql($sql),'NombreArea','NombreArea');  ?>

<?php echo CHtml::DropDownList('AreaAfec','valor',$areas, array ('prompt'=>'Seleccione')); ?>


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

	'id'=>'noconformidad-grid',

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

	'filter'=>$model,

	'columns'=>array(

		//'idNC',

		'Fecha',

		'AnalisisCausa',

		'Descripcion',		

		'Estado',

		'SistemaAfectado',		

		'Empresa',

		'AreaAfectada',		

		'Origen',

		

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>




in the model





public $AreaAfec;

	public function rules()

	{


		return array(

                           ....

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

			.....

		);

	}


	public function search()

	{


$criteria=new CDbCriteria;

$criteria->compare('idNC',$this->idNC);

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

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

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

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

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

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




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




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


return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

}



Am I failing to perform any step?

If anyone can help me I would greatly appreciate it