简体   繁体   中英

unexpected whole page refresh using p:commandButton

I am using below code to delete the row in p:datatable . If am using onclick method inside p:commandbutton tag it refresh whole page else its working fine. 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
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
  • if user cancels confirmation then returned value is false and further processing is aborted.

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 false cancels default behavior of html elements .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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