php yii2 中 gridview 中的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21880511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:29:53  来源:igfitidea点击:

Image in gridview in yii2

phpgridviewyii2

提问by Dency G B

How to put an image in GridView in yii2?I have the following code. But its not displaying the image as it is not giving any image url. Where to put the image url?

如何在 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'],
    ],
]); ?>

回答by

Try like,

尝试像,

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

   ),

And in your model,

在你的模型中,

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

Don't know this is the right way or not.But this works for me.

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

回答by Insane Skull

Use this:

用这个:

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

回答by arogachev

Yii 2 has built-in helper for building urls. You can bulld the url to image by path too (by passing second parameter $scheme).

Yii 2 具有用于构建 url 的内置帮助程序。您也可以通过路径将 url 扩展为图像(通过传递第二个参数$scheme)。

So I recommend using this:

所以我建议使用这个:

GridView:

网格视图:

use yii\helpers\Url;

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

Model:

模型:

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

回答by Vishal Kumar

You can try this one:

你可以试试这个:

 <?= 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']);
                                   },
                     ],
                    ],
                 ]);
        ?>

回答by Preetha R

In views->image->index.php

在视图->图像->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'],

    ]); ?>

In model->image

在模型->图像中

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

table attributes are, image_id,image_path,caption1,caption2,status

表属性是,image_id,image_path,caption1,caption2,status

回答by msucil

I used to use 'raw' format for this type of work.

我曾经为这种类型的工作使用“原始”格式。

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

if image exists then image is displayed otherwise it is blank.

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

回答by Mohan Prasad

It is simple you can just declare it in your index file as below.

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

[
            '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);

            }
            ],

You will have to get image instance from your model. If required example please comment, will get back on that.

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

回答by Ziyodulloxon Ubaydullayev

<?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'],
    ],
]); ?>

回答by Gonzalo

You can also use:

您还可以使用:

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

'@web' is a predefined path aliases.

' @web' 是一个预定义的路径别名

'urlManager->CreateUrl()'do something more than resolving aliases.

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