我想在管理面板中显示图片而不是图片名称。但我在yii 1.1中有很多例子,但在yii 2.0中没有理解使用。 这是我的网格视图
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\imagine\Image;
/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('app', 'Contents');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="content-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a(Yii::t('app', 'Create Content'), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'label',
'title',
'content',
/* ['attribute' => 'main_image',
'header' => 'Image',
'value' => function( $data ) {
return ?><img src="<?= Yii::$app->request->baseUrl ?>/upload/content/<?php echo $data->main_image?>">
<?php
}
],*/
[
'attribute'=>'photo',
'value' => Html::a(Html::img(Yii::getAlias('@web').'/upload/content'.$content->main_image, ['alt'=>'some', 'class'=>'thing', 'height'=>'100px', 'width'=>'100px']), ['site/zoom']),
'format' => ['raw'],
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
我收到了未定义内容的错误,我也尝试了上面的注释代码。但它在提交的列表中显示“未设置”。
如果对此有任何想法有帮助......
答案 0 :(得分:1)
您应该以这种方式格式化您的属性(希望路径正确)
['attribute' => 'main_image',
'header' => 'Image',
'format' => 'raw',
'value' => function( $model ) {
return '<img src="' . echo Yii::$app->request->baseUrl . '/upload/content/'. $model->main_image .'">' ;
}
],
答案 1 :(得分:0)
我使用此代码获得解决方案
['attribute' => 'main_image',
'header' => 'Image',
'format' => ['raw'],
'value' => function( $data ) {
return Html::img(Yii::getAlias('@web').'/upload/content/'.$data->main_image, ['alt'=>'some', 'class'=>'thing', 'height'=>'100px', 'width'=>'100px']);
}
],