提交表單後,模式將關閉,併發送成功通知。 但是,新提交的數據不會像捕獲那樣顯示,因爲頁面不會刷新。 (不能讓它刷新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()
)
));
這樣做需要添加一個調用模板,是否正確?你能否指點我一個例子,以便我更好地理解如何實現這樣的目標? – bottomrocks