bettor
(Live Webscore)
February 7, 2010, 6:14pm
1
Hi,
I have some problems rendering an image in CGridView. I have the following code:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'Flag',
'type'=>'image',
'value'=>'CHtml::image("images/flags/".$data->country.".gif", "country")',
),
),
));
$data->country contains the name of a country from my DB. So I have added the path and the extension of the image. However, the above code renders the following html:
<img alt="" src="<img src="images/flags/England.gif" alt="country" />"/>
I have found a solution to the problem by either declaring
'type'=>'raw',
or
'type'=>'html',
…but would like to know how to use the image type as I would like to use the proper settings of the framework.
Is there a bug in the code that is supposed to render an image or is there a bug in my head that has missed something in the above code making it not work?
Cheers,
b
Edit: The code is supposed to render the flag of the respective country.
tri
(tri - Tommy Riboe)
February 7, 2010, 6:31pm
2
What about this?
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'Flag',
'type'=>'image',
'value'=>'images/flags/'.$data->country.'.gif',
),
),
));
Edit: this should work too
...
'value'=>array('images/flags/'.$data->country.'.gif', 'country'),
...
(not tested)
/Tommy
bettor
(Live Webscore)
February 7, 2010, 6:41pm
3
tri:
What about this?
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'Flag',
'type'=>'image',
'value'=>'images/flags/'.$data->country.'.gif',
),
),
));
Edit: this should work too
...
'value'=>array('images/flags/'.$data->country.'.gif', 'country'),
...
(not tested)
/Tommy
Hi Tommy,
your first code renders an empty <td/>
Your second suggestion returns:
include(images/flags/.gif.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory
Source File
/home/yourownb/framework/YiiBase.php(338)
00326: * @param string class name
00327: * @return boolean whether the class has been loaded successfully
…
00338: include($className.’.php’);
tri
(tri - Tommy Riboe)
February 7, 2010, 6:54pm
4
bettor:
Hi Tommy,
your first code renders an empty <td/>
Your second suggestion returns:
include(images/flags/.gif.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory
Source File
/home/yourownb/framework/YiiBase.php(338)
00326: * @param string class name
00327: * @return boolean whether the class has been loaded successfully
…
00338: include($className.’.php’);
Then I don’t know right now.
References I used:
http://www.yiiframework.com/doc/api/CGridView
http://www.yiiframework.com/doc/api/CDataColumn
http://www.yiiframework.com/doc/api/CFormatter
/Tommy
bettor
(Live Webscore)
February 7, 2010, 7:03pm
5
thanks for attempting to help anyway.
tri
(tri - Tommy Riboe)
February 8, 2010, 2:46am
6
It can be done with ‘type’=>‘image’, but CFormatter::formatImage() will only pass one parameter to CHtml::image(), i.e. only url can be specified since CHtml::image doesn’t decode an array (which I wrongly assumed).
This should work (url only):
...
'value'=>'"images/flags/'.$data->country.'.gif"',
...
Note the double quotes.
/Tommy
dungdt88
(Dtdung55)
February 8, 2010, 4:54am
7
I think it should be:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'Flag',
'type'=>'html',
'value'=>'CHtml::image("images/flags/".$data->country.".gif", "country")',
),
),
));
or:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'Flag',
'type'=>'image',
'value'=>'"images/flags/" . $data->country . ".gif"',
),
),
));
When "type" is "image", "value" must be link to the image.
bettor
(Live Webscore)
February 8, 2010, 11:29am
8
tri:
It can be done with ‘type’=>‘image’, but CFormatter::formatImage() will only pass one parameter to CHtml::image(), i.e. only url can be specified since CHtml::image doesn’t decode an array (which I wrongly assumed).
This should work (url only):
...
'value'=>'"images/flags/'.$data->country.'.gif"',
...
Note the double quotes.
/Tommy
This is not working either. It returns:
<img alt="" src="images/flags/.gif"/>
bettor
(Live Webscore)
February 8, 2010, 11:31am
9
I think it should be:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'Flag',
'type'=>'html',
'value'=>'CHtml::image("images/flags/".$data->country.".gif", "country")',
),
),
));
or:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'Flag',
'type'=>'image',
'value'=>'"images/flags/" . $data->country . ".gif"',
),
),
));
When "type" is "image", "value" must be link to the image.
This has worked excellent:
'value'=>'"images/flags/" . $data->country . ".gif"',
thanks. Looks like the issue was with the formatting as Tommy suggested same strategy but I guess his formatting was incorrect such as mine.
Cheers guys,
bettor
tri
(tri - Tommy Riboe)
February 8, 2010, 12:04pm
10
bettor:
This has worked excellent:
'value'=>'"images/flags/" . $data->country . ".gif"',
thanks. Looks like the issue was with the formatting as Tommy suggested same strategy but I guess his formatting was incorrect such as mine.
Cheers guys,
bettor
Yeah, I should have mentioned that the $data->country was untested. Late hours
Cheers
/Tommy
rudenich
(Rudenich)
May 2, 2010, 12:20pm
11
Any body now, how i can set the html attribute of image??
mikl
(Mike)
May 2, 2010, 2:37pm
12
You can’t with the current implementation. Use type “raw” instead and use ‘CHtml::image(…)’ as value to create your custom image tag.
sbasuki
(Sriyono Basuki)
January 14, 2011, 10:23am
13
Try this one : (I already used it)
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'loc_address',
array('name'=>'loc_picture',
'type'=>'html',
'header'=>'Picture',
'value'=> 'CHtml::link(CHtml::image($data->loc_picture, "image", array("width"=>30)), "/picture/noimage.jpg", array("class"=>"highslide"))'
),
),
));
nishathul
(Nishathul)
February 27, 2013, 12:02pm
15
Hi my uploaded file path is applcation/backups/status/
and gridview column array
array('name'=>'status_icon',
'type'=>'image',
'value'=>'"$data->path". $data->status_icon ',
),
i already set a attribute $path set in my model as
$this->path = Yii::app()->baseUrl.'/backup/status/';
and its working fine for me
roymj88
(Roymj88)
May 10, 2013, 9:27am
16
This should work well :
'value'=>function($data){
return 'Your html code';
},
tri
(tri - Tommy Riboe)
May 10, 2013, 9:46am
17
Yes, anonymous functons will work from PHP 5.3.
/Tommy
beingyii
(Faisalshaikh210)
March 25, 2014, 10:35am
18
How to design this CGridView to display image and related field text information??
what css have to design?? or can any image extension work fine for it??
help me please i am working on this images are displaying fine but i need fine design to display…