问题
Hello Everyone I would like to open a rich:popupPanel inside a conditional expression, actually what I'm trying is something like this:
onclick="#{searchBackingBean.showLoginPanel==true ? #{rich:component('loginPopup')}.show();':''}"
However I'm getting errors related to EL, how should I write this correctly?. Thanks a lot.
回答1:
You may not nest EL expressions. I suggest to rewrite the expression as follows so that the condition is delegated to JavaScript:
onclick="if (#{searchBackingBean.showLoginPanel}) #{rich:component('loginPopup')}.show();"
(please note that I removed the superfluous == true
comparison because this makes no sense as the method returns/prints a boolean value already)
Note that this only works in <rich:xxx>
and <a4j:xxx>
components as they have enhanced the on*
attributes to re-evaluate the EL expression on postbacks. The standard JSF <h:xxx>
components doesn't do that. You'd need to workaround it with the rendered
attribute:
<h:commandButton>
<f:ajax render="script" />
</h:commandButton>
<h:panelGroup id="script">
<h:panelGroup rendered="#{searchBackingBean.showLoginPanel}">
<script>#{rich:component('loginPopup')}.show();</script>
</h:panelGroup>
</h:panelGroup>
回答2:
What I do is put the panel inside a panelGroup or some other container that can be rendered. Then set the rendered flag of that container depending on the condition and call render of that container when it possibly has to be shown.
something like this:
<a4j:commandButton ... render="popup"/>
<a4j:outputPanel id="popup" rendered="#{searchBackingBean.showLoginPanel}">
<script>
#{rich:component('loginPopup')}.show();
</script>
</a4j:outputPanel>
MAG, Milo van der Zee
来源:https://stackoverflow.com/questions/8957560/conditional-invocation-of-richpopup-panel