0

I am working with jsf and Primefaces components. I need do clic the commandButton depending on an input value. This is my html code:

   <h:form id="mainform">
        <p:panel id="panelform" header="Email Extractor"  >        
            <h:panelGrid id="formulaire" columns="2">  
                <h:panelGroup id="formblock" layout="block" >
                    <p:panelGrid columns="2">                                                                  
                        <p:outputLabel for="Emailsnbr" value="Enter the emails'number:" />  
                        <p:inputText  id="Emailsnbr"   type="number"  requiredMessage="Emails number is required"
                                      value="#{mailMB.number}" required="true" label="Emailsnbr">    
                        </p:inputText> 
                    </p:panelGrid>
                </h:panelGroup>
            </h:panelGrid>  
          <c:if  test="#{mailMB.number gt 10}"> 
                <p:commandButton   value="Extract" style="width: 8%;height: 100%" update="tableemails, :confirmPurchase, :mainform" id="extractbutton" ajax="true" widgetVar="ButtonExtract"
                                 actionListener="#{mailMB.searchEmails()}"
                                  oncomplete="if (args &amp;&amp; !args.validationFailed) purchase.show();"/>                          
         </c:if> 
            <c:if  test="#{mailMB.number lt 11}"> 
           <p:commandButton value="Extract" style="width: 8%;height: 100%" update="tableemails, :confirmPurchaseTest, :mainform" id="extractbuttonTest" ajax="true" widgetVar="ButtonExtract"
                                 actionListener="#{mailMB.searchEmails()}" 
                                  oncomplete=" if (args &amp;&amp; !args.validationFailed) emailsList.show();"/>                          
          </c:if>        
            </p:dialog>
        </p:panel>  
    </h:form>  

the commandButton in the tag <c:if test="#{mailMB.number lt 11}"> is always invoked and never the one in the tag <c:if test="#{mailMB.number gt 10}"> either i enter 9 or 15 as a value of the number input. Any explanation and help how make this work?

junior developper
  • 448
  • 2
  • 19
  • 40

2 Answers2

3

It looks like the problem are the c:if tags. JSTL tags are evaluated during view build time, at this time the bean values are not available and so your mailMB.number is always lesser then 11. Have a look at this question to get some details about that.

One solution is to use some JSF component with rendered attribute instead.

Something like this:

<h:panelGroup rendered="#{mailMB.number gt 10}">
  // first commandButton
</h:panelGroup>

<h:panelGroup rendered="#{mailMB.number lt 11}">
  // second commandButton
</h:panelGroup>

should work.

Community
  • 1
  • 1
unwichtich
  • 13,712
  • 4
  • 53
  • 66
1

As per the comments: don't create two buttons, use one and grab the value from the page in javascript itself:

<p:commandButton value="Extract" style="width: 8%;height: 100%" 
    update="tableemails, :confirmPurchaseTest, :mainform" 
    id="extractbutton" ajax="true" 
    widgetVar="ButtonExtract"
    actionListener="#{mailMB.searchEmails()}" 
    oncomplete="showResultPanel(args);" />

<h:outputScript>
    function showResultPanel(args) {
      if (args && !args.validationFailed) {
        var $input = jQuery("input[id$='Emailsnbr']");
        $input.val() > 10 ? purchase.show() : emailsList.show();
      }
    }
</h:outputScript>

You can probably collapse that into the oncomplete attribute, I just wanted to make it clearer.

mabi
  • 5,279
  • 2
  • 43
  • 78