Change the Row Color Based on the Column value in

2019-03-26 06:55发布

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

标签: yii
2条回答
ら.Afraid
2楼-- · 2019-03-26 07:38

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(
    ),
));
查看更多
霸刀☆藐视天下
3楼-- · 2019-03-26 07:44

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

查看更多
登录 后发表回答