Applying Css To Cgridview

Hi all,

I am applying a CSS style to 2 cells in my gridview.

Here is the code:





<?php


Yii::import('zii.widgets.grid.CGridColumn');

 

class TotalColumn extends CGridColumn {

 

    private $_total = 0;

 

    public function renderDataCellContent($row, $data) { // $row number is ignored

 

        $this->_total += $data->amount;

 

        echo $this->_total;

    }

}


?>




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

	'id'=>'adjust-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'Id',

		'reservation_id',

		'oth',

		'date',

		array('name'=>'amount','value'=>'$data->amount','cssClassExpression'=> '($data->amount < 0)? "green": "red"'),	//this works fine	

		'remarks',

		array(

            'header' => 'Cumulative Total',

            'class'  => 'TotalColumn'// how to apply the cssClassExpression here?

        ),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



I do not know how to apply the style to the ‘TotalColumn’. Can anyone please help me?

Thanks




    array(

        'header' => 'Cumulative Total',

        'htmlOptions' => array('class'=>'TotalColumn'),

    ),



If you need to apply the class to the headers and other parts, you should use headerHtmlOptions and so on too.

http://www.yiiframework.com/doc/api/1.1/CGridColumn

Thanks Keith for your reply,

I did not succeed in doing it.

I want to apply the same code that I applied in a previous cell of the grid, i.e.




 array('name'=>'amount','value'=>'$data->amount','cssClassExpression'=> '($data->amount < 0)? "green": "red"'),




I want the same evaluation to be carried out on the ‘TotalColumn’ value (<0, green else red)

You just need to do exactly the same thing then:




    array(

        'header' => 'Cumulative Total',

        'cssClassExpression' => '($data->amount < 0) ? "green": "red"'),

    ),



The evaluation must be carried out on the ‘TotalColumn’ value and not on $data->amount.

‘TotalColumn’ value is the cumulative total of $data->amount.

How are you calculating that value?

The code is found in the same php file before running the cgridview widget:-




<?php

Yii::import('zii.widgets.grid.CGridColumn');

 

class TotalColumn extends CGridColumn {

 

    private $_total = 0;

 

    public function renderDataCellContent($row, $data) { // $row number is ignored

 

        $this->_total += $data->amount;

 

        echo $this->_total;

    }

}


?>



Okay, it’s starting to come together now, I completely misunderstood what you were asking.

According to the docs, you can access the column object with $this in the cssClassExpression property. That suggests that if you make $total public, this should work:




    'cssClassExpression' => '($this->total < 0) ? "green": "red"'),



The only issue is that it’s not obvious whether this expression is evaluated before or after the renderDataCellContent() method, so you may or may not have to add the current row value to the current total in your comparison. I’d suggest experimenting with it.

Bravissimo, Keith.

Many thanks. It works like a charm. I searched through the docs but did not find any clue. I guess I’ll have to be more attentive :D

We got there in the end :)

I forgot to point out that the expression is evaluated before the renderDataCellContent() method, so I had to add the current row value to the cumulative total for it to work. You got me there finally :)