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

15,031

Solution 1

 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;"),
              ),

Solution 2

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

Solution 3

Hope this will solve your problem.

Replace "refund" with this code.

 array(
            'header' => 'Refund',
            'name' => 'refund',
            'value' => '($data->refund == 0) ? "Yes" : "No"'
        ),

Solution 4

When displaying a boolean field in a CGridView use the name:type:header format when creating the columns to specify the type as boolean. E.g.

$this->widget('zii.widgets.grid.CGridView', array(
    ...
    'columns'=>array(
       'id',
       'refund:boolean',
),

If you want to change the way the field is displayed in a CActiveForm change the render method to use either a checkbox or a dropdown list. My preference is dropdown list because it gives you the option of setting the value back to null.

$form->dropDownList($model,'refund', array(null=>"Not checked", 0=>"No", 1=>"Yes"));
Share:
15,031
user1045373
Author by

user1045373

Updated on June 09, 2022

Comments

  • user1045373
    user1045373 almost 2 years

    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}',
        ),
    ),
    

    ));