1

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jai
  • 352
  • 2
  • 18
  • How is this javascript or jquery related? How do you want us to try to reproduce? Please show an [mcve]. Most likely you return something from the action – Kukeltje Mar 25 '16 at 11:52
  • View full code. I add onclick(Java script function). It return true when i click yes in delete confirmation. It refresh whole page . – Jai Mar 25 '16 at 12:53
  • By default PrimeFaces adds `return false;` (because of ajax) to generated html code from `p:commandButton` component so it won't trigger form submission . If you add `confirm()` to `onclick` then after user clicks 'OK' the returned value is `true` and form submission occurs (normal post submit, not ajax - hence page refresh). You could try [`p:confirmDialog`](http://www.primefaces.org/showcase/ui/overlay/confirmDialog.xhtml). – Geinmachi Mar 25 '16 at 13:15

1 Answers1

3

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.

Community
  • 1
  • 1
Geinmachi
  • 1,251
  • 1
  • 8
  • 20
  • This is fixed in the upcoming 6.0 release (and most likely a 5.3.x one too). See https://github.com/primefaces/primefaces/issues/1349 – Kukeltje May 03 '16 at 12:42