在yii2中的gridview中的图像

Den*_*G B 9 php gridview yii2

如何在yii2中将图像放入GridView?我有以下代码.但它没有显示图像,因为它没有给出任何图像网址.在哪里放图像网址?

 <?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'],
    ],
]); ?>
Run Code Online (Sandbox Code Playgroud)

小智 20

试试吧,

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

   ),
Run Code Online (Sandbox Code Playgroud)

在你的模型中,

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

不知道这是正确的方法.但这对我有用.


Ins*_*ull 11

用这个:

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