Dropdown filter on admin posts


I want to add a status filter to the admin page of blog demo but I have no idea on how to make this.

For instance, if the administrator wants to see only published posts, he could click over the dropdown at the top of the page and select Published so the system should refresh the grid/page and reloads it with the posts which status matchs the dropdown status selected. If he wants to see only Draft, the same, he clicks on the dropdown and selects Draft so the system reloads the grid with Draft posts only.

Please, any suggestions?

Thanks in advance.

Hi scoob,

i don't know whether you're still interested or not, but i'm using such filter options for my admin pages.

I'm using the following DropDown inside the admin view to select the filter:

<div class="filterDropDown">

<?php echo CHtml::beginForm('','get'); ?>


<input type="hidden" name="r" value="<?php echo $this->id.'/'.$this->action->id;?>" />

<?php echo CHtml::dropDownList('cat',



    array('empty'=>'All', 'submit'=>'')); ?>

<?php echo CHtml::endForm(); ?>


Inside my model i'm using these function the get the select-labels and their corresponding sql conditions (maybe since 1.0.5 it could be done nicer with named scopes):

<?php	public function getFilterOptions()


		return array(







	public function getFilterConditions()


		return array(


			2=>'Name LIKE 'fubar'',

			3=>'(test=1 AND test2=2) OR test12=3',



	public function getFilterCondition($optNumber)


		$conds = $this->getFilterConditions();


			return $conds[$optNumber];


			return '';



and finally inside my controller's admin action i'm using


$criteria=new CDbCriteria;

if(isset($_GET['cat']) && $_GET['cat'] > 0)


   $criteria->condition = Post::model()->getFilterCondition($_GET['cat']);



Hopefully this helps.  ;)



Hello, Yoshi, in fact I've already solved my problem, but thanks anyway…

your idea "getFilterConditions" is awesome!!!



Just for the record …

I use a different approach. I run 1 pure SQL query and group all fields that should be in the dropdown filters. (You can of course run a distinct-query for each dropdown)

public $filter_status = array();

public $filter_date = array();

public function search()


  $sql = 'SELECT status, date FROM post GROUP BY status, date';

  $result = Yii::app()->db->createCommand($sql)->queryAll();

  foreach ($result as $r)


    $status = $r['status'];

    $date = $r['date'];





  // .... Standard search() method continues ...

  $criteria = new CDbCriteria;

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

  // ...


… And then you can use $model->filter_status in defitition of your CGridView column like this:

'filter' => $model->filter_status,

This will give you all values that are in the whole table. Disadvantage is, that it allows you to set nonsence-filter-combinations and your table will be empty. If you want to see only meaningful values in the filter, you will need to add current filter condition to you $sql variable. You can do it using:

MyModel::model()->findAll($condition) // ... and add grouping