ajax在下拉列表返回null时不起作用

ajax在下拉列表返回null时不起作用

本文介绍了f:ajax在下拉列表返回null时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该下拉列表与转换器关联.下拉列表值更改时,ajax起作用. 但是如果从下拉列表中选择-Select-"项,则ajax不会调用侦听器.我找不到任何好的解决方案.代码如下.

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>

转换器:

@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?

推荐答案

我找到了解决上述问题的棘手解决方案.我想为你分享.我放置了h:commandButton并使用jquery单击该事件.因此,当用户从下拉菜单中选择-- Select --项时,它将执行必要的功能.

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>

这篇关于f:ajax在下拉列表返回null时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:21