Change the Row Color Based on the Column value in CGridView

丶灬走出姿态 提交于 2020-01-22 14:18:00

问题


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


回答1:


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




回答2:


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(
    ),
));


来源:https://stackoverflow.com/questions/11077325/change-the-row-color-based-on-the-column-value-in-cgridview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!