0

I'm developing a project using Yii2 and get frustrated with Pjax because it never works for me. Here is a simple gridview with pjax that I have created:

<?php Pjax::begin(['id' => 'member-list-pjax', 'timeout' => 5000]);?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            'member_card_number',
            'member_name',
            [
                'attribute' => 'member_address',
                'format' => 'raw',
            ],
            'member_phone1',
            'member_phone2',
            [
                'attribute' => 'member_type',
                'value' => function ($model, $key, $index, $column){
                    return $model->getTypeLabel($model->member_type);
                },
                'filter' => $searchModel->getTypeLabel(),
            ],
        ],
    ]); ?>
<?php Pjax::end();?>

The Pjax doesn't work at all. When I search something in gridview's filter, it reloads the entire page. There is also no javascript error. Can anybody help me?

UPDATE

My action is actually very long. But here is the part that I think is related to the gridview:

$searchModel = new MemberSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

if (isset($_GET['MemberSearch']))
    $searchModel->attributes = $_GET['MemberSearch'];

return $this->render('form', [
    'searchModel' => $searchModel,
    'dataProvider' => $dataProvider,
]);

UPDATE 2

I use kartik's gridview now, I removed the PJax and put 'pjax' => true in the gridview, it successfully do Ajax Filter, BUT still reload the whole page not long after.

GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'pjax' => true,
    'pjaxSettings' => [
        'neverTimeout' => true,
    ],
    'columns' => [ /*columns same as above*/ ]
]);
9
  • check your ajax response, pjax will load entire page if there is html present in reponse
    – Nitin Pund
    Commented May 4, 2017 at 6:23
  • Check id, maybe is duplicate. Also you can check this link
    – tigrasti
    Commented May 4, 2017 at 7:25
  • try set higher timeout (10000) for testing purposes (page is reloaded if result is not returned with 200 OK within your timeout). in which way is your action code different from the generated one?
    – e-frank
    Commented May 4, 2017 at 8:55
  • @NitinP Why does it return html in response? Commented May 8, 2017 at 10:26
  • @tigrasti no id is duplicated Commented May 8, 2017 at 10:26

1 Answer 1

1

Please check your id and put this javascript in top of the page

$('body').on('click','.reload',function(){
    $.pjax.reload({container: '#w0-pjax'});
});

Also use below code

<?php \yii\widgets\Pjax::begin(['linkSelector'=>'','id'=>'w0-pjax']); ?>    
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.