[英]Reload page with javascript or Symfony?
提交表单后,将关闭模式并发送成功通知。 但是,新提交的数据不会显示为已捕获,因为页面不会刷新。 (无法使用PHP或javascript刷新)
在这种情况下,我认为使用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()
)
));
要使用Javascript重新加载网站,您需要调用window.location.reload()
函数。 您还可以调用window.location.href = window.location.href
,它具有类似的效果(此问题已在此处得到解答,并提供了更多详细信息, 如何使用JavaScript重新加载页面? )。
尽管我认为对您来说更好的解决方案是使用Javascript将更新的内容添加到页面中(而不是重新加载页面),但这对于用户体验来说将是“更好的”。
如果您要查找的是提交表单后刷新页面(因此模式已关闭),则可能需要查看以下内容:
$('#yourModal').on('hide.bs.modal', function () {
window.location.reload()
});
这应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.