2016-09-17 41 views
0

提交表單後,模式將關閉,併發送成功通知。 但是,新提交的數據不會像捕獲那樣顯示,因爲頁面不會刷新。 (不能讓它刷新PHP或JavaScript)用javascript或Symfony重新載入頁面?

在這種情況下,我會假設一個JavaScript刷新將是理想的 - 或者將Symfony重定向更合適?在PHP/symfony的

成功代碼:

$passthroughVars = array(
    'closeModal'=> 1, 
    'jsContent' => 'custom_widget' 
); 

$passthroughVars['widgetHtml'] = $this->renderView('WidgetBundle:Widget:widget.html.php', array(
    'widget' => $widget, 
)); 
$passthroughVars['widgetId'] = $widget->getId(); 
$passthroughVars['flashes'] = $this->getFlashContent(); 

$response = new JsonResponse($passthroughVars); 
$response->headers->set('Content-Length', strlen($response->getContent())); 

return $response; 

的Javascript:

Widgety.widgetOnLoad = function (container, response) { 
    if (response) { 
     if (response.upWidgetCount) { 
      var count = parseInt(mQuery('#WidgetCount').html()); 
      count = (response.upWidgetCount > 0) ? count + 1 : count - 1; 

      mQuery('#WidgetCount').html(count); 
     } 

     if (response.widgetHtml && response.widgetId) { 
      var el = '#Widget' + response.widgetId; 
      if (mQuery(el).length) { 
       mQuery(el).replaceWith(response.widgetHtml); 
      } else { 
       mQuery('#widget-container .widget').prepend(response.widgetHtml); 
      } 

      mQuery(el + " *[data-toggle='ajaxmodal']").off('click.ajaxmodal'); 
      mQuery(el + " *[data-toggle='ajaxmodal']").on('click.ajaxmodal', function (event) { 
       event.preventDefault(); 

       Widgety.ajaxifyModal(this, event); 
      }); 

      mQuery(el + " *[data-toggle='confirmation']").off('click.confirmation'); 
      mQuery(el + " *[data-toggle='confirmation']").on('click.confirmation', function (event) { 
       event.preventDefault(); 
       WidgetyVars.ignoreIconSpin = true; 
       return Widgety.showConfirmation(this); 
      }); 
     } 

     if (response.deleted && response.widgetId) { 
      var el = '#Widget' + response.widgetId; 
      if (mQuery(el).length) { 
       mQuery(el).remove(); 
      } 
     } 
    } 
}; 

在JavaScript中,我嘗試添加document.location.reload(true)但內容並沒有刷新。 同樣,使用PHP重定向,模式拒絕關閉,即使通過了closeModal = 1

代碼:

return $this->redirect($this->generateUrl('widget_page', 
array(
    'passthroughVars' => array(
     'closeModal' => 1, 
    ), 
    'widgetAction' => 'overview', 
    'widgetId' => $widget->->getId() 
    ) 
)); 

回答

0

要重新加載在Javascript一個網站,你需要調用window.location.reload()功能。您也可以撥打window.location.href = window.location.href,它具有類似的效果(這已經在這裏得到解答,並提供更多詳細信息,How to reload a page using JavaScript?)。

雖然我認爲一個更好的解決方案是將更新的內容添加到您的頁面與JavaScript(而不是重新加載頁面),這對用戶體驗會更好。

+0

這樣做需要添加一個調用模板,是否正確?你能否指點我一個例子,以便我更好地理解如何實現這樣的目標? – bottomrocks

相關問題