Yii2-Pjax在第二次表单提交时重新加载整个页面


Yii2 Pjax reloads entire page on second form submit

我似乎不明白pjax为什么在第二次提交表单时重新加载页面。它在从该url /site/profile?UserSearch%5Bsearchstring%5D=mi&_pjax=%23form-pjax提取的第一个表单提交中完全按照预期工作,但是,在第一个表单之后,它失去了url /site/profile?UserSearch%5Bsearchstring%5D=m的结尾。我检查了实际的html代码,表单保留了data-ajax属性。当pjax重新加载整个页面时,我尝试增加超时时间,但这并没有改变任何事情。

以下是我的视图中的代码

<?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(); ?>

每次重新加载pjax后,必须调用所需的javascript触发器/函数,如:

$('#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});
 });
});

这不是yii2的问题,而是javascript的工作方式,当您动态添加内容时,需要为每个添加的元素绑定触发器/事件。