f:ajax not working when dropdown return null

笑着哭i 提交于 2019-12-12 04:49:33

问题


The dropdown is linked with converter. The ajax works when dropdown value is changed. But in case of selection of "-- Select --" item from dropdown, ajax does not invoke listener. I could not find any good solution. Code is given below.

<h:selectOneMenu value="#{cc.attrs.beanProperty}" converter="myConverter" >
  <f:selectItem itemValue="#{null}" itemLabel="-- Select --" />
  <f:selectItems value="#{cc.attrs.list}" var="item" itemValue="#{item}" itemLabel="#{item.name}" />
  <f:ajax render=":form1" listener="#{myBean.listener}"/>
</h:selectOneMenu>

Converter:

@FacesConverter(value = "myConverter")
public class VendorConverter implements Converter {

    @Inject ObjectDAO dao;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if(value == null || value.contains("Select")){
            return null;            
        }
        return dao.find(Integer.valueOf(value));
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if(value == null) {
            return null;
        }                  
        return ((MyObject) value).getId().toString();
    }    
}

Could anybody point the solution?


回答1:


Since the f:ajax is not being triggered by the itemValue="#{null}" or the noSelectionOption="true" (which is better anyway than the null usage) I would recommend the following, prevent from the user to go back to the -- Select -- value after he already selected something

(unless you really want the user to go back to the -- Select -- option after he already picked some other option)

1) Replace

<f:selectItem itemValue="#{null}" itemLabel="-- Select --" />

with

<f:selectItem noSelectionOption="true" itemLabel="-- Select --" />

2) Add the use of itemDisabled like this

<f:selectItem itemDisabled="#{not empty cc.attrs.beanProperty}" 
    noSelectionOption="true" itemLabel="-- Select --" />

or instead of the itemDisabled="#{not empty cc.attrs.beanProperty}" just use the <h:selectOneMenu hideNoSelectionOption="true"> depend on your preferences.


Also, note that in order to find out whats wrong with your code you can try using placing a <h:message or <h:messages in your page




回答2:


I found a tricky solution for the said problem. I would like to share for you. I have place a h:commandButton and use jquery to click the event. So when user select the -- Select -- Item from dropdown it executes the necessary function.

<h:selectOneMenu value="#{cc.attrs.beanProperty}" converter="myConverter" onchange="selectedItem(this)" >
  <f:selectItem itemValue="#{null}" itemLabel="-- Select --" />
  <f:selectItems value="#{cc.attrs.list}" var="item" itemValue="#{item}" itemLabel="#{item.name}" />
  <f:ajax render=":form1" listener="#{myBean.listener}"/>
</h:selectOneMenu>

<h:commandButton class="reset" action="#{mybean.reset}" style="display: none;">            
    <f:ajax render="#{cc.attrs.renderComponent}"/>
</h:commandButton>


<script>
     function selectedItem(that) {                  
         if($(that).val() === ""){
               $(".reset").click();
         }
     }
</script>


来源:https://stackoverflow.com/questions/26458282/fajax-not-working-when-dropdown-return-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!