【问题标题】:Conditionally insert attributes in View configuration array有条件地在视图配置数组中插入属性
【发布时间】:2015-12-23 13:16:25
【问题描述】:

使用 Yii2,我正在尝试创建一个 detailView。我想隐藏空行,因此我使用了 kartik-v detailview。但是,如果属性符合特定条件,我也想隐藏它们。所以我偶然发现了this SO question,它抓住了我的问题的意图。然而,它的回答并不令人满意。 (This question 提出的问题大致相同)。一个例子

<?= DetailView::widget([
    'hideIfEmpty' => true, //available in kartik's detailview
    'model' => $model,
    'attributes' => [
        'id',
        'name', //cant be null, always shown
        'description:ntext', //can be null, so hidden thanks to kartiks detailview
        isAdmin() ? "password" :"", //an example, of course
        "hypotheticalOtherField",
        isAdmin() ? [
            'attribute'=>'client',
            'format'=>'raw',
            'value'=>function($object) {
                return Html::button("MyButton".$object->client);
            }
        ] : ""
    ]
])  ?>

如您所见,我想根据(在此示例中)用户是否为管理员来显示一些字段。可悲的是,如果不满足条件,将空字符串、空数组或空值插入属性数组会产生错误(IE The attribute must be specified in the format of "attribute", "attribute:format" or "attribute:format:label" 插入空字符串时)

我想我可以像这样创建属性数组:

$attrs = ['id','name','description:ntext'];
if (isAdmin()) array_push($attrs, "password");
array_push($attrs, "hypotheticalOtherField");
if (isAdmin()) array_push($attrs, [
    'attribute'=>'client',
    'format'=>'raw',
    'value'=>function($object) {
        return Html::button("MyButton".$object->client);
    }
]);

echo DetailView::widget([
    'hideIfEmpty' => true, //available in kartik's detailview
    'model' => $model,
    'attributes' => $attrs
]);

但是标准 Yii2 代码布局的概述被严重破坏了。

那么有什么方法可以有条件地将值插入到数组中,这样我就可以继续编写 Yii 风格的代码:美观、有条理、整洁?或者可能是 Yii2 知道在创建视图时应该跳过的值

【问题讨论】:

  • 为什么觉得attributes数组不好,?您似乎只是一个代码样式问题....我发布了一个与 yii2 风格和良好实践一致的代码。看看你喜不喜欢
  • '我想隐藏空行,因此我使用 kartik-v detailview' - 你可以使用默认视图来完成,不需要第三方插件
  • @Coz 那么怎么做呢?我不能再使用 'hideIfEmpty' 属性,因为那是 kartiks 属性,但我使用 kartiks detailview 因为该属性

标签: php arrays yii2


【解决方案1】:

您可以使用visibleDetailView

<?= DetailView::widget([
'hideIfEmpty' => true, //available in kartik's detailview
'model' => $model,
'attributes' => [
    'id',
    'name', //cant be null, always shown
    'description:ntext', //can be null, so hidden thanks to kartiks detailview
       [
            'visible' => (isAdmin() ? true : false),
            'value' => $model->password,
            'label' => 'test'
        ],    
])  ?>

添加您要添加​​的任何条件!!!在visible

【讨论】:

  • 这确实有效,但不适用于 Kartik 的详细视图。它仅将 'visible'=>false 属性定位在详细视图的底部。
【解决方案2】:

如果你尝试使用数组追加速记并且代码看起来更 yii2 时尚

基于数组的属性是一种正确(良好)的做法。

$attrs[] = ['id','name','description:ntext'];

if (isAdmin()) {
    $attrs[] = ['password'];
}
$attrs[] = ['hypotheticalOtherField']

if (isAdmin()) {
    $attrs[] = [
        'attribute'=>'client',
        'format'=>'raw',
        'value'=>function($object) {
            return Html::button("MyButton".$object->client);
    }
}

echo DetailView::widget([
    'hideIfEmpty' => true, //available in kartik's detailview
    'model' => $model,
    'attributes' => $attrs
]);

【讨论】:

  • 我也这样做,但我理解 stealthjong 的 需要更短的语法。 (不使用kartik)
猜你喜欢
  • 2022-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-01
  • 2012-04-27
  • 2017-05-28
  • 2015-02-10
  • 2018-10-26
相关资源
最近更新 更多