Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the Row Color Based on the Column value in CGridView

Tags:

yii

In Yii, CGridView has it's own background color in the row. But what I want to do is highlight particular row based on the value of one of the column.

For Instance, I have three column, id, name, status. Now, If the Value of status is Inactive or 0, I should highlight the row with some color.

I read the class reference briefly and searched this site as well. But could not find the relevant solution. If some example or some direction toward the right solution, that would be much appreciated.

Thanks, Ujjwal

like image 345
Ujjwal Prajapati Avatar asked Jun 18 '12 05:06

Ujjwal Prajapati


2 Answers

CGridView 'rowCssClassExpression' is the way to get what you want.

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'rowCssClassExpression'=>'($data->myFlag==0)?"normal":"especial"',
    'columns'=>array(
    ...
    ),
));

You can also call a custom php function, and pass the $data variable to it. That function should return the class name for the given row :)

like image 122
sucotronic Avatar answered Nov 14 '22 20:11

sucotronic


Use rowCssClass and rowCssClassExpression for your functionality. I did not tested this code but the trick you can use to get your solution.

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'rowCssClass'=>array('odd','even'),
    'rowCssClassExpression'=>($data->status==0)?even:odd,
    'columns'=>array(
    ),
));
like image 1
Onkar Janwa Avatar answered Nov 14 '22 21:11

Onkar Janwa