cruim
cruim

Reputation: 319

Change value in gridview colums yii2

I have this in my view

'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    [
        'attribute' => 'order_delivery_address_city',
        'value' => 'address.order_delivery_address_city'
    ],

which shows field like '1' or '2' or '3'... How to change it to be like this

if (address.order_delivery_address_city == '1') {
    'value' => 'New-York'
}

Upvotes: 2

Views: 3331

Answers (3)

Ankit Singh
Ankit Singh

Reputation: 912

It will work fine. Try this way.

'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'attribute' => 'order_delivery_address_city',
            'value' => function($model){
                $city_name = City::find()->select(['city_name'])->where(['city_id' => $model->order_delivery_address_city])->one();
                return $city_name ? $city_name->city_name:'None';
            },
        ],

It will look into City table and will search single record for matching city_id as contained in order_delivery_address_city and return the corresponding city_name. If no matching record found then it will return None.

Upvotes: 1

Vishva G
Vishva G

Reputation: 399

Try this:

             [
                'attribute'=>'order_delivery_address_city',
                'value'=>function($model){
                  return $model->order_delivery_address_city=='1' ? 'New-York':'Anything else';
                }
            ],

Upvotes: 2

Chinmay Waghmare
Chinmay Waghmare

Reputation: 5456

Try:

    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'attribute' => 'order_delivery_address_city',
            'value' => function($model){
                         if($model->address->order_delivery_address_city == '1'){
                            return 'New-York';
                         }
                       }
        ],

Upvotes: 2

Related Questions