Disable/enable a JSF input component depending on value of another input component

后端 未结 2 667
你的背包
你的背包 2020-12-19 11:41

I have two radio buttons:


    
    

        
相关标签:
2条回答
  • 2020-12-19 12:29

    Just let the target input component's disabled attribute check the value of the source input and use <f:ajax> in the source component to update the target component. It will cause the disabled attribute to be re-evaluated. No need for a value change listener nor an additional property.

    <h:selectOneRadio value="#{bean.choice}">
        <f:selectItem itemValue="yes" itemLabel="YES" />
        <f:selectItem itemValue="no" itemLabel="NO" />
        <f:ajax render="calendar" />
    </h:selectOneRadio>
    
    <p:calendar id="calendar" value="#{bean.date}" disabled="#{bean.choice eq 'no'}" />
    

    See also:

    • Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
    • When to use valueChangeListener or f:ajax listener?
    0 讨论(0)
  • 2020-12-19 12:31

    You can have method in your myBean which is a valueChangeListener event.

       private boolean caldisabled;  // with getter and setter
    
       public void checkSelectedVal(ValueChangeEvent event){
    
          String selectedVal=event.getNewValue().toString();
          if("NO".equalsIgnoreCase(selectedVal)){
             caldisabled=true;
          } else if("YES".equalsIgnoreCase(selectedVal)){
            caldisabled=false;
          }
    }
    

    And in your view in primefaces calendar component set disabled attribute

    <h:selectOneRadio value="#{myBean.yesNo}" valueChangeListener="#{mybean.checkSelectedVal}">
        <f:selectItem itemValue="yes" itemLabel="YES" />
        <f:selectItem itemValue="no" itemLabel="NO" />
        <f:ajax event="click" execute="@this" render="mycal"/>
    </h:selectOneRadio>
    
    <p:calendar id="mycal" value="#{myBean.date}" disabled="#{myBean.caldisabled}"/>
    

    And there should be another way to do this. I think as this calendar component is Jquery datepicker you should be able to do it using scripting alone with no need to go to bean and make ajax call

    0 讨论(0)
提交回复
热议问题