The action is not fired when the user confirm the deletion but, instead, a #
is added at the end of the URL.
Primefaces is actually rendering your p:menuitem
as a a
HTML tag, using the onclick
event of the element to execute its own Ajax request. E.g.
onclick="PrimeFaces.ab({...});return false;"
Notice that they added a return false;
at the end which prevents the default browser behaviour
of the a
element, therefore no #
will appear in the URL.
When you add the confirm
function, it is placed at the beginning of the onclick
element as follows:
onclick="return confirm('confirm?');PrimeFaces.ab({...});return false;"
In case you don't confirm it, no #
will appear in the URL since the default behaviour was prevented. If you do confirm it will appear. But the Ajax action will never be executed since you are calling the return
statement in the first place.
You can achieve the expected behaviour changing your p:menuitem
's onclick
event as follows:
onclick="if (!confirm('#{message.component_list_confirm_deletion}')) return false;"
Why the event is not fired while in a p:commandButton
everything works fine ?
Primefaces treats differently the p:commandButton
. It wraps user's onclick
and Primefaces Ajax functions, placing each of them in a separate function, and sends them to Primefaces#bcn
which executes the functions in order. The first one that returns false
stops the processing of the remaining functions. The onclick
in the generated HTML will be as follows:
onclick="PrimeFaces.bcn(this, event, [function(event){return confirm('confirm?')},function(event){PrimeFaces.ab({...});return false;}]);"