By default, Yii displays a textbox to search (filter) within results/records displayed by CGridView. In order to change those textboxes to drop down list (combo boxes), all you need to do is to assign an array to "filter" property of grid column. Have a look at the code below to get clearer picture.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'business-grid',
'dataProvider'=>$dataProvider,
'filter'=> Business::model(),
'columns'=>array(
'business_id',
'name',
'package_id'=> array(
'name' => 'package_id',
'value' => '$data->package->package_title',
'filter'=> CHtml::listData(Packages::model()->findAll(array('order'=>'package_title')), 'package_id', 'package_title')
),
'user_id'=>array(
'name' => 'user_id',
'value' => '$data->user->name',
'filter'=> CHtml::listData(Users::model()->findAll(array('order'=>'firstname')), 'id', 'name')
),
'categories' => array(
'name' => 'categories',
'value' => '$data->returnAllCategories(", ",false);',
'filter'=> CHtml::listData(Categories::model()->findAll(array('order'=>'category')), 'cat_id', 'category')
),
'keywords' => array(
'header' => 'Keywords',
'value' => '$data->returnAllKeywords(", ",false);',
),
'links'=>array(
'header'=>'Manage',
'type'=>'raw',
'value'=>'CHtml::link(CHtml::image(Yii::app()->request->baseUrl."/images/admin/approve.png","Approve"), array("approve","id"=>$data->business_id,"item"=>"business"),array("class"=>"approve-link","title"=>"Approve Business"))." ".
CHtml::link(CHtml::image(Yii::app()->request->baseUrl."/images/admin/reject.png","Reject"), array("reject","id"=>$data->business_id,"item"=>"business"),array("class"=>"reject-link","title"=>"Reject Business"))',
),
),
));
8 Comments
Gr8 this helps me a lot…
Thank you very much
Thanks for this. It looks like you are pulling data from multiple tables, do all those have a relationship? Is there a way to populate CGridView from multiple tables that do not have a relationship? If that is what you are doing can you please post your associated code that allows this.
@Mike In the example shown above, all the tables were related but I think you can do some thing like this
Nice explanation but is not quite working for me… 🙁
Looks OK, but the filter doesn’t actually filter anything…
The search textbox for the column gets replaced with the dropdown list, and the values shown inside the list are correct – I’ve verified the returned array contents for the filter:
$test = CHtml::listData(Status::model()->findAll(), ‘id’, ‘code’);
print_r($test);
The Drop Down shows blank on start (which is ok), and when I click on the drop down and select a value, the GridView does some processing (shows the little circle icon moving) and gets back to the original view, nothing filtered and the dropdown back to blank.
Note: If I search through the ‘Advanced Search’ link in the same form (GII generated), it works Ok.
Am I missing something? need to enable something somewhere else?
Are you providing correct attribute names for valueField parameter of CHtml::listData function?
I am also facing the same problem as Big-Z..
No processing … Pls help
Please check the ajax call in your browser developer toolbar. Check that correct values are sent to web server.
Do you have a URL where we can have a look at it?
I had the same problem, using DX-Sadafs advice I had a look at the request being sent when I chose from the dropdown and when I used the search. I saw I had named my columns incorrectly, so changed ‘name’=>’badName’ to ‘name’=>’goodName’ and everything worked.