Gridvie display image xolumn

Hi all, i want to display an image in my gridview

I try in this simple way and it works:


        <?= GridView::widget([

            'dataProvider' => $dataProvider,

            'filterModel' => $searchModel,

            'columns' => [

                ['class' => 'yii\grid\SerialColumn'],


                'id',

                [

                    'format' => 'image',

                    'value'=>function($data) { return $data->file; },

                ],


            ],

        ]); ?>



Now i want to specify html options for my image, in particular the class and the width

and i do the follow code reading yii\grid\Column documentation,without success:


             [

                    'format' => 'image',

                    'value'=>function($data) { return $data->file; },

                    'options'=>['class'=>'img-responsive','width'=>'100px']

                    ],



with this code the image doesn’t affect with my htmloptions.

So i try in another way:


            [

                'format' => 'raw',

                'value'=>function($data) { return Html::img($data->file,['style'=>['width'=>'100px'],'class'=>'img-responsive']); },

            ],

and now it works.

My question is, what’s wrong in my first attempt? What is the difference between these two methods?

Thank’s for any suggestion.

The first example sets the styles for the whole column group, not the individual images.

Like Patrick said the options are the tag options for the cell not the content. You can reference the DataColumn docs to see all the available options for a gridview data column.

Also, if you notice the grid widget format option uses i18n formatters. With that said if you look at the asImage() function (what format->image calls)




public function asImage($value, $options = []) {

    if ($value === null) {

        return $this->nullDisplay;

    }

    return Html::img($value, $options);

}



that renders the image in the dataColumn it sould be in “[image,[options]]” format. The proper way to achieve what you are going for using Yii’s built in function would be


 [

    'attribute' => 'file',

    'format' => ['image', ['width' => '100px', 'class' => 'img-responsive']]

]

Using above would output exactly the same thing as your second way of doing it. But that’s the whole reason why the first way doesn’t work.