Javascript 仅当这些消息存在时,如何在带有 requiredMessages 的primefaces 中显示弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10797541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show a popup in primefaces with the requiredMessages, only if these messages exist?
提问by AdSsa
I want to show a popup with the requiredMessages of some inputText fields when I click on a submit button. But just only in case of there are those messages. I have tried with bean variable and javascript on the oncomplete tag, but I'm not able to make it work properly. If I put visible="true" in p:dialog, the popup is always displayed, although I try to control it from the commandButton. Now, I have this, but the popup is never displayed:
当我单击提交按钮时,我想显示一个带有某些 inputText 字段的 requiredMessages 的弹出窗口。但只有在有这些消息的情况下。我已经尝试在 oncomplete 标签上使用 bean 变量和 javascript,但我无法使其正常工作。如果我在 p:dialog 中放置了 visible="true",弹出窗口总是会显示,尽管我尝试从 commandButton 控制它。现在,我有了这个,但从未显示弹出窗口:
<h:inputText id="Scheme"
required="true"
requiredMessage="Required.">
</h:inputText>
<h:commandButton id="submitModify" value="#{msg['systemdetail.modify']}"
action="#{sistem.modify}"
oncomplete="if (#{facesContext.maximumSeverity != null}) {dlg1.show();}">
</h:commandButton>
<p:dialog id="popup"
style="text-align:center"
widgetVar="dlg1"
modal="true">
<h:messages layout="table"/>
</p:dialog>
How can I do this? Thanks in advance.
我怎样才能做到这一点?提前致谢。
回答by BalusC
Standard JSF and PrimeFaces does not support request based EL evaluation in on*
attributes. RichFaces is the only who supports that. Besides, the standard JSF <h:commandButton>
does not have an oncomplete
attribute at all. You're probably confusing with PrimeFaces <p:commandButton>
标准 JSF 和 PrimeFaces 不支持在on*
属性中基于请求的 EL 评估。RichFaces 是唯一支持这一点的人。此外,标准 JSF<h:commandButton>
根本没有oncomplete
属性。您可能对 PrimeFaces 感到困惑<p:commandButton>
There are several ways to achieve this:
有几种方法可以实现这一点:
Check the condition in the
visible
attribute of the<p:dialog>
instead.<p:dialog visible="#{not empty facesContext.messageList}">
or if you want to show validation messages only instead of all messages
<p:dialog visible="#{facesContext.validationFailed}">
Use PrimeFaces
<p:commandButton>
instead, the PrimeFaces JS API supports the#{facesContext.validationFailed}
condition through theargs
object as well:<p:commandButton ... oncomplete="if (args.validationFailed) dlg1.show()" />
检查代替的
visible
属性中的条件<p:dialog>
。<p:dialog visible="#{not empty facesContext.messageList}">
或者如果您只想显示验证消息而不是所有消息
<p:dialog visible="#{facesContext.validationFailed}">
改用 PrimeFaces
<p:commandButton>
,PrimeFaces JS API 也#{facesContext.validationFailed}
通过args
对象支持条件:<p:commandButton ... oncomplete="if (args.validationFailed) dlg1.show()" />
回答by mkienenb
If you need to check for what kind of messages, here is a way that I made work with primefaces. Since primefaces oncomplete is called after update, by updating the component holding the javascript function, the javascript function can be rebuilt using the latest #facesContext.maximumSeverity} values before executed.
如果你需要检查什么样的消息,这是我使用primefaces的一种方法。由于primefaces oncomplete 在update 之后被调用,通过更新持有javascript 函数的组件,javascript 函数可以在执行前使用最新的#facesContext.maximumSeverity} 值重建。
<p:commandButton
oncomplete="executeAfterUpdate()"
update="updatedBeforeOnComplete"/>
<h:panelGroup id="updatedBeforeOnComplete">
<script language="JavaScript" type="text/javascript">
//
function executeAfterUpdate(){
if (#{facesContext.maximumSeverity==null
or facesContext.maximumSeverity.ordinal=='1'})
{
// your code to execute here
someDialog.show();
}
}
//
</script>
</h:panelGroup>