本文介绍了在DetailView小部件中更改属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Play的表,并在Yii2详细信息视图小部件中显示了每个记录的详细信息。我在该表中有一个 recurring 属性,该属性的类型为tinyint,可以为0或1。但是我不想将其视为数字,而是我想根据值(0或1)显示

I have a table named Play and I'm showing details of each record in Yii2 detail view widget. I have an attribute in that table recurring which is of type tinyint, it can be 0 or 1. But I don't want to view it as a number, instead i want to display yes or no based on the value (0 or 1).

我试图用detailview小部件中的函数更改它,但遇到错误: Closure类的对象无法转换为字符串

I'm trying to change that with a function in detailview widget but I'm getting an error: Object of class Closure could not be converted to string

我的详细信息查看代码:

My detail view code:

 <?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        'name',
        'max_people_count',
        'type',
        [
             'attribute' => 'recurring',
             'format'=>'raw',
             'value'=> function ($model) {
                        if($model->recurring == 1)
                        {

                            return 'yes';

                        }
                        else {
                        return 'no';
                        }
                      },
        ],
        'day',
        'time',
        ...

任何帮助将不胜感激!

推荐答案

尝试

'value' => $model->recurring == 1 ? 'yes' : 'no'

这篇关于在DetailView小部件中更改属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 20:37