Reputation: 1638
Need help in Undefined variable: dataProvider in yii2 I have installed kartik gridview extension. I am new in YII2. trying to build the grid. Please help.
Getting Following error
PHP Notice – yii\base\ErrorException Undefined variable: dataProvider
Here is my Controller Code
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'containerOptions' => ['style'=>'overflow: auto'], // only set when $responsive = false
'beforeHeader'=>[
[
'columns'=>[
['content'=>'Header Before 1', 'options'=>['colspan'=>4, 'class'=>'text-center warning']],
['content'=>'Header Before 2', 'options'=>['colspan'=>4, 'class'=>'text-center warning']],
['content'=>'Header Before 3', 'options'=>['colspan'=>3, 'class'=>'text-center warning']],
],
'options'=>['class'=>'skip-export'] // remove this row from export
]
],
'toolbar' => [
['content'=>
Html::button('<i class="glyphicon glyphicon-plus"></i>', ['type'=>'button', 'title'=>Yii::t('kvgrid', 'Add Book'), 'class'=>'btn btn-success', 'onclick'=>'alert("This will launch the book creation form.\n\nDisabled for this demo!");']) . ' '.
Html::a('<i class="glyphicon glyphicon-repeat"></i>', ['grid-demo'], ['data-pjax'=>0, 'class' => 'btn btn-default', 'title'=>Yii::t('kvgrid', 'Reset Grid')])
],
'{export}',
'{toggleData}'
],
'pjax' => true,
'bordered' => true,
'striped' => false,
'condensed' => false,
'responsive' => true,
'hover' => true,
'floatHeader' => true,
'floatHeaderOptions' => ['scrollingTop' => $scrollingTop],
'showPageSummary' => true,
'panel' => [
'type' => GridView::TYPE_PRIMARY
],
]);
Upvotes: 0
Views: 6237
Reputation: 1695
In your controller you must pass all the variables that must appear in the view to the render method you call at the end of the controller:
return $this->render('viewName', [
'dataProvider' => $dataProvider,
// ... Other fields
]);
Or you can call compact() like this:
return $this->render('viewName', compact('dataProvider'));
See also: Yii2 actions
Hope this helps.
Upvotes: 1
Reputation: 638
Your view expects that you pass the dataProvider variable in your controller action.
So make sure you have something like this in your controller action:
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
Upvotes: 2