在我的用户模型中,我有一个功能:
public function getRole() {
if ($this->role == self::ROLE_USER) {
return "user";
} else if ($this->role == self::ROLE_ADMIN) {
return "admin";
}
return "unassigned";
}
Run Code Online (Sandbox Code Playgroud)
在GridView中,我想调用它,但是无法弄清楚Yii2中的方式,旧的Yii方法似乎不起作用:
[
'attribute' => 'role',
'filter' => false,
'format' => 'raw',
'value' => '$model->getRole()',
],
Run Code Online (Sandbox Code Playgroud)
我不想使用匿名函数:
'value' => function($model) {
switch($model->role) {
case 10:
return "user";
break;
case 90;
return "admin";
break;
default;
return "unassigned";
break;
}
},
Run Code Online (Sandbox Code Playgroud)
您可以使用闭包(匿名函数)
[
'attribute' => 'role',
'filter' => false,
'format' => 'raw',
'value' => function ($model) {
return $model->getRole();
},
],
Run Code Online (Sandbox Code Playgroud)
可以使用字符串或匿名函数(没有其他方法)来设置value属性
$ value-公共财产
匿名函数或字符串,用于确定要在当前列中显示的值。
如果这是一个匿名函数,则将为每一行调用该函数,并将返回值用作要在每个数据模型中显示的值。
http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html# $ value-detail
如果所需的值与$ model实例相关,则匿名函数是唯一的可能性
| 归档时间: |
|
| 查看次数: |
8820 次 |
| 最近记录: |