Answer the question
In order to leave comments, you need to log in
How to display data in a GridView with a dynamic set of columns?
I'm trying to get into
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => $columns,
]);
?>
$columns[] = ['class' => 'yii\grid\ActionColumn', 'template' => '{view} {link}'];
foreach (???? ??) {
$columns[] = [
'attribute' => attribute_id',
'format' => 'raw',
'value' => function($model) {...},
'headerOptions' => ['style' => $width]
];
}
<?= GridView::widget([
'dataProvider' => $dataProvider,
// 'columns' => $columns,
]);
?>
Answer the question
In order to leave comments, you need to log in
This is how it "builds" the columns of the GridView if its $columns property is empty.
protected function guessColumns()
{
$models = $this->dataProvider->getModels();
$model = reset($models);
if (is_array($model) || is_object($model)) {
foreach ($model as $name => $value) {
if ($value === null || is_scalar($value) || is_callable([$value, '__toString'])) {
$this->columns[] = (string) $name;
}
}
}
}
$models = $this->dataProvider->getModels(); //массив с моделями
You can specify the 'visible' parameter for each column, something like this:
[
'attribute' => 'verified',
'visible' => is_visible('verified'),
],
is_visible
some function or method, or an associative array with values. class NotNullDetailView extends DetailView
{
protected function normalizeAttributes()
{
parent::normalizeAttributes();
foreach ($this->attributes as $i => $attribute) {
if ($attribute['value'] === null || $attribute['value'] === '')
unset($this->attributes[$i]);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question