Thursday, March 8, 2012

How to make CGridview with dropdown filter

To get the filter of your CGridView you have to add filter parameters in your CGridview call. This filters can be an instance of the object active records. If you want to add some filtering of your model with dropdown menu you can see the example below :


$this->widget('zii.widgets.grid.CGridView',array(
   'dataProvider'=>$model->search(),
   'id'=>'risk-id',
   'filter'=>$model,
   'columns'=>array(
   array(
      'name'=>'No',
      'type'=>'raw',
      'value'=>'$this->grid->dataProvider->pagination->currentPage*$this->grid->dataProvider->pagination->pageSize + $row+1',//this for the auto page number of cgridview
      'filter'=>''//without filtering 
   ),
   array(
      'name'=>'name',
      'type'=>'raw',
      'value'=>'Chtml::link(Chtml::encode($data["name"]),array("risk/view","id"=>$data["risk_id"]))',
      'filter'=>CHtml::listData(Risk::model()->findAll(
                  array(
                   'select'=>array('name'),
                   'distinct'=>true
                   
                  )),"name","name")//this is the focus of your code
      
   ),
   array(
      'name'=>'date_identified',
      'type'=>'raw',
      'value'=>'Chtml::encode($data->date_identified)'
   ),
   array(
      'name'=>'description',
      'type'=>'raw',
      'value'=>'Chtml::encode($data->description)'
   ),
   array(
      'name'=>'type',
      'type'=>'raw',
      'value'=>'Chtml::encode($data->type)',
      
   ),
   array(
      'name'=>'link',
      'type'=>'raw',
      'value'=>'$data->link'
   ),
   

)

));

YOu can see that in the field of name, You can put the dropdown box to your filtering attribut. In this example I use the data of name in my risk table and get the distinct value of it.


You can also make your spesific column to exclude the filtering by passing parameter filter to null object or '' object.

And the result you can see here :




1 comment: