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: 407
  • 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: 407
  • 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

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