Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom column in CGridView in YII?

Tags:

yii

I need to add a column in CGridView.

I use this:

$this->widget('zii.widgets.grid.CGridView', array(
            'id'=>'user-grid',
            'dataProvider'=>$model->search(),
            'filter'=>$model,
            'pager' => array(
                'firstPageLabel' => '<<', 
                ),
            'columns'=>array(
                'username',
                'name',
                'email',
                'creationDate',
                array(
                        'class' => 'CButtonColumn',
                        'template' => '{change} {view}',
                        'buttons' => array(
                                        'change' => array(
                                                    'url'=> "'http://test.com/userservice/".$model->username."'",
                                        ),
                        ),

                ),
                array(
                    'name' => 'test',
                    'value' => 'testtest', 
                )
            ),
));

But I got error:

Property "User.test" is not defined.

like image 300
Chalist Avatar asked Dec 08 '22 18:12

Chalist


1 Answers

You're almost there, in your column array you would use the name param for attributes of the model in the dataprovider, instead for custom columns you can use header like so:

'columns'=>array(
    ...
    array(
        'header' => 'test',
        'value' => '"testtest"', 
    ),
    ...
)
like image 95
Stu Avatar answered Feb 12 '23 03:02

Stu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!