webSkunk webSkunk - 12 days ago 4x
PHP Question

How to color rows depending on data values Yii CGridView

I have Users model.

Displaying it with CGridView i want to color the rows depending on

which is an integer either 0 or 1, so either of 2 colors.

Could it be done by simple settings of CGridView ?


* Lists all models.
public function actionIndex() {
$model = new Users('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['Users']))
$model->attributes = $_GET['Users'];

$this->render('index', array(
'model' => $model,


<div class="row">
<div class="col-sm-12 table-responsive">
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'users-grid',
'itemsCssClass' => 'table table-bordered table-hover dataTable',
'dataProvider' => $model->search(),
'enablePagination' => false,
// 'filter'=>$model,
'columns' => array(
'class' => 'CButtonColumn',


Here is a good looking example which may help you:


I had no luck implementing it by myself, using only build-in features, so it might be the only option. I did not try this one, but as comments say, it should work.

EDIT: I made it, much shorter way

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $data_provider,
    'rowCssClassExpression' => '$data["is_admin"] ? "tr_isadmin" : ""',
    'columns' => array(
            'header' => 'Is admin?',
            'name' => 'is_admin',

So, rowCssClassExpression adds a class for tr wrapper based on $data["is_admin"] value.

Then, simply add css like

.tr_isadmin { 
    background-color: red; 

Of course, it is just simplified example. You have to adapt it for your purpose.