简体   繁体   中英

Yii2 Pjax reloads entire page on second form submit

I cannot seem to figure out why pjax reloads the page on the second form submission. It works exactly as intended on the first form submission pulling from this url /site/profile?UserSearch%5Bsearchstring%5D=mi&_pjax=%23form-pjax , however, after that first one it loses the ending of the url /site/profile?UserSearch%5Bsearchstring%5D=m . I checked the actual html code and the form is retaining the data-ajax attribute. I tried increasing the timeout as had been suggested when pjax is reloading the entire page, but that did not change anything.

Below is the code from my view

<?php Pjax::begin(['timeout' => 5000, 'id' => 'form-pjax']); ?>
    <?php $form = ActiveForm::begin([
        'method' => 'get',
        'action' => Url::to(['site/profile']),
        'options' => ['data-pjax' => true ],
    ]); ?>

        <?= $form->field($searchModel, 'searchstring', [
                'template' => '<div class="input-group">{input}<span class="input-group-btn">' .
                Html::submitButton('Search', ['class' => 'btn btn-default']) .
                '</span></div>',
            ])->textInput(['placeholder' => 'Find friends by username or email']);
        ?>

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

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            'username',
            [
                'label' => 'View Profile',
                'format' => 'raw',
                'value'=>function ($data) {
                    return Html::a(Html::encode("View Profile"),'/site/redirectprofile/'.$data->id);
                },
            ], 
            [
                'label' => 'Follow',
                'format' => 'raw',
                'value'=>function ($data) {
                    return Html::a(Html::encode(Follow::find()->where(['user_id' => $data->id, 'follower_id' => Yii::$app->user->Id])->exists() ? 'Unfollow' : 'Follow'),'/site/follow/'.$data->id.'/'.$data->username);
                },
            ],                           
        ],
        'summary'=>'',
    ]); ?>
<?php Pjax::end(); ?>

You must call needed javascript triggers/functions after every pjax reload like:

$('#form-pjax').on('pjax:success', function() {
  /*call Your functions here, or copy from your generated page js code,
    that bind event to pjax form, 
    at end of generated html You should have something like: */ 

 jQuery(document).on('submit', "#form-pjax form[data-pjax]", function (event) {
   jQuery.pjax.submit(event, '#form-pjax', {"push":true,"replace":false,"timeout":000,"scrollTo":false});
 });

});

This is not yii2 issue, it's how javascript works, when You add dynamically content it's needed to bind triggers/events for every added element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM