2016-05-14 96 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在第一次搜索時工作,但在第二次搜索時重新加載頁面 –

相關問題