. . .

Replace text box with drop down list in filter of CGridView

Published: April 12, 2011

On This Page

    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"))',
    		),
    
    	),
    ));

     

    Don't forget to share this post

      Let's Build Digital Excellence Together


      • Cost Efficient Solutions.
      • Minimal Timelines.
      • Effective Communication.
      • High Quality Standards.
      • Lifetime Support.
      • Transparent Execution.
      • 24/7 Availability.
      • Scalable Teams.

      Join Our 200+ Happy Clients Across Globe


      Free Consultation.

        Do you need tech help of your startup/business? Experts from our team will get in touch with you.

        Please do not post jobs/internships inquiries here.