GridView generate dynamic columns from ActiveDataProvider Veriable

I am new to Yii2 Framework and I tend to end up in situations that i don’t get answers to… To blab about my current situation, I have a table with user_id , score and date column. I want to display a GridView report by pivoting the data with date between a given week as header, (unique) user in first column and score as the data corresponding to each user against the date. I wrote a query ActiveDataProvider query which does that and sends them to view.

The main challenge for me is to set up column in GridView Widget where the dates (7 day dates of given week) can be made header.

Is there any method for this I missed ?? some pointers would be appreciated immensely

  1. by query get data
  2. from data collect info to array
  3. by array data provider display data in GridView

Thanks @uldisn for your response however to clarify further, I can get data as array but my challange is to input data into the column as I don’t have fixed column label/header…

|    user   |    10/02/2020    |   11/02/2020   |   12/02/2020   |   13/02/2020    |    14/02/2020    |    15/02/2020    |
--------------------------------------------------------------------------------------------------------------------------
|    mike   |        12        |        14      |        20      |        14       |        15        |         30       |
|    cole   |        16        |        15      |        16      |        10       |        10        |         10       |
|    hank   |        18        |        14      |        15      |        13       |        17        |         19       |
|    sam    |        19        |        13      |        12      |        15       |        19        |         12       |

Right now dumping the data to GridView without column gives me

|  user_id  |    2020 02 10    |   2020 02 11   |   2020 02 12   |   2020 02 13    |    2020 02 14    |    2020 02 15    |
--------------------------------------------------------------------------------------------------------------------------
|     1     |        12        |        14      |        20      |        14       |        15        |         30       |
|     2     |        16        |        15      |        16      |        10       |        10        |         10       |
|     3     |        18        |        14      |        15      |        13       |        17        |         19       |
|     4     |        19        |        13      |        12      |        15       |        19        |         12       |

I am looking for something like below which can allow me to format the user_id to Name and format header date as well.

foreach($dataprovider as $data)
      {
         $column[] = [
                    'label' = > $data-> ???
                   'header' => ??
             ];
      }



GridView::Widget([
                 'dataProvider' => $model,
                 'columns' =>$column,
                 ])

Some week ago created my data provider for pivot: https://github.com/d3yii2/d3data

$data = [];
$dateColumns = [];
foreach($query->all() as $model){
  $dateColumns[$model->date] = $model->date;
  if(!isset($data[$model->user_id][$model->date]){
    $data[$model->user_id]['user_id'] = $model->user_id;
    $data[$model->user_id][$model->date] = 0;
  }
  $data[$model->user_id][$model->date] += $model->weight;
}

$columns = [
  [
      'header' => 'User',
      'attribute' => 'user_id',
  ]
];
foreach($dateColumns  as $date){
  $columns[] = [
      'header' => $date,
      'attribute' => $date,
   
];
}
echo GridView::Widget([
      'dataProvider' => new ArrayDataProvider([
                 'allModels' => $data,
                 'columns' =>$columns,
     ]);
1 Like

Thanks man… I did took some reference from your code and it helped me. I can atleast populate the table as desired, however since I am getting the data as array rather than model object, how can i reference the relation of each data to the model in GridVIew value function ?

Since I have the start date and end date:

$period = new DatePeriod($start, $interval, $p_end);
foreach ($period as $pd)
{
    $columns[] = [
        'header' => date('d/m/Y', strtotime($pd->format('Y-m-d'))),
        'attribute' => $pd->format('Y-m-d'),
        'value' => function($model) use ($pd) {
        / /How to convert my array data to model here?        
         },
    ];
}

This worked

foreach($period as $pd)
{
    $columns[] = [
        'header' => date('d/m/Y', strtotime($pd->format('Y-m-d'))),
        'attribute' => $pd->format('Y-m-d'),
        'value' => function($model) use ($pd){
             return $model[$pd->format('Y-m-d')]. " ". Html::a(Yii::t( 'app', '{modelClass}',['modelClass' => '',]), ['targets/edit', 'date' => $pd->format('Y-m-d'), 'user' => $model['model_id']], ['class' => 'fa fa-pencil pull-right modal-remote']);;
    },
    'format'=>'raw',
];

}

Thanks for your help :slight_smile: