简体   繁体   English

使用p:commandButton意外刷新整个页面

[英]unexpected whole page refresh using p:commandButton

I am using below code to delete the row in p:datatable . 我正在使用下面的代码删除p:datatable的行。 If am using onclick method inside p:commandbutton tag it refresh whole page else its working fine. 如果在p:commandbutton标记内使用onclick方法,则刷新整个页面,否则工作正常。 please Give me some solution. 请给我一些解决方案。

<p:commandButton title="Delete Affiliation" immediate="true" process="@this"
    update="affiliationList" alt="Delete Affiliation" icon="ui-icon-trash"
    action="#{readAuthorDetailsbean.deleteSelectedAffiliation}" style="width:30px"
    onclick="return confirm('Are you sure, Do you want to delete this record?');">
    <f:setPropertyActionListener value="#{affiliation}" 
        target="#{readAuthorDetailsbean.selectedAffilliationListAL}" />
</p:commandButton>

If you rephrase your onclick attribute to 如果您将onclick属性改为
if (!confirm('Are you sure, Do you want to delete this record?')) return false;
then it should work as expected. 那么它应该会按预期工作。 What it does is: 它的作用是:

  • if user confirms it doesn't return a value and PrimeFaces can further process AJAX request 如果用户确认它没有返回值并且PrimeFaces可以进一步处理AJAX请求
  • if user cancels confirmation then returned value is false and further processing is aborted. 如果用户取消确认,则返回的值为false并且中止进一步的处理。

If you just do return confirm('Are you sure?') and user is sure, the returned true value cancels PrimeFaces added return false at the end of the onclick attribute (it does that for ajax) and instead of ajax request you see normal post submission (page reload). 如果您只是return confirm('Are you sure?')并且用户确定,则返回的true值会取消PrimeFaces在onclick属性末尾添加的return false (它对ajax起作用),而不是ajax请求,您会看到正常提交后(重新加载页面)。
return false cancels default behavior of html elements . return false取消html元素的默认行为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM