yii2 detailview conditional row class

China☆狼群 提交于 2019-12-24 09:18:49

问题


I would like to change class for one single attribute in detailview, based on a condition:

If I wouldn't want to make it conditional, it would be working like so:

[
    'attribute' => 'ungueltig',
    'format' => 'boolean',
    'contentOptions' => [
        'class' => 'danger',
        ]
],

I want this one to change to conditional, and I have tried a lot of different ways, e.g.:

[
    'attribute' => 'ungueltig',
    'format' => 'boolean',
    'contentOptions' => function ($model) {
        if ($model->ungueltig == 1) {
            return ['class' => 'danger'];
        } else {
            return '';
        }
    },
],

(I would think this is the most logical solution, but nothing happens, so page is loading fine but without class danger at the attribute, no error message)

or

[
    'attribute' => 'ungueltig',
    'format' => 'boolean',
    'contentOptions' => ['class' => function ($model) {
        if ($model->ungueltig == 1) {
            return 'danger';
        } else {
            return '';
        }
    },]
],

= error message: htmlspecialchars() expects parameter 1 to be string, object given

so I have no clue and I don't even find any help on the web. Can you please point me to the right direction? Many thanks!


回答1:


You should simply try :

'contentOptions' => [
    'class' => ($model->ungueltig == 1) ? 'danger' : '',
],

DetailView display only one model, you don't need any function here.



来源:https://stackoverflow.com/questions/42362266/yii2-detailview-conditional-row-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!