multiple gridview with same model filter conflict


(Shahrulhadi) #1

Hi, i use multiple gridview with dataProvider and searchModel as below.

Gridview1:


$searchModel1 = new MyModelSearch();

$dataProvider1 = $searchModel1 ->search(Yii::$app->request->queryParams);



Gridview2:


$searchModel2 = new MyModelSearch();

$dataProvider2 = $searchModel2 ->search(Yii::$app->request->queryParams);

When i filter something on gridview 1, gridview 2 will change as well. This is because both gridview using same name for the filter input text such as MyModelSearch[name].

How to make different gridview use different name for filter input text?


(Ilaiyamdu) #2

Just use different id for each Gridview,

when you using more than one widget in your page, make sure to assign a different id.




<?= GridView::widget([

        'id' => 'your_gridview_id'

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'created_at',

            'updated_at',


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>


(Shahrulhadi) #3

Hi, thx for your fast response, unfortunately adding id doesnt solve this problem for me.


(Ilaiyamdu) #4

Post your grid view code, No need of two searchModels.


(Shahrulhadi) #5

this is part of controller action




        $searchModelSuperior = new KakitanganSearch();

        $dataProviderSuperior = $searchModelSuperior->search(Yii::$app->request->queryParams, 1);

        $dataProviderSuperior->pagination->pageParam = 'superior-page';

        $dataProviderSuperior->sort->sortParam = 'superior-sort';


        $searchModelSubordinate = new KakitanganSearch();

        $dataProviderSubordinate = $searchModelSubordinate->search(Yii::$app->request->queryParams, 2);

        $dataProviderSubordinate->pagination->pageParam = 'subordinate-page';

        $dataProviderSubordinate->sort->sortParam = 'subordinate-sort';


        $searchModelAvailableStaff = new KakitanganSearch();

        $dataProviderAvailableStaff = $searchModelAvailableStaff->search(Yii::$app->request->queryParams, 3);

        $dataProviderAvailableStaff->pagination->pageParam = 'availableStaff-page';

        $dataProviderAvailableStaff->sort->sortParam = 'availableStaff-sort';

The reason i use different search model variable, as u can see above, i add parameter value (1, 2 or 3) to search function. Depend on the value, an if statement will add the required andFilterWhere in the search class so that each gridview will obtain specific record.

Here is code for my gridview


<h2><?= Yii::t('app', 'Supervisor') ?></h2>

    <?= GridView::widget([

        'id' => 'grid1',

        'dataProvider' => $dataProviderSuperior,

        'filterModel' => $searchModelSuperior,

        // 'filterRowOptions' => ['name' => 'KakitanganSearch1'],

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'nama',

            'namapanggilan',

            'nokp',

            [

                'attribute' => 'jawatan',

                'value' => 'fkJawatan.nama',

                'label' => Yii::t('app', 'Position'),

            ],

            'status',

            [

                'attribute' => 'createByUser',

                'value' => 'createdBy.username',

                'label' => Yii::t('app', 'Created By'),

            ],

            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>


    <h3><?= Yii::t('app', 'Subordinate') ?></h3>

    <?= GridView::widget([

        'id' => 'grid2',

        'dataProvider' => $dataProviderSubordinate,

        'filterModel' => $searchModelSubordinate,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'nama',

            'namapanggilan',

            'nokp',

            [

                'attribute' => 'jawatan',

                'value' => 'fkJawatan.nama',

                'label' => Yii::t('app', 'Position'),

            ],

            'status',

            [

                'attribute' => 'createByUser',

                'value' => 'createdBy.username',

                'label' => Yii::t('app', 'Created By'),

            ],

            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>


    <h2><?= Yii::t('app', 'Staff without supervisor') ?></h2>

    <?= GridView::widget([

        'id' => 'grid3',

        'dataProvider' => $dataProviderAvailableStaff,

        'filterModel' => $searchModelAvailableStaff,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'nama',

            'namapanggilan',

            'nokp',

            [

                'attribute' => 'jawatan',

                'value' => 'fkJawatan.nama',

                'label' => Yii::t('app', 'Position'),

            ],

            'status',

            [

                'attribute' => 'createByUser',

                'value' => 'createdBy.username',

                'label' => Yii::t('app', 'Created By'),

            ],

            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>


(Ilaiyamdu) #6

Yii::$app->request->queryParams will be same on all gridview, to avoid this conflict you can use Pjax.

Try this







//namespace

use yii\widgets\Pjax;


<?php Pjax::begin(['id' => '1']) ?>


<?= GridView::widget([

        'id' => 'your_gridview_id1'

    ]); ?>

<?php Pjax::end() ?>

<?php Pjax::begin(['id' => '2']) ?>


<?= GridView::widget([

        'id' => 'your_gridview_id2'

    ]); ?>

<?php Pjax::end() ?>




(Shahrulhadi) #7

Here is my code with Pjax. But the problem still happen.


<h2><?= Yii::t('app', 'Supervisor') ?></h2>

    <?php Pjax::begin(['id' => '1']) ?>

    <?= GridView::widget([

        'id' => 'grid1',

        'dataProvider' => $dataProviderSuperior,

        'filterModel' => $searchModelSuperior,

        // 'filterRowOptions' => ['name' => 'KakitanganSearch1'],

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'nama',

            'namapanggilan',

            'nokp',

            [

                'attribute' => 'jawatan',

                'value' => 'fkJawatan.nama',

                'label' => Yii::t('app', 'Position'),

            ],

            'status',

            [

                'attribute' => 'createByUser',

                'value' => 'createdBy.username',

                'label' => Yii::t('app', 'Created By'),

            ],

            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>

    <?php Pjax::end() ?>


    <h3><?= Yii::t('app', 'Subordinate') ?></h3>

    <?php Pjax::begin(['id' => '2']) ?>

    <?= GridView::widget([

        'id' => 'grid2',

        'dataProvider' => $dataProviderSubordinate,

        'filterModel' => $searchModelSubordinate,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'nama',

            'namapanggilan',

            'nokp',

            [

                'attribute' => 'jawatan',

                'value' => 'fkJawatan.nama',

                'label' => Yii::t('app', 'Position'),

            ],

            'status',

            [

                'attribute' => 'createByUser',

                'value' => 'createdBy.username',

                'label' => Yii::t('app', 'Created By'),

            ],

            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>

    <?php Pjax::end() ?>


    <h2><?= Yii::t('app', 'Staff without supervisor') ?></h2>

    <?php Pjax::begin(['id' => '3']) ?>

    <?= GridView::widget([

        'id' => 'grid3',

        'dataProvider' => $dataProviderAvailableStaff,

        'filterModel' => $searchModelAvailableStaff,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'nama',

            'namapanggilan',

            'nokp',

            [

                'attribute' => 'jawatan',

                'value' => 'fkJawatan.nama',

                'label' => Yii::t('app', 'Position'),

            ],

            'status',

            [

                'attribute' => 'createByUser',

                'value' => 'createdBy.username',

                'label' => Yii::t('app', 'Created By'),

            ],

            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>

    <?php Pjax::end() ?>


(Ilaiyamdu) #8

Are you sure Pjax is working(Without page reload) .


(Shahrulhadi) #9

Sorry, but im not familiar with pjax.

From the docs,

I also has no eperience using pjax in normal php.

So can you explain a bit how to verify pjax is working?


(Ilaiyamdu) #10

See demo of kartik widget here, link

Getting data without page reload using ajax.

Once you familiar with Yii2 widgets, You can use kartik widgets instead of Yii widgets. It have multiple inbuilt functionalities.


(Shahrulhadi) #11

There is page loading, so that mean pjax doesnt work. How to fix it then?


(DrawilDrawil) #12

I know this is late, but I too was stuck with PJAX so I hope my comment helps new users. PJAX is also a wonderful tool that could save you a ton of time.

So PJAX updates the portion of the page that is wrapped in PJAX blocks, if you have multiple widgets (or any html that you’d like to update), then give each PJAX block a unique ID. Now whenever there’s a form submission (GridView Filter enter event) or link clicked, PJAX will redownload the entire page using AJAX behind the scene (I assume), take the portion that corresponds to the ID configured in the block and replace the existing one with the one newly obtained from the server. @Strance’s question, PJAX resolves to refreshing the page when there’s an error Or when the server takes too much time to respond. To amend the latter, you set a sufficiently large timeout value like so:

Pjax::begin(['id' => 'unique-id', 'timeout' => 99999999])