display Yes and No istead of Boolean 0 and 1

Hello all,

I have generated some code, and I have a feild called Active that the database has defined as a tinyint(1).

I want to display Yes or No rather than 1 or 0 on my gridview.

The code below gives me the following error:


 PHP Notice – yii\base\ErrorException

Undefined variable: model

Note: The error line is at the bottom of the code below.


<?php


use yii\helpers\Html;

use yii\grid\GridView;


/* @var $this yii\web\View */

/* @var $searchModel frontend\models\StudentsSearch */

/* @var $dataProvider yii\data\ActiveDataProvider */


$this->title = 'Students';

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="students-index">


    <h1><?= Html::encode($this->title) ?></h1>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>


    <p>

        <?= Html::a('Create Students', ['create'], ['class' => 'btn btn-success']) ?>

    </p>


    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],

            [

                'class' => 'yii\grid\ActionColumn',

                //'template' => '{view} {update} {delete} {link}',

                'template' => '{view}',

                'buttons' => [

                    'view' => function ($url,$model) {

                        if($model->healthNotes <> ''){

                        return Html::a(

                            '<span class="glyphicon glyphicon-warning-sign"></span>', 

                            $url);

                        }

                        else {

                            return '';

                        }

                    },

                    'link' => function ($url,$model,$key) {

                            return Html::a('Action', $url);

                    },

                ],

            ],

            'studentID',

            'studentTitle',

            'firstName',

            'secondName',

            'surname',

            // 'dateOfBirth',

            // 'dateTimeCreated',

            // 'healthNotes',

            // 'schoolAttended',

            'active',

            [

             'label'=>'Active',

             'format'=>'raw',

             'value' => $model->active == 0 ? 'No' : 'Yes',  // <----- this bit is not working

            ],


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>


</div>



Ok got it!




//change this:

'value' => $model->active == 0 ? 'No' : 'Yes',


//to this:

'value' => function($model, $key, $index, $column) { return $model->active == 0 ? 'No' : 'Yes';},

Thank you to Tom[] in the chat room for pointing me in the right direction!

1 Like

@dustbin1_uk: Use the following code:


'value' => function($model) {

    return $model->active == 1 ? 'Yes' : 'No';

}

EDIT: You answered your own question.

There’s an easier alternative, set the format to boolean, that will produce the same result:

[color=#666600][[/color][color=#000000]

         [/color][color=#008800]'label'[/color][color=#666600]=&gt;[/color][color=#008800]'Active'[/color][color=#666600],[/color][color=#000000]


         [/color][color=#008800]'format'[/color][color=#666600]=&gt;[/color][color=#008800]'boolean'[/color][color=#666600],[/color][color=#000000]


         [/color][color=#000000]


        [/color][color=#666600]],[/color]

Thanks :) Always learning.

Indeed! Thank you!

But, how to filtering with yes or not,

because we must input 0 or 1 in filtermode.

set filter to array in the grid attributes to make a drop down filter in the header




[

'format' => 'boolean',

'attribute' => 'my_attrib',

'filter' => [0=>'No',1=>'Yes'],

]

1 Like