M
M
Monitorkin2017-03-15 00:37:30
Yii
Monitorkin, 2017-03-15 00:37:30

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,
  ]);
?>

data from $dataProvider.
The ActiveDataProvider is used, the query for it is built using yii\db\Query().
The composition of the columns, their name and data type in them (selected by the user) are not known in advance.
The columns should look like this:
$columns[] = ['class' => 'yii\grid\ActionColumn', 'template' => '{view} {link}'];
foreach (????   ??) {
    $columns[] = [
      'attribute' => attribute_id',
      'format' => 'raw',
      'value' => function($model) {...},
      'headerOptions' => ['style' => $width]
      ];		
  }

If I do like this:
<?= GridView::widget([
    'dataProvider' => $dataProvider,
   // 'columns' => $columns,
  ]);
?>

, then the data is displayed, but I need to control the output, format the data in the cells depending on the type, add attributes, etc. While there is no clear understanding of how the Gridview is built ...
Please tell me how to correctly form the columns in the Foreach loop, where are the column data pulled from? I realized that the data itself is present in the $models variable, here again we are talking about the $model variable, I got completely confused ...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey, 2017-03-15
@masterfreelance

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;
                }
            }
        }
    }

M
Maxim Timofeev, 2017-03-15
@webinar

$models = $this->dataProvider->getModels(); //массив с моделями

everything is here: www.yiiframework.com/doc-2.0/yii-data-activedatapr...

A
AlexKuznec, 2017-03-17
@AlexKuznec

You can specify the 'visible' parameter for each column, something like this:

[
         'attribute' => 'verified',
         'visible' => is_visible('verified'),
],

where is_visiblesome function or method, or an associative array with values.
In general, look at the source code of widgets and other classes, fill in the data by analogy, or inherit from the class and modify the functions themselves. Often this is faster than searching for answers through Google (but you also need to know the documentation). I have already corrected a lot of things in this way)
For example, so that the DetailView does not display empty values, it is enough to do this:
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 question

Ask a Question

731 491 924 answers to any question