Yii2在gridview行中显示多个图像 [英] Yii2 display multiple images in gridview row

查看:143
本文介绍了Yii2在gridview行中显示多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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张图片。



我的代码如下:



()

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();
}

我的索引文件网格视图图像代码 p>

  [
'label'=> '危险',
'format'=> 'raw',
'value'=>函数($ data){
返回Html :: img($ data-> getPictogramUrl(),['alt'=>'myImage','width'=>'20','height'= > '30' ]);
},
],

我也试着添加一个引导工具提示此..工具提示显示成功,但我认为循环不是以正确的方式完成的,所以它重复我的图像。



这里是我更新的gridview代码。

  [
'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;
返回$图片;
},
],


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 my_id as follows: 1, 2, 3, 4, 5, 6.

In Table B i have my_id as follows. 1 ,1 ,1 ,2 ,3, 3.

In Table B i also have c_id as follows. 1, 2, 3, 4, 5, 6.

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_id because of the foreign key constraints. but it displays only 1 image.

My code is below:

In my model

 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()  ;
}

my index file grid view image code

 [
        'label' => 'Hazards',  
        'format' => 'raw',   
        'value' => function ($data) {
                return Html::img($data->getPictogramUrl(), ['alt'=>'myImage','width'=>'20','height'=>'30']); 
            },
        ],   

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.

 [
        '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;

        }
        ],

解决方案

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

 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
}

Now in you view loop over all urls and append images with each url

[
    '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;
    },
],

这篇关于Yii2在gridview行中显示多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆