phpajaxgridviewyii2pjax

Pjax reloads page on gridview search on Yii2


I'm loading a gridview inside a page with an ajax request. Then, after the page is loaded, i want to let the user order and search as usual with gridview, obviously not reloading the page.

While the sorting works, the search reloads the page (and since the action loading the ajax content is different from the current one the page changes entirely). I know Pjax reloads the entire page after the timeout value, but that is not the problem as i changed to a really high value and i still get the reload.

Also, that is the only pjax on the page.

What could be the problem?

This is the code for the view with the gridview

    <?php Pjax::begin([
        "id" => "associates-ajax-list",
        "enablePushState" => FALSE,
        "enableReplaceState" => FALSE,
        "timeout" => 5000,
    ]); ?>

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

            'name',
            'surname',

        ],
    ]); ?>
    <?php Pjax::end(); ?>
</div>

This is the code for the ajax action

public function actionAssociatesList($id) {
    $searchModel = new \app\models\AssociateSearch();
    $searchModel->associates_for = $id;
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    if (Yii::$app->request->isAjax) {
        return $this->renderAjax('associates_list', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    } else {
        return $this->render('associates_list', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }
}

The page where this content is loaded is a standard view page generated with Gii


Solution

  • The problem was the gridview. You need to specify an id. The funny thing is that this gridview was the only one generated for this page, so apparently there was no conflict.

    <?= GridView::widget([
            'id' => "grid-view-name",
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,