Cgridview Advanced Filtering

Hi

I have some trouble getting filtering in a CGridView to work the way I want. What I have is a CGridView of buttons. A button may be placed on a layer, but it is not required. A layer is placed in a panel. The structure is shown below:

4688

dbmodel.png

I have made a dropdown filter where I can choose which layer I want to filter on. So far so good. But I would also like to be able to filter on panel, wich is not directly related to button. How can I achieve this?

Thanks in advance!

I found a solution for this, but I’m not sure if it’s optimal. Anyway, this is what I do:

Button model:




public $panelId;




public function afterFind()

{

	parent::afterFind();


	if ($this->scenario === 'search' && !is_null($this->layer)) {

		$this->panelId = $this->layer->panelId;

	}

}




public function search()

{

	$criteria = $this->getSearchCriteria();


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

	$criteria->compare('t.name', $this->name, true);

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

	$criteria->with = array('layer'=>array('with'=>'panel'));


	return new CActiveDataProvider($this, array(

		'criteria' => $criteria,

		'pagination' => array(

			'pageSize' => 15,

		),

	));

}



View:




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

	'id'=>'button-grid',

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

	'filter'=>$model,

	'columns'=>array(

		array(

			'name'=>'panelId',

			'value'=>'!empty($data->layer) ? CHtml::encode($data->layer->panel->name) : ""',

			'filter' => CHtml::dropDownList('Button[panelId]', $model->panelId, CHtml::listData(Panel::model()->findAll(array('order'=>'name')), 'id', 'name'), array('empty' => 'Choose panel...')),

		),

		array(

			'name'=>'layerId',

			'value'=>'!is_null($data->layerId) ? CHtml::encode($data->layer->name) : ""',

			'filter' => CHtml::dropDownList('Button[layerId]', $model->layerId, CHtml::listData(Layer::model()->findAll(array('order'=>'name')), 'id', 'name'), array('empty' => 'Choose layer...')),

		),

		'name',

		array(

			'class' => 'CButtonColumn',

		),

	),

)); ?>



Any thouhts for improving this are welcome :)