【问题标题】:Yii2. How can I specify width only for the certain column in GridView widget?Yii2.如何仅为 GridView 小部件中的特定列指定宽度?
【发布时间】:2016-02-15 14:59:39
【问题描述】:

我需要指定特定列的宽度(例如“文件名”)。我已经尝试过these 的建议,但它对我不起作用。

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

<?= 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'],
    ],
]); ?>

【问题讨论】:

  • @Kevin 想知道您是否解决了问题
  • @Kevin,不,很遗憾。现在我无法访问源代码,也无法测试其他可能的解决方案。

标签: php gridview yii2


【解决方案1】:

您应该为要设置样式的每个属性使用 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'],
  ],
]); ?>

【讨论】:

  • 我已经多次测试了这段代码..你有错误吗.? .结果不是你需要的。 ?请解释你的意思是不工作..
  • 列的宽度等于里面的内容。当我编写您的代码时,将列宽指定为 30px 并刷新页面,没有任何变化。
  • 它也不起作用。我不知道为什么。如果我更改 tableOptions 它正在工作。但是应用于特定字段的更改只是被忽略了。感谢您的关心!
  • 仅用于测试...尝试在 tableOptions 中不使用 table-condensed
  • 你可以尝试最小宽度或最大宽度
【解决方案2】:

使用 headerOptions:

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

【讨论】:

    猜你喜欢
    • 2021-11-05
    • 2016-07-14
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 2020-03-18
    • 2021-05-19
    相关资源
    最近更新 更多