Change Display Data in GridView Widget

Hi All,

In the index file of Model User,




<?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

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


            'id',

            'username',

            [

                'attribute' => 'role_id',

                'value' => 'role.role_name',

            ],            

            'Note:ntext',            

            'email:email',

            'status',

            'created_at',

            'updated_at',                          

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

        ],

    ]); ?>



how to change field status to ACTIVE or INACTIVE instead of number 0 or 10.

Thanks.

Regards,

Wilson

Try this: http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html#$value-detail

I would usually create a getter method that returns the string representation of the status field.




public function getStatusText()

{

    switch ($this->status) {

    case self::ACTIVE

        $text = "Active";

        break;

    case self::INACTIVE:

        $text = "Inactive";

        break;

    default:

        $text = "(Undefined)";

        break;

    }

    return $text;

}



And you can use it in GridView’s column.




            [

                'attribute' => 'status',

                'value' => 'statusText',

            ],            



Thanks softark. This method works.

Hi,

in DetailView Widget how to map these matter too? because i try same solution does not work.




<?= DetailView::widget([

        'model' => $model,

        'attributes' => [

            'id',

            'username',            

            'email:email',

            [

                'attribute' => 'status',

                'value' => 'StatusText',

            ],    

            'created_at',

            'updated_at',

            'role.role_name',

            'note:ntext',

        ],

    ]) ?>



Try "statusText" with a lowercase "s".

Hi Patrick Jones,

in model user function i use Big letter getStatusText() so i called it with StatusText. While in index file, it works with StatusText.

I solve with this method




[

   'attribute' => 'status',

   'value' => $model->status == 10 ? 'ACTIVE' : 'INACTIVE',

],