Cgridview Dropdown Filter With Grouped Values

I implemented this: http://stackoverflow.com/questions/10087747/cgridview-filter-dropdown-from-array

to get a dropdown filter for my CGridView which works great.

however I need to build in the following:

For filter options:

Red

Yellow

Green

I need to have an additional entry:

[Red | Green]

which returns all Red and all Green but excludes Yellow.

Any help on how I can maybe do this would be really appreciated

gvanto

Dear Friend.

I have to some extend simulated your scenario.

I have got two tables.

  1. BOX id, c_id(FK).

  2. COLOR id,color.

I have created three filters in admin.php.

The central filter has column values.

The other filters are only for filtering purposes.




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

	'id'=>'box-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'id',

		array(

		'name'=>"filter1",

		'filter'=>array(1=>'red',2=>'blue',3=>'green')

		),

		array(

		'name'=>"filter2",

		'value'=>'$data->color->color',

		'filter'=>array(1=>'red',2=>'blue',3=>'green')

		),

		array(

		'name'=>"filter3",

		'filter'=>array(1=>'red',2=>'blue',3=>'green')

		),

		array(

			'class'=>'CButtonColumn',

		),

	),



This is the model Box.php




<?php




class Box extends CActiveRecord

{

         public $filter1;

         public $filter2;

         public $filter3;

	

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}

	

	

	public function tableName()

	{

		return 'box';

	}


	

	public function rules()

	{

		

		return array(

			array('c_id', 'required'),

			array('c_id', 'numerical', 'integerOnly'=>true),

			

			array('id,filter1,filter2, filter3', 'safe', 'on'=>'search'),

		);

	}


	

	public function relations()

	{

		

		return array(

		'color'=>array(self::BELONGS_TO,'Color','c_id')

		);

	}




	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'c_id' => 'C',

		);

	}


	

	public function search()

	{


		$criteria=new CDbCriteria;

                $criteria->select=array('id','c_id');

                $criteria->with=array('color');

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

		

	

		if($this->filter1!==null||$this->filter2!==null||$this->filter3!==null)

		    $criteria->addInCondition('c_id',array($this->filter1,$this->filter2,$this->filter3));

              

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}




I hope I helped a bit.

Thanks a million seenivasan, this has helped me solve it! Works great now.

Code for CGridView:

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

‘id’=>‘puzzle-grid’,

‘dataProvider’=>$model->search(),

‘filter’=>$model,

‘columns’=>array(

‘id’,

‘group.name’,

array(

‘name’=>‘group_id’,

‘filter’=>CHtml::listData(PuzzleGroup::model()->findAll(),‘id’,‘name’),

),

‘category.name’,

‘phrase’,

array(

‘name’=>‘enabled’,

‘filter’=>array(‘Yes’=>‘Yes’,‘No’=>‘No’),

),

array(

‘class’=>‘CButtonColumn’,

),

),

));

//‘group.name’,

?>

Relations between the two tables:

public function relations()

{

// NOTE: you may need to adjust the relation name and the related

// class name for the relations automatically generated below.

return array(

‘tbHints’ => array(self::HAS_MANY, ‘TbHints’, ‘puzzle_id’),

‘group’ => array(self::BELONGS_TO, ‘PuzzleGroup’, ‘group_id’),

‘category’ => array(self::BELONGS_TO, ‘PuzzleCategory’, ‘category_id’),

);

}

Code for search function:

public function search()

{

// Warning: Please modify the following code to remove attributes that

// should not be searched.

$criteria=new CDbCriteria;

$criteria->compare(‘id’,$this->id);

$criteria->compare(‘group_id’,$this->group_id);

$criteria->compare(‘category_id’,$this->category_id);

$criteria->compare(‘phrase’,$this->phrase,true);

$criteria->compare(‘enabled’,$this->enabled,true);

return new CActiveDataProvider($this, array(

‘criteria’=>$criteria,

));

}

For the above code can anyone please let me know how to create CGridView for Puzzle with dropdownlist filters containind puzzlegroup name from PuzzleGroup which is related to Puzzle with group_id.

Also i want to display PuzzleGroup name instead of id in the CGridView.