本文介绍了yii2 detailview条件行类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据条件更改detailview中单个属性的类:

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)

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

=错误讯息: htmlspecialchars()期望参数1为字符串,给定对象

所以我没有任何线索,我甚至找不到任何帮助网络。你能指点我正确的方向吗?非常感谢!

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!

推荐答案

您应该尝试:

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

DetailView 只显示一个模型,这里不需要任何函数。

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

这篇关于yii2 detailview条件行类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:01