最初の配列で値が変更された場合、SelectItem[]
2番目の配列を変更したい。SelectOneMenu
それは可能ですか?
質問する
6686 次
3 に答える
0
さて、私は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>
于 2010-06-08T17:38:09.830 に答える