S
S
Silverviql2018-08-29 14:40:51
Yii
Silverviql, 2018-08-29 14:40:51

How to make conditions in Gridview to replace the output data?

I want to display data on stores in a Gridview table. But the store is stored as a number and not as a string value, I did not find how you can change the value by condition, for example, switch .

[
                'attribute' => 'id_shop',
                'value'=>switch ($model->id_shop) {
                    case 2;
                    $model->id_shop= 'Московский';
                    break;
                    case 6;
                    $model->id_shop= 'Пушкина';
                    break;
                     case 9;
                    $model->id_shop= 'Сибирский';
                    break;
                    case 12;
                    $model->id_shop= 'Четаева';
                    break;
                     case 16;
                    $model->id_shop= 'Маркса';
                    break;
                },
            ],

This, as I understand it, does not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2018-08-29
@Silverviql

[
  'attribute' => 'id_shop',
  'value'=> function ($model) {
    switch ($model->id_shop) {
      case 2;
      return 'Московский';
      case 6;
      return 'Пушкина';
      case 9;
      return 'Сибирский';
      case 12;
      return 'Четаева';
      case 16;
      return 'Маркса';
    }
    return null;
  }
],

But it's better to put the decryption of stores somewhere in the model:
[
  'attribute' => 'id_shop',
  'value'=> function (MyModel $model) {    
    return ArrayHelper::getValue(MyModel::getShopList(), $model->shop_id);
  }
],

MyModel extends Model {
  public static function getShopList(){
    return [
      2 => 'Московский',
      6 => 'Пушкина',
      9 => 'Сибирский',
      12 => 'Четаева',
      16 => 'Маркса',
    ];
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question