0
votes

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
        ],
    ]);
2
post your full code. - GAMITG
You need to edit the code you forgot to include into your question, not post it as an answer. I can't make sense of it enough to do that though. - Flexo♦
@Flexo♦ : I just started to use stackoverflow.. I was not familliar with the rules. Now i learnt everything about posting question and answer, editing etc.. but unfortunately i am blocked and now unable to post new questions and getting following error. You have reached your question limit Sorry, we are no longer accepting questions from this account. See the Help Center to learn more. can you please help how to come out of this ban.?? i had already edited my all questions posted. - vijay nathji
You haven't edited this one... - Flexo♦
@Flexo♦ Edited.. Please help how to come out of ban.. - vijay nathji

2 Answers

1
votes

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

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.