php 如何在 YII 中更改 GridView 列大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20799869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change GridView column size in YII?
提问by iProgrammer
Im trying change my GridView column width. This is my code:
我正在尝试更改我的 GridView 列宽。这是我的代码:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'prefixs-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name' => 'id',
'header' => 'No.',
'htmlOptions' => array('class' => 'center'),
),
array(
'name' => 'pfx',
'htmlOptions' => array('width' => 30),
),
array(
'class' => 'CButtonColumn',
),
),
)); ?>
When i run 'pfx' columns width must be changed to 30, but width is same. How i can change width of column? Any ideas?
当我运行 'pfx' 列宽度必须更改为 30,但宽度相同。如何更改列的宽度?有任何想法吗?
回答by rodrigovr
If you do not want to use a separated CSS definition you should do:
如果您不想使用单独的 CSS 定义,您应该这样做:
'htmlOptions' => array('style' => 'width: 30px;'),
'filterHtmlOptions' => array('style' => 'width: 30px;'),
回答by bhargav katakpara
'columns' => [
[
'headerOptions' => ['width' => '150'],
'attribute' => 'name',
'format' => 'raw',
'value' => 'name'
],
]
回答by Dzhuneyt
There are multiple ways to approach this issue.
有多种方法可以解决这个问题。
You can widen the cell's contents (cells of individual rows):
您可以加宽单元格的内容(单个行的单元格):
$columns[] = array(
'header'=>'Name',
'value'=>'$data["name"]',
'htmlOptions'=>array('style'=>'width: 150px')
);
Sometimes you may want to widen the column header instead of the individual rows' cells:
有时您可能想要加宽列标题而不是单个行的单元格:
$columns[] = array(
'header'=>'Name',
'value'=>'$data["name"]',
'headerHtmlOptions'=>array('style'=>'width: 150px')
);
Widening the header is the preferred option, but even if you do that, sometimes it doesn't work as expected.
加宽标题是首选选项,但即使您这样做,有时也不会按预期工作。
If this is the case, you can go my personal preferred way and widen the contentsof the header cell. The widening will be inherited by the rows below it as well.
如果是这种情况,您可以按照我个人喜欢的方式扩大标题单元格的内容。加宽也将被其下方的行继承。
$columns[] = array(
'header'=>'<div style="width:150px;">Name</div>',
'value'=>'$data["name"]',
);
回答by Paramone
Simply add
只需添加
'contentOptions' =>['style' => 'width:30px'],
'contentOptions' =>['style' => 'width:30px'],
-- For example:
- 例如:
'columns' => [
[
'attribute' => 'product.name',
'contentOptions' =>['style' => 'width:30px'],
],
That should do the trick!
这应该够了吧!
回答by adamS
i noticed with cgridview in the tags there is an id attribute id="table-name_c0" id="table-name_c1"etc. you could hook into this and the .filtersclass for the filter width. e.g.
我注意到标签中的 cgridview 有一个 id 属性id="table-name_c0" id="table-name_c1"等。你可以连接到这个和.filters过滤器宽度的类。例如
#table-name table-name_c0 {
width: 30px;
}
#table-name .filters:nth(0) {
width:30px;
}
where c0 and :nth(0) are the same number. it's slightly hacky but what you are trying to achieve is also slightly hacky imho
其中 c0 和 :nth(0) 是相同的数字。它有点老套,但您想要实现的目标也有点老套,恕我直言

