Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show "Yes/No" CGridView yii depending on the flag field 0/1?

Tags:

php

yii

I am stuck in a problem in CGridView yii, my refund field show 0/1 but I want to show "Yes" if 0 and "No" if 1, without using any second table.

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'transaction-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
    'id',
    'member_id',
     array(
        'header' => 'MemberName',
        'name' => 'member_id',
        'value' => '$data->member->f_name'
    ),

    'refund',
    'band_id',

    array(
        'class'=>'CButtonColumn',
        'template'=>'{view}',
    ),
),

));

like image 739
user1045373 Avatar asked Jul 16 '12 09:07

user1045373


3 Answers

Both of the other answers will work, but the cleanest way to do it is:

'columns'=>array(
    'id',
    'member_id',

    ...

    'refund:boolean',
),

There are a bunch of CGridView column data types that are auto-used if you use colons like above. More info here: https://github.com/samdark/a-guide-to-yii-grids-lists-and-data-providers/blob/master/grid-columns.md

like image 174
acorncom Avatar answered Nov 07 '22 11:11

acorncom


 array(
                    'name' => 'refund',
                    'header' => "Refund",
                    'value' => '$data->refund?Yii::t(\'app\',\'Yes\'):Yii::t(\'app\', \'No\')',
                    'filter' => array('0' => Yii::t('app', 'No'), '1' => Yii::t('app', 'Yes')),
                    'htmlOptions' => array('style' => "text-align:center;"),
              ),
like image 35
Pentium10 Avatar answered Nov 07 '22 10:11

Pentium10


Hope this will solve your problem.

Replace "refund" with this code.

 array(
            'header' => 'Refund',
            'name' => 'refund',
            'value' => '($data->refund == 0) ? "Yes" : "No"'
        ),
like image 4
Onkar Janwa Avatar answered Nov 07 '22 12:11

Onkar Janwa