Morgan Azhari
Morgan Azhari

Reputation: 209

Add if in Yii bootstrap tbgridview

I've got this

$this->widget('bootstrap.widgets.TbGridView', array(
'type'=>'striped bordered condensed',
'dataProvider'=>$data,
'template'=>"{items}",
'columns'=>array(
    array('name'=>'name', 'header'=>'Name' ,'type'=>'raw', 'value' => 'CHtml::link(CHtml::encode($data->url),array("view","id"=>$data->id))',),
    array('name'=>'status', 'header'=>'Status', 'htmlOptions' => array('class'=>'status'),),
),

In status, there are two status: On and Off. I want to add class "green" if the status is on and class "red" if the status is off.

Do you know how I can achieve this?

Upvotes: 0

Views: 1160

Answers (1)

Michael Härtl
Michael Härtl

Reputation: 8587

You did not mention, where you want to add that class. If you want to add it to each row, you can use the rowCssClassExpression property of the GridView:

// ...
'template'=>"{items}",
'rowCssClassExpression' => '$data->status ? "green" : "red"',
// ...

If you only want to add it to a specific column, you can use the cssClassExpression of a column:

array(
    'name'=>'status',
    'header'=>'Status',
    'htmlOptions' => array('class'=>'status'),
    'cssClassExpression' => '$data->status ? "green" : "red"',
),

Upvotes: 2

Related Questions