値が別のに変更された場合、SelectOneMenuにリストを変更するには?
-
20-09-2019 - |
質問
私は値が最初のものに変更された場合、第二SelectItem[]
にSelectOneMenu
配列を変更したいです。可能ということですか?
解決
JSFだけではない、私はこれを考え出したが、私はRichFacesののAJAX機能を使用していました。ちょうど私の最初の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>
所属していません StackOverflow