2016-05-14 63 views
1

我似乎无法弄清楚为什么pjax会在第二个表单提交中重新加载页面。它的工作原理与第一次提交表单时所提交的网址/site/profile?UserSearch%5Bsearchstring%5D=mi&_pjax=%23form-pjax完全相同,然而,在第一个表单丢失网址/site/profile?UserSearch%5Bsearchstring%5D=m的结尾之后。我检查了实际的html代码,表格保留了data-ajax属性。我尝试增加超时,正如pjax重新加载整个页面时所暗示的,但是这并没有改变任何东西。Yii2 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

必须调用所需的JavaScript每pjax重装等之后触发/功能:

$('#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的作品,当您动态添加内容时,需要为每个添加的元素绑定触发器/事件。

+0

什么代码准确地将在里面,我目前没有任何代码,使pjax –

+0

我更新了你的答案。 –

+0

不起作用,它具有相同的行为。 pjax在第一次搜索时工作,但在第二次搜索时重新加载页面 –

相关问题