简体   繁体   中英

How can I make Yii CGridView respect decimal point in columns with custom values

I have this grid

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'appartment-grid-milimeters-editable-totals',
    'dataProvider'=>$modelTotals->searchTotals(),
    'summaryText'=>'',
    'selectableRows' => 0,

When I a define columns that point to a model's attributes, decimal point and desired precision shows exactly as expected.

'columns'=>array(
'sharedUtilitiesParticipationMilimeters',
...)

eg, 16.20 shows in the grid.

But if I try a custom value which actually holds the same decimal value as that model's attribute, like this:

'columns'=>array(
array(
'name'=>'sharedUtilitiesParticipationMilimeters',
'header'=>'Κοιν.',
'value'=>$totals['sharedUtilitiesParticipationMilimeters'],
..)

then 16.2 shows in the grid, referring to the example above.

The same happens when I use 'value'=>number_format($totals['sharedUtilitiesParticipationMilimeters'],2,'.',''),

I could live with 16.2 as opposed to 16.20 but why is that happening?

Thank you in advance.

EDIT:

One may easily think that the problem is $totals['sharedUtilitiesParticipationMilimeters'] does not actually hold the same (decimal) value as the attribute sharedUtilitiesParticipationMilimeters itself. Yet, the strange think is that outside the grid (as a textfield) $totals['sharedUtilitiesParticipationMilimeters'] is shown 16.20 while inside the grid (as value=>$totals['sharedUtilitiesParticipationMilimeters']) it is shown as 16.2 .

This outputs 16.20 :

 <?php echo CHtml::textField('sharedUtilitiesParticipationMilimeters', $totals['sharedUtilitiesParticipationMilimeters'],
                            (bccomp($totals['sharedUtilitiesParticipationMilimeters'], 1000.00, 2) == 0) ?
                            array('size'=>10,'disabled'=>true)
                            : ((bccomp($totals['sharedUtilitiesParticipationMilimeters'], 0.00, 2) == 0) ? array('size'=>10,'disabled'=>true, 'style'=>'background-color:red') : array('size'=>10,'disabled'=>true,'style'=>'color:red')) 
                        );
                                ?>

This outputs 16.2 :

'columns'=>array( 
array( 
                    'name'=>'sharedUtilitiesParticipationMilimeters',
                    //'type'=>'raw',
                    'value'=>$totals['sharedUtilitiesParticipationMilimeters'],
                    'htmlOptions'=>(bccomp($totals['sharedUtilitiesParticipationMilimeters'], 1000.00, 2) == 0) ?
                            array('size'=>10,'width'=>'60px')
                            : ((bccomp($totals['sharedUtilitiesParticipationMilimeters'], 0.00, 2) == 0) ? array('size'=>10,'width'=>'60px', 'style'=>'background-color:red') : array('size'=>10,'width'=>'60px','style'=>'color:red')),
                    ),
),

If it's just for display purposes, append " : " followed by the type of format you want:

'columns'=>array(
    'sharedUtilitiesParticipationMilimeters:raw',
    ...
)

or

'sharedUtilitiesParticipationMilimeters:text',

For other formats, see CFormatter .

If you want to create/extend your own format, see my answer on:
Yii zii.widgets.CDetailView - Output an attribute as HTML code format

You can also follow the tutorial on the Yii wiki here:
http://www.yiiframework.com/wiki/360/custom-number-formatting-or-decimal-separators-and-i18n/#hh4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM