Search code examples
phpyiicgridview

Style columns in CGridView based on data in yii


Is it possible to style CGridView based on data? For example these two cases:

enter image description here

Or I have to use other options?


Solution

  • Yes, you can specify css class by data. Use cssClassExpression option.

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
        'columns'=>array(
            // ...
            array(
                'name'=>'Delta',
                'value' => $data->value,
                'cssClassExpression' => '$data->delta > 0 ? "up" : "down"',
            ),
            // ...
        ),
    ));