multiple gridview with data of same model in one page

Hi, i has 2 gridview in one page. Each gridview has their own data provider that load data of same model.

$kumpulan_responden = \app\models\KumpulanResponden::find()->where(['id' => $idkr])->one();

$searchModel = new RespondenSearch();

$searchModel2 = new RespondenSearch();

$dataProvider = $searchModel->search(Yii::$app->request->queryParams, 2, $kumpulan_responden);

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

I has done like in the guide about multiple gridview in one page

$dataProvider->pagination->pageParam = 'responden1-page';

$dataProvider->sort->sortParam = 'responden1-sort';

$dataProvider2->pagination->pageParam = 'responden2-page';

$dataProvider2->sort->sortParam = 'responden2-sort';

Problem is when i search data in one gridview, result of the other gridview also change.

I think it is because my gridview both load data from same model, the guide give example of each gridview has data from different model.

So how to solve this problem?


use yii\widgets\Pjax;

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

                <?= GridView::widget([

                    'dataProvider' => $dataProvider,

                    'filterModel' => $searchModel,

                    'columns' => [  ],

                ]); ?>

                <?php Pjax::end(); ?>

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

                <?= GridView::widget([

                    'dataProvider' => $dataProvider2,

                    'filterModel' => $searchModel2,

                    'columns' => [ ],

                ]); ?>

          <?php Pjax::end(); ?>

It will work.

Hi I am also in the same situation and tried your suggestion, but not able to get it to work. it is searching on both gridviews.

For those who are still looking for the answer, this is how I solve it:

Add public variable and overwrite the formName by the variable:

class ModelSearch extends Model {

    public $formNameParam = "ModelSearch";

     * @return string — the form name of this model class.
    public function formName() {
        return $this->formNameParam;

In your controller, you just need to assign whatever the name you want:

$searchModel = new ModelSearch(["formNameParam" => "FirstModelSearch"]);

Thanks for the answer, back then i am very new to yii, dont even know how to read the docs properly. Back then i think i solve it by modifying each of the search input name in the gridview configuration. After that, other project does not require to has multiple gridview of same model in same page, so i never look for easier solution. I has just check the doc, your solution is the proper one, thanks again.

1 Like