Bianca
(Biancajsen)
November 9, 2012, 12:42pm
1
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
Keith
(Kburton)
November 9, 2012, 12:52pm
2
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
Bianca
(Biancajsen)
November 9, 2012, 1:17pm
3
Keith:
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)
Keith
(Kburton)
November 9, 2012, 1:24pm
4
You just need to do exactly the same thing then:
array(
'header' => 'Cumulative Total',
'cssClassExpression' => '($data->amount < 0) ? "green": "red"'),
),
Bianca
(Biancajsen)
November 9, 2012, 1:28pm
5
Keith:
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.
Keith
(Kburton)
November 9, 2012, 2:28pm
6
How are you calculating that value?
Bianca
(Biancajsen)
November 9, 2012, 2:56pm
7
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;
}
}
?>
Keith
(Kburton)
November 9, 2012, 3:17pm
8
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.
Bianca
(Biancajsen)
November 9, 2012, 3:31pm
9
Keith:
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
Bianca
(Biancajsen)
November 9, 2012, 7:43pm
11
Keith:
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.
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