I have Users model.
Displaying it with CGridView i want to color the rows depending on $model->is_admin
which is an integer either 0 or 1, so either of 2 colors.
Could it be done by simple settings of CGridView ?
Controller
/**
* 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,
));
}
View
<div class="row">
<div class="col-sm-12 table-responsive">
<?php
$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(
'username',
'first_name',
'last_name',
'email',
),
array(
'class' => 'CButtonColumn',
),
),
));
?>
</div>
</div>
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(
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.