Yii Framework Forum: Dropdown filter on admin posts - Yii Framework Forum

Jump to content

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

Dropdown filter on admin posts Rate Topic: -----

#1 User is offline   Junior - df9 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 416
  • Joined: 24-May 09
  • Location:Brazil

Posted 24 May 2009 - 05:46 PM

Hello,

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

#2 User is offline   yoshi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 28-February 09
  • Location:Germany

Posted 16 June 2009 - 01:16 PM

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'); ?>
Select:
<input type="hidden" name="r" value="<?php echo $this->id.'/'.$this->action->id;?>" />
<?php echo CHtml::dropDownList('cat',
    isset($_GET['cat'])?(int)$_GET['cat']:0,
    Post::model()->getFilterOptions(),
    array('empty'=>'All', 'submit'=>'')); ?>
<?php echo CHtml::endForm(); ?>
</div>


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(
1=>'Filter1',
2=>'Filter2',
3=>'Filter3',
);
}

public function getFilterConditions()
{
return array(
1=>'FlagXY=1',
2=>'Name LIKE 'fubar'',
3=>'(test=1 AND test2=2) OR test12=3',
);
}

public function getFilterCondition($optNumber)
{
$conds = $this->getFilterConditions();
if(isset($conds[$optNumber]))
return $conds[$optNumber];
else
return '';
}
?>


and finally inside my controller's admin action i'm using
<?php 
$criteria=new CDbCriteria;
if(isset($_GET['cat']) && $_GET['cat'] > 0)
{
  $criteria->condition = Post::model()->getFilterCondition($_GET['cat']);
}
?>


Hopefully this helps.  ;)

Greets,
yoshi
1

#3 User is offline   Junior - df9 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 416
  • Joined: 24-May 09
  • Location:Brazil

Posted 21 June 2009 - 05:00 PM

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

your idea "getFilterConditions" is awesome!!!

thanks!!

:)
0

#4 User is offline   rackycz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 16-December 09

Posted 14 August 2014 - 01:59 AM

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'];
  
    $this->filter_status[$status]=$status;
    $this->filter_date[$date]=$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

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