【问题标题】:How to add an image in CGridview如何在 CGridview 中添加图像
【发布时间】:2014-01-02 08:45:04
【问题描述】:

这是我对表格的gridview的索引:

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tblvehicleimagegrid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(

    'serial_no',
    array(
      'name'  =>'image',
        'type'=>'raw',
        'value'=> CHtml::image(Yii::getPathOfAlias('webroot')."/images/originals/images.jpg"),

    ),

    'line_no',
..........
.........

出现错误

在这方面需要一些帮助。

【问题讨论】:

    标签: gridview yii


    【解决方案1】:

    看看this page

    如果你像 bettor 在 Yii 论坛上那样编写代码:

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

    然后你会得到这个:

    <img alt="" src="<img src="images/flags/England.gif" alt="country" />"/>
    

    注意不兼容的元素?

    换句话说,您需要将列声明中的“类型”设置为“html”,以便 CGridView 知道要查找 HTML,这就是 CHTML::image 将为您提供的内容。

    编辑:

    我知道 Yii 文档可能有点不清楚,但是如果您查看代码,它通常会很有意义。在这种情况下,CHTML::image() 的文档确实告诉我们它返回了一个图像标签,具体来说。

    【讨论】:

    • hm...我完全删除了 'CHtml::image' 部分;它现在正在工作。不过,感谢指导方针..只有当我知道如何使用 CHtml::image 解决它时;这将是一个补充。就像@naveen goyal 提到的那样,在行周围添加单引号也很重要
    【解决方案2】:

    像这样在值中添加单引号...

    'value'=> 
    'CHtml::image(Yii::getPathOfAlias("webroot")."/images/originals/images.jpg")',
    

    【讨论】:

      猜你喜欢
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 2021-07-21
      • 2012-07-12
      • 2012-04-25
      • 2012-04-17
      相关资源
      最近更新 更多