Image in gridview in yii2

2019-01-22 23:07发布

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?

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

8条回答
做个烂人
2楼-- · 2019-01-22 23:10

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-22 23:13

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.

查看更多
男人必须洒脱
4楼-- · 2019-01-22 23:14

Use this:

   [
        'attribute' => 'image',
        'format' => 'html',    
        'value' => function ($data) {
            return Html::img(Yii::getAlias('@web').'/images/'. $data['image'],
                ['width' => '70px']);
        },
    ],
查看更多
狗以群分
5楼-- · 2019-01-22 23:15

Yii 2 has built-in helper for building urls. You can bulld the url to image by path too (by passing second parameter $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);
}
查看更多
太酷不给撩
6楼-- · 2019-01-22 23:19

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']);
                                   },
                     ],
                    ],
                 ]);
        ?>
查看更多
贼婆χ
7楼-- · 2019-01-22 23:20

You can also use:

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

'@web' is a predefined path aliases.

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

查看更多
登录 后发表回答