Yii 1.1: Customize CGridView columns directly in your view

5 followers

You usually take a model instance passed into your view to provide data to a CGridView in this way:

$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:

$columns = array_keys($model->metaData->columns);
$columns[array_search('column_name', $columns)] = array(
    'name' => 'column_name',
    'value' => ...
);
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $model->search() ,
    'columns' => $columns
));

I have used the metaData 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.

Total 4 comments

#16789 report it
Ashish Gupta at 2014/03/27 02:53pm
Hello Friends I am new in Yii Please Help me on cgrid view

I am trying to display text instead of numeric numbers

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'admin-user-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'fname',
        'lname',
        'username',
        'password',
        'email',
        array('name'=>'status_id','value'=>CHtml::encode($model->getStatusText())),
        array('name'=>'pkg_id','value'=>CHtml::encode($model->getPackageText())),
        'active_date',
        'deactive_date',

        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

getPackageText() and getStatusText() functions are defined in model class. I am able to display numeric number which is stored in database but I want to display text for better user experience.If I am try to call these functions from cgridview its displaying blank instead of text. Please help me to achieve my requirement and suggest better solution. my model class functions.

public function getPackageText()
    {
        $user=Package::model()->findByAttributes(array('pkgid'=>$this->pkg_id));
        return $user['pkg_name'];
    }

    public function getStatusText()
    {
        $user=Status::model()->findByAttributes(array('statusid'=>$this->status_id));
        return $user['status_name'];
    }
#15557 report it
Fire at 2013/11/21 05:47am
this doesn't seam to work for CSqlDataProvider...

I tried doing this for a CSqlDataProvider, and always gives me Trying to get property of non-object

#9473 report it
clapas at 2012/08/14 10:13am
@imre

As you say, this is handy for models under change, like when the application is in development. Also, I have classes with many attributes, and I don't like to copy and paste the attributes from one place to another. Indeed, it can clutter the code when you write all the attributes in the widget params.

Thanks for commenting! Cheers

#9469 report it
Imre at 2012/08/14 09:20am
can see only niche use case for this.

I've always used:

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $model->search() ,
    'columns' => array(
        'col1',
        'col2',
        array(
            'name' => 'col3',
            'value' => '...',
        ),
    ),
));

I'd use your solution when i want it to be dynamic to model change. However you usually have also ID columns etc which you dont want to display.

Leave a comment

Please to leave your comment.

Write new article
  • Written by: clapas
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +3 / -2
  • Viewed: 21,098 times
  • Created on: Aug 14, 2012
  • Last updated: Aug 14, 2012
  • Tags: cgridview, columns