2016-12-02 5 views
1

Ich bin neu in yii2, standardmäßig mein Feld in Gridview ist dezimal, aber ich habe eine Bedingung in Wert Eigenschaft.Yii2 Gridview Format Eigenschaft Basis auf Ausgabe?

meine Code-Ansicht sieht wie folgt aus

[ 
      '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";      
       } 
      }, 
     ], 

Also, was ich brauche, ist, wenn die Ausgangszahl das Format dezimal sein wird, und wenn Ausgabe-String wird das Format Text sein.

Mit obigem Code bekomme ich diesen Fehler

Object of class Closure could not be converted to string

ich diese http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html#$format-detail lesen it'show string|array so dass ich anonyme Funktion in Immobilien-Format verwenden.

Bin ich falsch? Was ist falsch an meinem Code? Wie sollte mein Code aussehen? Jede Referenz wird geschätzt, da ich neu bei yii2 bin.

Vielen Dank im Voraus.

Antwort

1

Gridview nicht erlaubt Schließung gibt, tun Sie es wie folgt aus:

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