如何在YII中修复CGRIDVIEW的列大小?

时间:2014-05-27 10:55:30

标签: php gridview yii dataprovider cgridview

我想修复GridView的列宽。当我使用GridView时,根据数据调整列的宽度...我的目标是为每个列设置固定宽度,例如:for" ID"我想要宽度= 10px; for" Name"列应该是width = 50px ...和soo on

视图

if(!empty($selected_columns))
    {
        $this->widget('bootstrap.widgets.TbGridView', array(
            'type' => 'bordered striped',
            'id' => 'bike_search_grid',   
            'dataProvider' => $model->search_bike(),
            'ajaxUpdate' => true, //false if you want to reload aentire page (useful if sorting has an effect to other widgets)
            'filter' => null, 
            'template'=>'<div style="overflow:auto;">{items}</div>{pager}{summary}',
            'columns' => $selected_columns,
            'enablePagination' => true
        ));
    }

EDITED :::

模型是

$selected_columns = array();
$selected_columns[] = array(
                'header' => 'ID',
                'name' => 'bike_id',
                'value'=>'$data["bike_id"]', 
                'htmlOptions'=>array('width'=>'30'),
                'headerHtmlOptions'=>array('width'=>'30')
            );

2 个答案:

答案 0 :(得分:0)

$ selected_colums的格式不正确。试试这个:

$selected_columns[] = array(
           array(
                'header' => 'ID',
                'name' => 'bike_id',
                'value'=>'$data["bike_id"]', 
                'htmlOptions'=>array('width'=>'10'),
                'headerHtmlOptions'=>array('width'=>'10')
                ),
            );

每个要显示的列都应以数组形式提供(&#39;键&#39; =&gt;&#39;值&#39;)格式

答案 1 :(得分:0)

解决方案:=&gt;

查看:

$this->widget('bootstrap.widgets.TbGridView', array(
            'type' => 'bordered striped',
            'id' => 'bike_search_grid',   
            'dataProvider' => $model->search_bike(),
            'ajaxUpdate' => true, //false if you want to reload aentire page (useful if sorting has an effect to other widgets)
            'filter' => null, 
            'template'=>'<div style="overflow:auto;">{items}</div>{pager}{summary}',
            'columns' => $selected_columns,
            'enablePagination' => true
        ));

MODEL:

  $selected_columns[] = array(
                    'header' => 'ID',
                    'name' => 'bike_id',
                    'type'=>'html',
                    'htmlOptions'=>array('style'=>'word-wrap: break-word;'),
                    'headerHtmlOptions'=>array('style'=>'text-align:center;'),
                    'value'=> '"<div style=\"width:90px;\">" . $data["bike_id"] . "</div>"',
                                  );