我有一个名为Play的表,我在Yii2详细视图小部件中显示每条记录的详细信息.我在该表中有一个属性recurring为tinyint 的属性,它可以是0或1.但我不想将其视为数字,而是我想显示yes或no基于值(0或1).
我正在尝试使用detailview小部件中的函数更改它,但我收到一个错误: Object of class Closure could not be converted to string
我的详细视图代码:
<?= 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',
...
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激 !
aro*_*hev 15
与GridView处理一组模型不同,DetailView流程只有一个.因此不需要使用闭包,因为$model它是唯一一个用于显示的模型,并且在视图中可用作变量.
你绝对可以使用rkm建议的解决方案,但有更简单的选择.
顺便说一句,您可以简化条件,因为允许的值只是0和1:
'value' => $model->recurring ? 'yes' : 'no'
Run Code Online (Sandbox Code Playgroud)
如果您只想将值显示为布尔值,则可以使用冒号添加格式化程序后缀:
'recurring:boolean',
Run Code Online (Sandbox Code Playgroud)
'format' => 'raw' 这里是多余的,因为它只是没有html的文本.
如果要添加更多选项,可以使用:
[
'attribute' => 'recurring',
'format' => 'boolean',
// Other options
],
Run Code Online (Sandbox Code Playgroud)
使用formatter是一种更灵活的方法,因为这些标签将根据config中设置的应用程序语言生成.
官方文件:
rkm*_*rkm 13
尝试
'value' => $model->recurring == 1 ? 'yes' : 'no'
Run Code Online (Sandbox Code Playgroud)