我想改变SelectItem[]阵列中的第二SelectOneMenu,如果该值中的第一个改变。可能?

有帮助吗?

解决方案

我想通了这一点,但我用的RichFaces的AJAX功能,不仅JSF。刚才添加的标签给我的第一selectOneMenu用于,和它的作品:)

<a4j:support event="onchange" action="#{bean.onChange}"
             reRender="otherSelectOneMenuID"/>

感谢反正响应!

其他提示

如果您的值变化监听结合第一selectOneMenu用于应该是可能的。

获取从ValueChangeEvent的新值,并相应地更新列表。然后JSF页面应显示更新后的列表。

希望是有道理的!

那么我使用A4J和它的工作。

<code>
//JSF
<h:outputLabel value="First selectOneMenu: "/>
<h:selectOneMenu value="#{yourBackingBean.selectedItem}">
<f:converter converterId="defaultConverter"/>
<f:selectItem id="df01" itemLabel="Item01" itemValue="1" />
<f:selectItem id="df02" itemLabel="Item02" itemValue="2" />
<f:selectItem id="df03" itemLabel="Item03" itemValue="3" />
<a4j:support event="onchange" reRender="secondSelectOneMenu"/> //secondSelectOneMenu is the id of the dropdown you want to change
</h:selectOneMenu>


<h:outputLabel value="Second selectOneMenu: "/>
<h:selectOneMenu value="#{yourBackingBean.attributeToStoreSelectedValue}" id="secondSelectOneMenu">
<f:converter converterId="defaultConverter"/>
<f:selectItem id="df00" itemLabel="Select" itemValue="0" /> //Default value
<f:selectItems value="#{yourBackingBean.returnByChoice}" />
</h:selectOneMenu>


//Converter

public class DefaultConverter implements Converter {
public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
    return value;
}

public String getAsString(FacesContext ctx, UIComponent component, Object value) {
    String label = "";
    if (value != null) {
        label = value.toString();
    }
    return label;
}
}

//Backing Bean Sample
public List<SelectItem> returnByChoice() { //it must return a list of SelectItems so it can be displayed on the jsf page
   String id = (String) getSelectedItem(); //this is the value chosen from the first dropDownMenu wich selectedItem is the attribute onthe binding of the first dropDownMenu.
   ArrayList<SelectItem> arrItems = new ArrayList<SelectItem>();
   if (id != null) {

            List<YourClass> yourObjectList = yourDao.findAllItemsFromType(new Integer(id));

         Iterator<YourClass> iterator = yourObjectList.iterator();
         String tempName = "";
         String tempId = "";
         YourClass tempYourObject = null;

        while (iterator.hasNext()) {
           tempYourObject = iterator.next();
           tempId = String.valueOf(tempYourObject.getId());
           tempName = tempYourObject.getName();
           arrItems.add(new SelectItem(tempId, tempName));
        }
    }
    return arrProfiles;
}
</code>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top