Search code examples
phpwidgetyii2dataprovider

How to make Yii2 handle two different instances of a dataprovider?


This is siteController index.php. Here I want to display two list from different table on same page.But it display only one table data for both div.when I set only $dataProvider on both div widget and if I set $dataProvider2 which create in actionIndex() it get error $dataProvider2 not found.

                        <?= GridView::widget([
                        'dataProvider' => $dataProvider,
                        'columns' => [
                            ['class' => 'yii\grid\SerialColumn'],
                       [

                           'attribute'=>'practiceCode',
                           'format' => 'raw',
                           'value'=>function ($model, $index, $widget) {
                       },
                    ],
                    'firstName',

                        ],

              ]); ?>

            </div>
            <div class="col-lg-6" style="border:solid 1px;">
                <h2>Member</h2>
                <?= GridView::widget([
                        'dataProvider2' => $dataProvider2,//Error $dataProvider2 not found
                        'columns' => [
                            ['class' => 'yii\grid\SerialColumn'],
                       [

                           'attribute'=>'memberCode',
                           'format' => 'raw',
                           'value'=>function ($model, $index, $widget) {

                       },
                    ],
                    'firstName',
              ]); ?>    
            </div>

This is actionIndex() here i have write two dataProvider($dataProvider,$dataProvider2) But i am not able to set set this dataProvider in index.php widget

public function actionIndex()
    {
        $query = new \yii\db\Query;
        $query->select('*')
              ->from('practice');
        $query->createCommand();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
            'pageSize' => 10,
             ],
        ]);
        $query = new \yii\db\Query;
        $query->select('*')
              ->from('member');
        $query->createCommand();

        $dataProvider2 = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
            'pageSize' => 10,
             ],
]);
        return $this->render('index', [
            'dataProvider' => $dataProvider,'dataprovider2'=>$dataProvider2 ]);
}

Solution

  • You Need to change dataProvider2 to dataProvider

    <div class="col-lg-6" style="border:solid 1px;">
    <h2>Member</h2>
    <?= GridView::widget([
        'dataProvider' => $dataProvider2,//Error $dataProvider2 not found
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
    
                'attribute'=>'memberCode',
                'format' => 'raw',
                'value'=>function ($model, $index, $widget) {
    
                },
            ],
            'firstName',
        ]
    ]); ?>
    

    Since dataProvider is a Property Of GridView .. It will works

    And Change Your Controller Action as

    return $this->render('index', [
            'dataProvider' => $dataProvider,'dataProvider2'=>$dataProvider2 ]);