$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 know if this still applies or not (given that there is an accepted answer), but there is a slightly better solution in the form of "rowHtmlOptionsExpression". This specifies an expression that will be evaluated for every row. If the result of the eval() call is an array, it will be used as the htmlOptions for the <tr> tag. So you can basically now use something like this:
All your tags will have an id attribute with the records' PK. Just modify the jQuery slightly to obtain the id from that tag instead of an extra column and you should be set.
Extend the class CDataColumn
Under protected/components/ create the file DataColumn.php with the following content:
We can use this new class like this:
When you add arrays in the
columns
collection without specifying aclass
property, the type of column being created isCDataColumn
. The propertyCDataColumn::value
is explicitly documented to beTherefore,
value
has the special property that it getseval
'ed for each row and that's why you can set it "dynamically". This is an exception however, and almost nothing else supports the same functionality.However, you are in luck because the property
cssClassExpression
is another special exception that covers exactly this usage case. So you can do it like this:Edit: I made a mistake while copy/pasting from your example and did not notice that you were trying to do the same thing for additional attributes inside
htmlOptions
(I have now deleted the relevant part of the code).If you need to add more options for dynamic values you have no choice but to subclass
CDataColumn
and override therenderDataCell
method (stock implementation is here).