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

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

问题描述

我想在 gridviews 的单行中显示多个图像.例如:我有表 A、表 B 和表 C.

<块引用>

表 A 有 my_id.

在表 B 中 my_id 是外键.与 my_id 一起,它还有 c_id.

表 C 有 c_id,在表 B 中引用.

表 C 也有一个文件路径来显示图像.

表 A 中,我有 my_id 如下:1、2、3、4、5、6.

表 B 中,我有 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, 6. 并且这些 id 具有与它们中的每一个相关联的文件路径.它们是不同的图像.

由于外键约束,现在我的 gridview 应该为 my_id 显示 3 个不同的图像.但它只显示 1 张图片.

我的代码如下:

在我的模型中

 公共函数 getPictogramsID(){$pictogramsID = SdsrefGhsPictograms::find()->where(['sdsref_id' => $this->sdsref_id])->all();foreach ($pictogramsID 作为 $picID){返回 $picID->pictogram_id;}}公共函数 getPictogramPath(){$pictogramsID = GhsPictogram::find()->where(['pictogram_id' => $this->getPictogramsID()])->all();foreach ($pictogramsID 作为 $picID){$pic = $picID->pictogram_filepath;}返回 $pic;}公共函数 getPictogramUrl(){//var_dump($this->getPictogramPath());出口();返回 Yii::$app->request->BaseUrl.'/web'.$this->getPictogramPath() ;}

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

<代码> ['标签' =>'危害','格式' =>'生的','价值' =>功能($数据){return Html::img($data->getPictogramUrl(), ['alt'=>'myImage','width'=>'20','height'=>'30']);},],

我也在尝试为此添加引导工具提示.工具提示显示成功,但我认为循环没有以正确的方式完成,因此它重复了我的图像.

这是我更新的 gridview 代码.

<代码> ['标签' =>'危害','格式' =>'生的','价值' =>功能($数据){$图片 = '';//附加所有图像foreach($data->getPictogramName() as $name)foreach ($data->getPictogramUrl() 作为 $url)$images = $images.Html::img($url,['alt'=>'','width'=>'30','height'=>'30','data-toggle'=>'tooltip','data-placement'=>'left','title' => $name ,'style'=>'cursor:default;']);返回 $images;}],

解决方案

您在模型和网格视图中几乎没有逻辑错误.在所有这些领域中,您都在处理一项而不是三项.

在你的模型中

 公共函数 getPictogramsID(){$ids = [];$pictogramsID = SdsrefGhsPictograms::find()->where(['sdsref_id' => $this->sdsref_id])->all();foreach ($pictogramsID 作为 $picID){$ids[] = $picID->pictogram_id;}return $ids;//返回所有三个 id}公共函数 getPictogramPath(){$pic = [];$pictogramsID = GhsPictogram::find()->where(['pictogram_id' => $this->getPictogramsID()])->all();foreach ($pictogramsID 作为 $picID){$pic[] = $picID->pictogram_filepath;}返回 $pic;}公共函数 getPictogramUrl(){$url = [];foreach($this->getPictogramPath() as $path):$url[] = Yii::$app->request->BaseUrl.'/web'.$path;Endforeach;返回 $url;//返回所有 url}

现在,您可以查看所有 url 的循环并为每个 url 附加图像

<预><代码>['标签' =>'危害','格式' =>'生的','价值' =>功能($数据){$图片 = '';//附加所有图像foreach($data->getPictogramUrl() as $url):$images = $images.Html::img($url, ['alt'=>'myImage','width'=>'20','height'=>'30']);端叉;返回 $images;},],

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天全站免登陆