PcLinkButton extension ¶
This extension enables rendering a 'dynamic' image per row object in a grid view. The class provided by this extension, PcLinkButton, is a brother class to other *Column classes - CDataColumn, CLinkColumn, CButtonColumn and CCheckBoxColumn. What it provides is similar to the way "urlExpression" and "labelExpression" are evaluated in CLinkColumn: an ability to render "imageUrlExpression" per data object being rendered.
Requirements ¶
Not many I guess. Tested on Yii v1.1.10.
Usage ¶
- Extract the contents of this extension and put PcLinkButton.php in your /protected/components directory.
- In your grid view rendering, do something similar to:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'my-id',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
//... more columns
array(
'class' => 'PcLinkButton',
'imageUrlExpression' => 'SomeModel::getWebPath($some_param) . basename($data->icon_filename)',
'urlExpression' => '"/pathTo/" . strtolower($data->name)',
'labelExpression' => '$data->name',
'header' => "Column Title,
),,
));
Resources ¶
Changelog ¶
- v1.0 Added sorting capability to PcLinkButton's columns.
- v0.1 Initial release.
great addition
thanks for this!
FYI, i added a public variable "imageOptions" to your code in PcLinkButton.php so that the following lines in the renderDataCellContent function change to
if (is_string($imageUrl)) { echo CHtml::link(CHtml::image($imageUrl, $label, $imgOptions), $url, $options); } else { echo CHtml::link($label, $url, $options); }
this way one can pass the width/height of the image to render
added fancybox
for those who might find this useful, I added fancybox functionality:
<!-- Add fancyBox --> <?php $baseUrl = Yii::app()->baseUrl; $cs = Yii::app()->getClientScript(); $cs->registerScriptFile($baseUrl.'/protected/components/fancybox/source/jquery.fancybox.pack.js?v=2.1.5'); $cs->registerCssFile($baseUrl.'/protected/components/fancybox/source/jquery.fancybox.css?v=2.1.5'); Yii::app()->clientScript->registerScript('img_click', " $('.fancybox').fancybox(); "); ?>
and the pclinkbutton column array:
array( 'class'=>'PcLinkButton', 'header'=>'Image', 'url' => 'Yii::app()->baseUrl."/images/plants/".$data->Filename', 'linkHtmlOptions' => array('class'=>'fancybox'), 'imageUrlExpression'=>'Yii::app()->baseUrl."/images/plants/".$data->Filename', 'labelExpression' => '$data->Caption', 'imageOptions' => array('width'=>'50px'), ),
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.