如何在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'],
],
]); ?>
答案 0 :(得分:20)
尝试,
array(
'format' => 'image',
'value'=>function($data) { return $data->imageurl; },
),
在你的模特中,
public function getImageurl()
{
return \Yii::$app->request->BaseUrl.'/<path to image>/'.$this->logo;
}
不知道这是正确的方法。但这对我有用。
答案 1 :(得分:11)
使用此:
[
'attribute' => 'image',
'format' => 'html',
'value' => function ($data) {
return Html::img(Yii::getAlias('@web').'/images/'. $data['image'],
['width' => '70px']);
},
],
答案 2 :(得分:2)
Yii 2内置帮助程序,用于构建网址。您也可以通过路径将网址压缩为图像(通过传递第二个参数$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);
}
答案 3 :(得分:2)
你可以尝试这个:
<?= 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']);
},
],
],
]);
?>
答案 4 :(得分:1)
在views-&gt; image-&gt; 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'],
]); ?>
在模型 - &gt;图像
中 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的,状态
答案 5 :(得分:1)
我曾经使用'raw'格式进行此类工作。
ArrayList<String> ar = new ArrayList();
ar.add("UserId"); //adding item to arraylist
ar.add("Directory");
ar.add("Username");
ar.add("PhoneNumber");
// approach one using iteration
for (Iterator<String> it = ar.iterator(); it.hasNext(); ) {
String f = it.next();
if (f.equals("UserId"))
System.out.println("UserId found");
}
// approach two using a colletion which supports fetching element by key
Map<String,String> myMap=new HashMap<>();
for(String strg:ar){
myMap.put(str,str);
}
String result=myMap.get("UserId");
如果图像存在,则显示图像,否则为空白。
答案 6 :(得分:0)
您也可以使用:
public function getImageurl()
{
return \Yii::$app->urlManager->createUrl('@web/path/to/logo/'.$this->logo);
}
' @web '是predefined path aliases。
'urlManager->CreateUrl()'做的不仅仅是解决别名。
答案 7 :(得分:0)
很简单,你可以在索引文件中声明它,如下所示。
[
'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);
}
],
您必须从模型中获取图像实例。如果需要示例请评论,将重新开始。
答案 8 :(得分:0)
<?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'],
],
]); ?>