Yii2 gridview格式属性基于输出?

时间:2016-12-02 08:22:17

标签: yii2

我是yii2中的新手,默认情况下我的gridview中的字段是十进制但我在value属性中有一些条件。

我的代码视图看起来像这样

[
            '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

我读了http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html#$format-detail这就是string|array所以我在格式属性中使用了匿名函数。

我错了吗?我的代码出了什么问题?我的代码应该怎么样?任何参考将被赞赏,因为我是yii2的新手。

提前致谢。

1 个答案:

答案 0 :(得分:1)

GridView不允许在那里关闭,就像那样:

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