【问题标题】:yii2 detailview conditional row classyii2 detailview 条件行类
【发布时间】:2017-07-10 18:39:24
【问题描述】:

我想根据条件更改详细视图中单个属性的类:

如果我不想让它有条件,它会像这样工作:

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

我想让这个变成有条件的,我尝试了很多不同的方法,例如:

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

(我认为这是最合乎逻辑的解决方案,但没有任何反应,因此页面加载正常,但属性没有类危险,没有错误消息)

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

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

所以我不知道,我什至没有在网上找到任何帮助。你能指出我正确的方向吗?非常感谢!

【问题讨论】:

    标签: class if-statement yii2 attributes detailview


    【解决方案1】:

    你应该尝试一下:

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

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

    【讨论】:

    • 天哪,最简单的事情没有出现在我的脑海中......非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2016-05-05
    • 1970-01-01
    • 2017-03-26
    • 2017-07-18
    • 2017-11-12
    • 1970-01-01
    相关资源
    最近更新 更多