1
votes

I have configured my Gridview to export data into Excel. Additionally, I've set my page limit to 15 and currently have 80 records.

Now, whenever I toggle the gridview to display all records for exporting, the pagination disappears but only the current items displayed on the gridview (15 items) are shown when it should be all 80.

After playing around with my code, I found out that this occurs whenever I try to access the dataprovider in the controller before it is rendered in the view.

Even this simple snippet in the controller causes this issue:

var_dump($dataProvider->getModels());

If I remove the dataprovider access, the toggle works fine.

How can I resolve this issue? I really need to access the data inside the dataprovider to toggle my columns' visibility depending on data. before it is rendered into the view.

I am using Kartik Gridview for Yii 2 if that means anything.

Update: My code look something like this

Controller

$model = new MyModelSearch()
$dataProvider = $model->search(Yii::$app->request->queryParams);
$hasTypes = ([type1 => false, type2 => false, type3 => false]);

//this causes my error
foreach($dataProvider->getModels() as $data) {
       foreach($data->myModel as $m) {
            if($hasTypes[$m->type]) {
                continue;
                $hasTypes[$m->type] = true;
            }
       }
}
return $this->render('my-view',[
      'model' => $model,
      'dataProvider' => $dataProvider,
      'hasTypes' => $hasTypes
]);

View

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
              'label' => 'Type 1',
              'attribute' => 'type',
              'value' => 'type_value',
              'visible' => $hasTypes[1] // check if type is present
        ],
        'toolbar' => ['{export}', '{toggleData}'],
        'export' => ['target' => GridView::TARGET_SELF],
        'panel' => ['type' => GridView::TYPE_PRIMARY],
])
1
show your controller and view code for this action.. - ScaisEdge

1 Answers

1
votes

I found a similar issue in the github repository so I asked there and the issue was resolved finally. The quick solution was to clone the data provider and access the models using the clone like so:

$dataProviderClone = clone $dataProvider;
foreach($dataProviderClone->getModels() as $data) {
     // do something
}

Still don't understand what was causing the problem though.