2016-12-02 76 views
1

我是yii2中的新成員,默認情況下,我在gridview中的字段是十進制數,但我在value屬性中有一些條件。Yii2 gridview格式屬性基於輸出?

我的代碼視圖看起來像這樣

[ 
      'attribute' => 'harga_diskon_periode', 
      'format' => function($model){ 
       if($model->diskon_now == ""){ 
        return "text"; 
       }else{ 
        return "decimal"; 
       }    
      }, 
      'value' => function($model){ 
       if($model->diskon_now == ""){ 
        return "Tidak ada diskon";      
       } 
      }, 
     ], 

所以我需要的是,如果輸出數字格式將是小數,如果輸出字符串格式將是文本。

通過上面的代碼,我得到這個錯誤

Object of class Closure could not be converted to string

it'show string|array所以我在格式屬性中使用匿名函數我讀這http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html#$format-detail

我錯了嗎?我的代碼有什麼問題?我的代碼應該如何?任何參考將不勝感激,因爲我是yii2的新手。

在此先感謝。

回答

1

GridView控件不允許封在那裏,那樣做:

'attribute' => 'harga_diskon_periode', 
'value' => function ($model) { 
    return $model->diskon_now == '' 
     ? 'Tidak ada diskon' 
     : \Yii::$app->formatter->asDecimal($model->harga_diskon_periode); 
},