I'm a student and working on my practical project. Its a web application which tracks google rankings for keywords and shows them as a table. Now I want to color every row green, if the current ranking is better than the previous, red if the ranking went down etc. How can I implement this in Yii with CGridView? Currently I have the following solution. In the ControllerClass I have a function
public function getCssClass($data)
{
$cssClass;
if('($data->current_pos>$data->prev_pos) || ($data->current_pos===null && $data->prev_pos!==null)')
{
$cssClass='rdown';
}
elseif('$data->current_pos<$data->prev_pos')
{
$cssClass='rup';
}
else
{
$cssClass='requal';
}
return $cssClass;
}
And in the view I call this function at 'rowCssClassExpression':
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'keyword-grid',
'dataProvider'=>$keywordDataProvider,
'rowCssClassExpression' => $this->getCssClass($data),
'columns'=>array(
'keyword_name',
array(
'name'=>'current_pos',
'htmlOptions'=>array('style'=>'text-align: center'),
),
array(
'name'=>'prev_pos',
'htmlOptions'=>array('style'=>'text-align: center'),
),
array(
'name'=>'top_pos',
'htmlOptions'=>array('style'=>'text-align: center'),
),
'url',
array(
'class'=>'CButtonColumn',
'template'=>'{view}{delete}',
'viewButtonUrl'=>'Yii::app()->createUrl("/keyword/view", array("id"=>$data->id))',
'deleteButtonUrl'=>'Yii::app()->createUrl("/keyword/delete", array("id"=>$data->id))',
),
),
'nullDisplay'=>'-',
)); ?>
This is my table:
Keyword current previous
Keyword1 7 7
Keyword2 8 10
Keyword3 26 20
But the html result is that every row gets the class 'rdown'. I can't find my mistake :-( If someone could help and point me in the right direction I would really appreciate it.