【问题标题】:Image in gridview in yii2yii2中gridview中的图像
【发布时间】:2014-02-19 12:28:53
【问题描述】:

yii2中如何在GridView中放一张图片?我有如下代码。但它没有显示图像,因为它没有提供任何图像 url。图片地址放在哪里?

 <?php echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'c_id',
        'name:ntext',
        'description:ntext',
        array(
                'format' => 'image',
               'attribute'=>'logo',

),

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

【问题讨论】:

  • 单元格中的html输出是什么?

标签: php gridview yii2


【解决方案1】:

试试看,

 array(
'format' => 'image',
'value'=>function($data) { return $data->imageurl; },

   ),

在你的模型中,

public function getImageurl()
{
return \Yii::$app->request->BaseUrl.'/<path to image>/'.$this->logo;
}

不知道这是否正确。但这对我有用。

【讨论】:

  • 这个 getImageurl() 就像魔术一样。但我认为现在我们更频繁地使用@Insane Skull 的答案..
【解决方案2】:

使用这个:

   [
        'attribute' => 'image',
        'format' => 'html',    
        'value' => function ($data) {
            return Html::img(Yii::getAlias('@web').'/images/'. $data['image'],
                ['width' => '70px']);
        },
    ],

【讨论】:

  • 您也可以使用 columnLabel 来设置列的标签。
【解决方案3】:

Yii 2 具有用于构建 url 的内置助手。您也可以通过路径将网址添加到图像中(通过传递第二个参数$scheme)。

所以我推荐使用这个:

GridView:

use yii\helpers\Url;

[
    'format' => 'image',
    'value' => function ($model) {
        return $model->getImageUrl(); 
    },
],

型号:

public function getImageUrl()
{
    return Url::to('@web/path/to/logo/' . $this->logo, true);
}

【讨论】:

  • 如何设置图片的宽度?
  • 您可以为此创建单独的问题,因为当前问题已解决。
  • 我评论了这个question
【解决方案4】:

你可以试试这个:

 <?= GridView::widget
                ([
                    'dataProvider' => $dataProvider,
                     'filterModel' => $searchdata,
        'columns' => [
                      [
                         'attribute' => 'logo',
                         'format' => 'html',
                         'label' => 'Image',
                         'value' => function ($data) {
                                    return Html::img('/advanced/hello/frontend/web/image/' . $data['logo'],
                                    ['width' => '80px',
                                     'height' => '80px']);
                                   },
                     ],
                    ],
                 ]);
        ?>

【讨论】:

    【解决方案5】:

    在视图->图像->index.php

    <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
    
            'columns' => [
                ['class' => 'yii\grid\SerialColumn'],
                 [
                 'attribute'=>'image_path',
                'label'=>'Image',
                'format'=>'html',
                'content' => function($data){
                    $url = $data->getParentName();
                    return Html::img($url, ['alt'=>'yii','width'=>'250','height'=>'100']);
                            }
                ],
                'caption1',
                'caption2',
                'status',
                ['class' => 'yii\grid\ActionColumn'],
            ],
            'tableOptions' =>['class' => 'table table-striped table-bordered'],
    
        ]); ?>
    

    在模型中->图像

     public function getParent()
        {
            return $this->hasOne(Image::className(), ['image_id' => 'image_id']);
        }
        public function getParentName()
        {
            $model=$this->parent;
            return $model?$model->image_path:'';
        }
    

    表属性是, image_id,image_path,caption1,caption2,状态

    【讨论】:

      【解决方案6】:

      我曾经在这类工作中使用“原始”格式。

      [
          "attribute": "image",
          "format": "raw",
          "value": function($model){
              return ($model->image) ? Html::img("/path-to-img-location" . $model->image) : false;
          }
      ]
      

      如果图像存在,则显示图像,否则为空白。

      【讨论】:

        【解决方案7】:

        你也可以使用:

        public function getImageurl()
        {
          return \Yii::$app->urlManager->createUrl('@web/path/to/logo/'.$this->logo);
        }
        

        '@web' 是 predefined path aliases。

        'urlManager->CreateUrl()' 做的不仅仅是解析别名。

        【讨论】:

          【解决方案8】:

          很简单,您可以在索引文件中声明它,如下所示。

          [
                      'label' => Your Label Here',  
                      'format' => 'raw',   
                      'value' => function ($data) {
                           $images = '';
          
                          $images = $images.Html::img(\Yii::$app->request->BaseUrl.'/your-path-here/'.$date->getImagefilename(),['alt'=>'','width'=>'30','height'=>'30', 'data-toggle'=>'tooltip','data-placement'=>'left','title' => $name->pictogram_comment ,'style'=>'cursor:default;']);
                          return ($images);
          
                      }
                      ],
          

          您必须从模型中获取图像实例。如果需要示例,请发表评论,我们会回复。

          【讨论】:

            【解决方案9】:
            <?php echo GridView::widget([
                'dataProvider' => $dataProvider,
                'filterModel' => $searchModel,
                'columns' => [
                    ['class' => 'yii\grid\SerialColumn'],
            
                    'c_id',
                    'name:ntext',
                    'description:ntext',
                    'logo:image',
            
                    ['class' => 'yii\grid\ActionColumn'],
                ],
            ]); ?>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-05-24
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-10-07
              • 1970-01-01
              相关资源
              最近更新 更多