There are a lot of topic about CGridView already, I just want to share one small thing: How to define customer image into action buttons.
In CGridView, there are 3 default buttons – View, Update, and Delete, if you want to add you own buttons, for example, I need run a report in the grid, so I create run button, what I did below is insert a new button into CGridView Columns:
array(//we need run the report action
'class'=>'CButtonColumn',
'header'=>'Actions',
'template'=>'{run}',
'buttons'=>array(
'run'=>array(
'url'=>'Yii::app()->createUrl("report", array("run"=>$data->report_id))',
'imageUrl'=>Yii::app()->baseUrl."/images/play.png",
'label'=>'Run this report',
'imageoptions'=>array(
'height'=>"24px",
'width'=>"24px",
'class'=>"textmiddle",
),
),
),
),
You will see a new “imageoptions” in the definition, which is using to define the html options for the button image. If you don’t have this, you need resize your image into 24X24px before you can use that.
I added "imageoptions" in CButtonColumn.php, so it can handle the images in the action buttons. it will output
<a href="/index.php/report/run/1" title="Run the report">
<img class="textmiddle" width="24px" height="24px" alt="Run the report" src="/images/play.png">
</a>
To add "imageoptions" is not difficult. add one row into CButtonColumn.php (Yii 1.1.14)
/**
* Renders a link button.
* @param string $id the ID of the button
* @param array $button the button configuration which may contain 'label', 'url', 'imageUrl' and 'options' elements.
* See {@link buttons} for more details.
* @param integer $row the row number (zero-based)
* @param mixed $data the data object associated with the row
*/
protected function renderButton($id,$button,$row,$data)
{
if (isset($button['visible']) && !$this->evaluateExpression($button['visible'],array('row'=>$row,'data'=>$data)))
return;
$label=isset($button['label']) ? $button['label'] : $id;
$url=isset($button['url']) ? $this->evaluateExpression($button['url'],array('data'=>$data,'row'=>$row)) : '#';
$options=isset($button['options']) ? $button['options'] : array();
//add image html options if there is some.
$imgOptions=isset($button['imageoptions']) ? $button['imageoptions'] : array();
if(!isset($options['title']))
$options['title']=$label;
if(isset($button['imageUrl']) && is_string($button['imageUrl']))
echo CHtml::link(CHtml::image($button['imageUrl'],$label,$imgOptions),$url,$options);
else
echo CHtml::link($label,$url,$options);
}
If you don’t want to change the Yii framework, you can create like MyButtonColumn under you components folder. Just change to ‘class’=>“application.components.MyButtonColumn” in your button definition.
Any comments are welcome.