1

I am new in yii2, by default my field in gridview is decimal but I have some condition in value property.

my code view looks like this

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

So what I need is, if output number the format will be decimal and if output string the format will be text.

With above code I get this error

Object of class Closure could not be converted to string

I read this http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html#$format-detail it'show string|array so I use anonymous function in format property.

Am I wrong? What's wrong with my code? How should my code looks like? any reference will be appreciated as I'm new to yii2.

Thanks in advance.

1 Answer 1

1

GridView does not allow closure there, do it like that:

'attribute' => 'harga_diskon_periode',
'value' => function ($model) {
    return $model->diskon_now == ''
        ? 'Tidak ada diskon'
        : \Yii::$app->formatter->asDecimal($model->harga_diskon_periode);
},
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.