[英]Primefaces p:menuitem don't fire event when onclick event return true
I'm using the PrimeFaces p:menuitem
component to perform a deletion. 我正在使用PrimeFaces
p:menuitem
组件执行删除。 In order to add a confirmation step, I used the onclick
event in which a JavaScript confirm dialog is displayed. 为了添加确认步骤,我使用了
onclick
事件,其中显示了JavaScript确认对话框。 Below my code : 在我的代码下面:
<p:menuitem icon="fa fa-fw fa-remove"
title="#{message.global_remove}"
value="#{message.global_remove}"
actionListener="#{componentListMB.delete(cp.id)}"
onclick="return confirm('#{message.component_list_confirm_deletion}')"
update=":component_list" />
The action is not fired when the user confirm the deletion but, instead, a #
is added at the end of the URL. 当用户确认删除时,不会触发该操作,而是在URL的末尾添加
#
。
Why the event is not fired while in a p:commandButton
everything works fine ? 为什么在
p:commandButton
所有事件都可以正常工作时不触发事件?
I'm using: 我正在使用:
The action is not fired when the user confirm the deletion but, instead, a
#
is added at the end of the URL.当用户确认删除时,不会触发该操作,而是在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. Primefaces实际上是渲染您的
p:menuitem
作为a
HTML标签,使用onclick
元素的事件来执行自己的Ajax请求。 Eg 例如
onclick="PrimeFaces.ab({...});return false;"
Notice that they added a return false;
注意,他们添加了
return false;
at the end which prevents the default browser behaviour
of the a
element, therefore no #
will appear in the URL. 最后
which prevents the default browser behaviour
a
元素which prevents the default browser behaviour
,因此URL中不会出现#
。
When you add the confirm
function, it is placed at the beginning of the onclick
element as follows: 添加
confirm
函数时,它会按如下所示放置在onclick
元素的开头:
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. 如果您不确定,则URL不会出现
#
,因为阻止了默认行为。 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. 但是Ajax操作将永远不会执行,因为您首先要调用
return
语句。
You can achieve the expected behaviour changing your p:menuitem
's onclick
event as follows: 您可以通过更改
p:menuitem
的onclick
事件来实现预期的行为,如下所示:
onclick="if (!confirm('#{message.component_list_confirm_deletion}')) return false;"
Why the event is not fired while in a
p:commandButton
everything works fine ?为什么在
p:commandButton
所有事件都可以正常工作时不触发事件?
Primefaces treats differently the p:commandButton
. Primefaces对
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. 它包装了用户的
onclick
和Primefaces Ajax函数,将每个函数放在一个单独的函数中,然后将它们发送到Primefaces#bcn
,后者按顺序执行这些函数。 The first one that returns false
stops the processing of the remaining functions. 返回
false
的第一个将停止其余功能的处理。 The onclick
in the generated HTML will be as follows: 生成的HTML中的
onclick
将如下所示:
onclick="PrimeFaces.bcn(this, event, [function(event){return confirm('confirm?')},function(event){PrimeFaces.ab({...});return false;}]);"
You could use the <p:confirm>
inside <p:menuitem>
to do the confirmation. 您可以使用
<p:menuitem>
的<p:confirm>
进行确认。
EDIT : just remove the return
before the confirm() and all should work as expected also. 编辑 :只需删除confirm()之前的
return
,并且所有内容也应按预期工作。
You could try to delegate your <p:menuitem>
actionListener into a <p:commandButton>
. 您可以尝试将
<p:menuitem>
actionListener委托给<p:commandButton>
。 Then nest a <p:confirm>
inside the <p:commandButton>
for your confirmation message. 然后将
<p:confirm>
嵌套在<p:commandButton>
以显示确认消息。 Then trigger your command button via the onclick
attribute of <p:menuitem>
. 然后通过
<p:menuitem>
的onclick
属性触发命令按钮。
Using confirm() without return does not resolve the problem.
使用confirm()不返回不能解决问题。 Instead, it submits the form even when the confirmation dialog is cancelled.
相反,即使取消了确认对话框,它也会提交表单。 I want to know if there is any workaround using onclick event before using
我想知道是否在使用onclick事件之前有任何解决方法
You shall reset values on cancel button in the confirmation dialog or you shall fetch new data at page loading. 您应在确认对话框中的“取消”按钮上重置值,或者应在页面加载时获取新数据。
Use ap:confirmDialog: 使用ap:confirmDialog:
<p:menuitem icon="fa fa-fw fa-remove"
title="#{message.global_remove}"
value="#{message.global_remove}"
onclick="PF('confirm').show();"/>
<p:confirmDialog widgetVar="confirm"
message="#{message.component_list_confirm_deletion}">
<p:commandButton value="Confirm"
oncomplete="PF('confirm').hide();" update=":component_list"
action="#{componentListMB.delete(cp.id)}" />
<p:commandButton value="Cancel"
onclick="PF('confirm').hide();" /> </p:confirmDialog>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.