unchanged
Title
Customize CGridView columns directly in your view
You usually take a model instance passed into your view to provide data to a
[CGridView](http://www.yiiframework.com/doc/api/1.1/CGridView "CGridVeiw
API") in this way:
~~~
[php]
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search() ,
));
~~~
Very often, you will want to customize a single column, while keeping the rest
untouched. There are many ways to accomplish this, but I find the following to
be the easiest one:
~~~
[php]
$columns = array_keys($model->metaData->columns);
$columns[array_search('column_name',
$columns)]$columns['column_name'] = array(
'name' => 'column_name',
'value' => ...
);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search() ,
'columns' => $columns
));
~~~
I have used the
[metaData](http://www.yiiframework.com/doc/api/1.1/CActiveRecord/#metaData-detail
"ActiveRecord::metaData property") ActiveRecord's property to get all
the model's attribute names and then customized a single attribute (i.e.
column_name), keeping the rest to the defaults, directly in the view, without
having to perform loops or implement methods in other classes.