我似乎无法弄清楚为什么pjax会在第二次表单提交时重新加载页面。它完全按照从此网址/site/profile?UserSearch%5Bsearchstring%5D=mi&_pjax=%23form-pjax
提取的第一个表单提交的方式工作,但是,在第一个表单提交后,它会丢失网址/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(); ?>
答案 0 :(得分:0)
每次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如何工作的,当您添加动态内容时,它需要为每个添加的元素绑定触发器/事件。