Hide attribute in Kartik DetailView Yii 2

2k views Asked by At

I want to hide attribute in my DetailView, for example:

[
    'attribute' => $attribute,
    'format' => 'raw',
    'value' => Yii::$app->formatter->asDatetime($model->$attribute).' par '.Yii::$app->myFormatter->asUser($model->visited_by),
    'visible' => false,
];

Visible property doesn't work. What do I have to do?
Do you have any idea?

Thank you

2

There are 2 answers

0
Mikołaj Woźniak On

If you need to dinamically determine what is visible Then you can try template option for te DetailView model

<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        'id',
        'title',
        'group_id',
    ],
    'template' => function ($item, $index, $widget){
        $classes = '';
        $classes .= ($item['attribute'] == 'your-attr' && $item['value'] == 'your-value') ? 'hidden' : '';

        return "<tr class='$classes'><th>$item[attribute]</th><td>$item[value]</td></tr>";
    }
]) ?>

If you know you will not use or even need those hidden values you can also dinamically determine what attributes you pass to DetailView like:

<?php 
 // your logic to know what attributes display
  echo DetailView::widget([
   'model' => $model,
   'attributes' => $attributes
]) ?>
0
Tibor Nagy On

You can use kartik's GridView and the bootstrap classes hidden and show. See Bootstrap docs.

use kartik\detail\DetailView;
....
[
    'attribute' => $attribute,
    'format' => 'raw',
    'value' => Yii::$app->formatter->asDatetime($model->$attribute).' par '.Yii::$app->myFormatter->asUser($model->visited_by),
    'rowOptions' => [
        'class' => ($i_want_to_see_it ? 'show' : 'hidden'),
    ],
];