CGridView - add own attribute from PHP


    $this->widget('zii.widgets.grid.CGridView', array(

        'dataProvider'=>$dataProvider,

        'columns'=>array(

            'title',          // display the 'title' attribute

            'category.name',  // display the 'name' attribute of the 'category' relation

            'content:html',   // display the 'content' attribute as purified HTML

            array(            // display 'create_time' using an expression

                'name'=>'create_time',

                'value'=>'date("M j, Y", $data->create_time)',

            ),

            array(            // display 'author.username' using an expression

                'name'=>'authorName',

                'value'=>'$data->author->username',

    //HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                  'htmlOptions'=>array('class'=>'$data->author->username', 'secondAttribute' => $data->author->id),

    //HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

            ),

            array(            // display a column with "view", "update" and "delete" buttons

                'class'=>'CButtonColumn',

            ),

        ),

    ));

In option value i can add variable from PHP, but for option htmlOptions this is not possible. Why? How can i make attribute with PHP variable?

Don’t use single quote:

‘htmlOptions’=>array(‘class’=>$data->author->username, ‘secondAttribute’ => $data->author->id)

this won’t work. $data must be evaluated for every row…

Look at this wiki article: http://www.yiiframework.com/wiki/314/cgridview-use-special-variable-data-in-the-htmloptions-of-a-column-i-e-evaluate-htmloptions-attribute/

@redguy very thanks :)