W
W
walder2018-08-09 10:20:32
Yii
walder, 2018-08-09 10:20:32

Is it possible to write a function in columns for DetailView?

Hello,
is it possible to write a function to display a table, for example, I have data in the database, maybe 3 items, maybe 4. Can I write something like this?

columns => [
  foreach($data as $item) {
    return 'label'=>$item['label'],
               'value'=> $item['value'] ;
  }
]

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Bay, 2018-08-09
@walder

The question is not clear.
Do you need an indefinite number of columns to stick in the GridView? If so, then define $columns above, and fill it with whatever you like. It's an array.
If you need custom data for a specific column with a function, then read at least a little manual

[
            'label' => 'Картинка',
            'format' => 'raw',
            'value' => function($data){
                return Html::img(Url::toRoute($data->category_image),[
                    'alt'=>'yii2 - картинка в gridview',
                    'style' => 'width:15px;'
                ]);
            },
        ],

M
Maxim Fedorov, 2018-08-09
@qonand

There is no columns property in the DetailView widget, perhaps you mean the GridView widget?
In any case, you can't use the code given in the question. an attribute list cannot take a function, only an array. But no one bothers you to form this array before the widget is displayed

M
maximilianoarturo, 2018-08-09
@maximilianoarturo

You can write something like:

$attributes = [];
foreach($data as $item) {
    $attributes[] = [ 
        'label'=>$item['label'],
        'value'=> $item['value'] 
    ];
  }

...

<?= DetailView::widget([
  'model' => $model,
  'attributes' => $attributes,
])?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question