Yii2在gridview行中显示多个图像 [英] Yii2 display multiple images in gridview row
问题描述
我想在gridviews单行中显示多个图像。例如:我有表A,表B和表C.
表A有my_id。
在表B中,my_id是外键。与my_id一起,它有c_id。
表C具有表B中引用的c_id。
表C还有一个文件路径来显示图像。
在表A 中,我有 my_id
如下:
1,2,3,4,5,6。
在表B中, em>我有 my_id
,如下所示。
1,1,1,2,3,3。
在表B中,我还有 c_id
如下。
1,2,3,4,5,6。
在表C中,我的c_id是:
1,2,3,4,5 5,6.这些id的文件路径与每个文件路径相关联。他们是不同的图像。
现在我的gridview应该为 my_id
显示3个不同的图像,因为外键约束。但它只显示1张图片。
我的代码如下:
() 我的索引文件网格视图图像代码 p> 我也试着添加一个引导工具提示此..工具提示显示成功,但我认为循环不是以正确的方式完成的,所以它重复我的图像。 这里是我更新的gridview代码。 模型和网格视图中的逻辑错误很少。在所有这些领域,您处理的是一件商品而不是三件商品。 在您的模特中 现在,您可以查看遍历所有网址并追加图片每个网址 I want to display multiple images in a gridviews single row. For example: I have table A, Table B and table C. Table A has my_id. In Table B my_id is the foreign key. Along with my_id it has c_id. Table C has c_id which is in reference in Table B. Table C also has a filepath to display images. in Table A i have In Table B i have In Table B i also have In table C my c_id's are:
1, 2, 3, 4, 5, 6. and these id's have filepath associated with each of them. They are different images. Now my gridview should display 3 different images for My code is below: In my model my index file grid view image code I am also trying to add a bootstrap tool tip to this.. tool tip is displaying successfully but I think the looping is not not done in a correct way so it is repeating my images. here is my updated gridview code.
You have few logical errors in model and grid view. In all these areas you are dealing with one item instead of three. In your model Now in you view loop over all urls and append images with each url
这篇关于Yii2在gridview行中显示多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
pre $ $ $ $ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ >其中(['sdsref_id'=> $ this-> sdsref_id]) - > all();
foreach($ pictogramsID as $ picID){
return $ picID-> pictogram_id;
$ b public function getPictogramPath()
{
$ pictogramsID = GhsPictogram :: find() - >其中(['pictogram_id'=> $ this-> getPictogramsID()]) - > all();
foreach($ pictogramsID as $ picID){
$ pic = $ picID-> pictogram_filepath;
}
return $ pic;
$ b public function getPictogramUrl()
{
// var_dump($ this- > getPictogramPath());出口();
return \Yii :: $ app-> request-> BaseUrl。'/ web'。$ this-> getPictogramPath();
}
[
'label'=> '危险',
'format'=> 'raw',
'value'=>函数($ data){
返回Html :: img($ data-> getPictogramUrl(),['alt'=>'myImage','width'=>'20','height'= > '30' ]);
},
],
[
'label'=> '危险',
'format'=> 'raw',
'value'=>函数($ data){
$ images ='';
//追加所有图片
foreach($ data-> getPictogramName()as $ name)
foreach($ data-> getPictogramUrl()as $ url)
$ images = $ images.Html :: img($ url,['alt'=>'','width'=> '30','height'=> '30','data- toggle'=>'tooltip','data-placement'=>'left','title'=> $ name,'style'=>'cursor:default;']);
返回$图片;
$ b],
public function getPictogramsID()
{
$ ids = [];
$ pictogramsID = SdsrefGhsPictograms :: find() - >其中(['sdsref_id'=> $ this-> sdsref_id]) - > all();
foreach($ pictogramsID as $ picID){
$ ids [] = $ picID-> pictogram_id;
}
return $ ids; //返回所有三个id
}
public function getPictogramPath()
{
$ pic = [];
$ pictogramsID = GhsPictogram :: find() - >其中(['pictogram_id'=> $ this-> getPictogramsID()]) - > all();
foreach($ pictogramsID as $ picID){
$ pic [] = $ picID-> pictogram_filepath;
}
return $ pic;
$ b public function getPictogramUrl()
{
$ url = [];
foreach($ this-> getPictogramPath()as $ path):
$ url [] = \Yii :: $ app-> request-> BaseUrl。'/ web'。$路径;
endforeach;
返回$ url; //返回地址
}
[
'label'=> '危险',
'format'=> 'raw',
'value'=>函数($ data){
$ images ='';
//追加所有图片
foreach($ data-> getPictogramUrl()as $ url):
$ images = $ images.Html :: img($ url,['alt' => 'MYIMAGE', '宽度'=> '20' , '身高'=> '30' ]);
endforach;
返回$图片;
},
],
my_id
as follows:
1, 2, 3, 4, 5, 6.my_id
as follows.
1 ,1 ,1 ,2 ,3, 3.c_id
as follows.
1, 2, 3, 4, 5, 6.my_id
because of the foreign key constraints. but it displays only 1 image. public function getPictogramsID()
{
$pictogramsID = SdsrefGhsPictograms::find()->where(['sdsref_id' => $this->sdsref_id])->all();
foreach ($pictogramsID as $picID){
return $picID->pictogram_id;
}
}
public function getPictogramPath()
{
$pictogramsID = GhsPictogram::find()->where(['pictogram_id' => $this->getPictogramsID()])->all();
foreach ($pictogramsID as $picID){
$pic = $picID->pictogram_filepath;
}
return $pic;
}
public function getPictogramUrl()
{
//var_dump($this->getPictogramPath()); exit();
return \Yii::$app->request->BaseUrl.'/web'.$this->getPictogramPath() ;
}
[
'label' => 'Hazards',
'format' => 'raw',
'value' => function ($data) {
return Html::img($data->getPictogramUrl(), ['alt'=>'myImage','width'=>'20','height'=>'30']);
},
],
[
'label' => 'Hazards',
'format' => 'raw',
'value' => function ($data) {
$images = '';
// append all images
foreach($data->getPictogramName() as $name)
foreach ($data->getPictogramUrl() as $url)
$images = $images.Html::img($url,['alt'=>'','width'=>'30','height'=>'30', 'data-toggle'=>'tooltip','data-placement'=>'left','title' => $name ,'style'=>'cursor:default;']);
return $images;
}
],
public function getPictogramsID()
{
$ids = [];
$pictogramsID = SdsrefGhsPictograms::find()->where(['sdsref_id' => $this->sdsref_id])->all();
foreach ($pictogramsID as $picID){
$ids[] = $picID->pictogram_id;
}
return $ids;// returning all three ids
}
public function getPictogramPath()
{
$pic = [];
$pictogramsID = GhsPictogram::find()->where(['pictogram_id' => $this->getPictogramsID()])->all();
foreach ($pictogramsID as $picID){
$pic[] = $picID->pictogram_filepath;
}
return $pic;
}
public function getPictogramUrl()
{
$url = [];
foreach($this->getPictogramPath() as $path):
$url[] = \Yii::$app->request->BaseUrl.'/web'.$path;
endforeach;
return $url; // returning al urls
}
[
'label' => 'Hazards',
'format' => 'raw',
'value' => function ($data) {
$images = '';
// append all images
foreach($data->getPictogramUrl() as $url):
$images = $images.Html::img($url, ['alt'=>'myImage','width'=>'20','height'=>'30']);
endforach;
return $images;
},
],