Yii2 GridView无法设置列宽 [英] Yii2 GridView cannot set column width

查看:397
本文介绍了Yii2 GridView无法设置列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用Yii2 GridView Widget。我已经尝试设置列的宽度后,在这里读取一些答案在stackoverflow,但它只是不会为我工作,我会喜欢它,如果列有不同的宽度(特别是第一个)。

this is the first time I am using the Yii2 GridView Widget. I have tried setting a column's width after reading some answers here on stackoverflow, but it just won't work for me and I would love it if the columns had different widths (especially the first one).

我试着直接使用'contentOptions'设置第一列的宽度,并且使用产品名称列的外部CSS文件设置宽度。

I have tried setting the width using 'contentOptions' directly for the first column and also with an external CSS file for the product name column.

有人可以告诉我,如果有替代方案或者我做错了什么?

Can someone please tell me if there is an alternative or if there is something I am doing wrong?

 <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        [
            'class' => 'yii\grid\SerialColumn',
            'contentOptions' => ['style' => 'max-width:20px;'],
        ],

        [
            'format' => 'raw', 
            'label' => 'Image',
            'value' => function ($data) { 
                $url = $data->imgSrc;
                return Html::img($url, ['width' => '40px']);
            },
        ],

        [
            'attribute' => 'name',
            'format' => 'raw',
            'label' => 'Product name',
            'contentOptions' => function ($model, $key, $index, $column) {
                return ['class' => 'tbl_name'];
            },
        ],
    ],
]); ?>


推荐答案

headerOptions:

If you want set width for single column you can use headerOptions:

[
  'attribute' => 'attribute_name',
   'headerOptions' => ['style' => 'width:20%'],
],

这篇关于Yii2 GridView无法设置列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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