cGridView conditional formating


id int



Using CGridView, if record is not read (is_read != 1) then display record in bold, else no bold.

please provide direction…

In your model:

        public function bold($field=null)


            if (!empty($field))

                $field  = '<strong>'.$field.'</strong>';   


            return $field;


In CGridView:


                  'name' => 'name',

                  'type' => 'raw',

                  'value' => '($data->is_read !== "1")?$data->bold($data->name):$data->name',


Hope this helps.

how to change the background of the 1 line row if the condition matched ?


You can change CSS class depending on the condition:

sorry, I don’t get it, but seems it interesting.

can you give me sample code ?

$this->widget('zii.widgets.grid.CGridView', array(




        'rowCssClassExpression'=>'$data->status == 0 ? "inactive" : "active"',

In more complex cases you can specify "rowCssClassExpression" as a function(){…}, it works only in PHP 5.3+

P.S. It just defines a CSS class for each row. You can set background-color and other options in main.css file.

thanks for that, it works. We need to know what folder the styles.css for that gridview. It is in assets folder.

I did it succesfully, but change the class even and odd. how to let the even and odd class stay remain ?

so it only for conditional.

rowCssClassExpression overwrites any classes applied to the row, so you will need to set even/odd class there too (using $row variable). Also you should define your CSS classes in webroot/css/main.css, don’t modify files in the “assets” folder.

fisrt thanks for the solution…i need in advance to show the particular row in bold how can this be made?..

Sorry i didn’t get your question?