Yii2.如何仅为 GridView 小部件中的特定列指定宽度


Yii2. How can I specify width only for the certain column in GridView widget?

我需要为特定列指定宽度(例如"file_name"(。我已经尝试了这些建议,但它对我不起作用。

可能的解决方案之一是使列可调整大小,但我也不知道该怎么做。

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'tableOptions'=>['class'=>'table-striped table-bordered table-condensed'],
    'options'=>['style' => 'white-space:nowrap;'],
    'columns' => [
        ['class' => 'yii'grid'SerialColumn'],
        'lastname',
        'firstname',
        'middlename',
        'rs_account',
        'sum',
        'file_name',
        'state',
        'history_id',
        [
            'label' => 'Code',
            'attribute' => 'codes.code',
        ],
        ['class' => 'yii'grid'ActionColumn'],
    ],
]); ?>

您应该对要设置样式的每个属性使用 contentOptions

<?= GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $searchModel,
  'tableOptions'=>['class'=>'table-striped table-bordered table-condensed'],
  'options'=>['style' => 'white-space:nowrap;'],
  'columns' => [
      ['class' => 'yii'grid'SerialColumn'],
      'lastname',
      'firstname',
      'middlename',
      'rs_account',
      'sum',
      ['attribute' => 'file_name',
         'label' =>'Filename'
         'contentOptions' => ['style' => 'width:680px;  min-width:600px;  '],
      ],
      'state',
      'history_id',
      [
          'label' => 'Code',
          'attribute' => 'codes.code',
      ],
      ['class' => 'yii'grid'ActionColumn'],
  ],
]); ?>

使用 headerOptions:

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