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

I ran into a problem in CGridView yii, my return field shows 0/1, but I want to show β€œYes” if 0 and β€œNo” if 1, without using a 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}',
    ),
),

));

+5
source share
5 answers
 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;"),
              ),
+3
source

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

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

    ...

    'refund:boolean',
),

There are a bunch of CGridView column data types that are automatically used if you use a colon, as described above. More details here: https://github.com/samdark/a-guide-to-yii-grids-lists-and-data-providers/blob/master/grid-columns.md

+19
source

, .

"" .

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

CGridView name:type:header , boolean. .

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

CActiveForm, , . - , null.

$form->dropDownList($model,'refund', array(null=>"Not checked", 0=>"No", 1=>"Yes"));
+1

ADMIN.PHP

(             '' = > '',             '' = > '',             '' = > Array ( '1' = > 'Inacive', '2' = > ''),             '' = > '($ - > == "1" )? ( "Inacive" ):( "" )'         ),

-1

All Articles