[英]JSF ValueChangeEvent: how do I change the options on one SelectOneMenu based on my selection on a previous SelectOneMenu?
好的,我已經堅持了幾天(輕描淡寫)。
說我有一個selectOneMenu
<h:selectOneMenu id="selectFamily" valueChangeListener="#{menuBean.changeFamily}" onclick="submit()" immediate="true" > <f:selectItems id="familyNames" value="#{menuBean.familyNames}" /> </h:selectOneMenu>
我想基於在先前的selectOneMenu中選擇的選項來更改另一個selectOneMenu上的選項。
<h:selectOneMenu id="selectFunction" immediate="true"> <f:selectItems id="functions" value="#{menuBean.functions}" /> </h:selectOneMenu>
我該如何做?閱讀后,我對如何做有一些想法,但我似乎無法正確地做到這一點。 對不起,我將bean類轉儲到這里,希望有人可以指導我。
公共類MenuBean {
/** Creates a new instance of MenuBean */
public MenuBean() {
}
private SelectItem[] familyNames = {
new SelectItem((Integer) 1,"Operations"),
new SelectItem((Integer) 2, "Special"),
new SelectItem((Integer) 3, "Support")};
public SelectItem[] getFamilyNames() {
return familyNames;
}
private SelectItem[] functions = {new SelectItem("Select Function")};
private SelectItem[] operationsFunctions = {
new SelectItem("Air/Ocean Freight"),
new SelectItem("Customs"),
new SelectItem("Land Transport"),
new SelectItem("Logistics/SCM"),
new SelectItem("Rail Transport"),
new SelectItem("Special")
};
private SelectItem[] specialFunctions = {
new SelectItem("General Management"),
new SelectItem("Quality & Processes")
};
private SelectItem[] supportFunctions = {
new SelectItem("Finance/Controlling"),
new SelectItem("Human Resources"),
new SelectItem("ICT"),
new SelectItem("Legal Affairs"),
new SelectItem("Marketing/Public Relations"),
new SelectItem("Procurement"),
};
public SelectItem[] getFunctions(int n) {
if (n==1) {
functions = operationsFunctions;
} else if (n==2) {
functions = specialFunctions;
} else if (n==3) {
functions = supportFunctions;
}
return functions;
}
public void setFunctions(SelectItem[] function) {
this.functions = function;
}
public void changeFamily(ValueChangeEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
int value = (Integer) event.getNewValue();
setFunctions(getFunctions(value));
context.renderResponse();
}}
我認為這里的問題是getFunctions(int i)需要一個參數。 由於它是由下面的EL表達式調用的,因此它不應帶有參數。
<f:selectItems id="functions" value="#{menuBean.functions}" />
另外,ValueChangeEvent中的值需要轉換為字符串,而不是整數。 從您的問題中不確定是否已將selectItem包裝在表單中,否則,您也必須這樣做。 我能想到的最后一件事是backingBean的范圍,它需要在回發中生存下來。
我嘗試了以下示例,該示例的會話范圍為ManagedBean和JSF 1.2,並且可以正常工作:
<h:form>
<h:selectOneMenu id="selectFamily"
valueChangeListener="#{menuBean.changeFamily}"
immediate="true"
onchange="submit()">
<f:selectItems id="familyNames" value="#{menuBean.familyNames}"/>
</h:selectOneMenu>
<h:selectOneMenu id="selectFunction" immediate="true">
<f:selectItems id="functions" value="#{menuBean.functions}"/>
</h:selectOneMenu>
</h:form>
public class MenuBean {
private SelectItem[] familyNames = {
new SelectItem(1, "Operations"),
new SelectItem(2, "Special"),
new SelectItem(3, "Support")};
public SelectItem[] getFamilyNames() {
return familyNames;
}
private SelectItem[] functions = {new SelectItem("Select Function")};
private SelectItem[] operationsFunctions = {
new SelectItem("Air/Ocean Freight"),
new SelectItem("Customs"),
new SelectItem("Land Transport"),
new SelectItem("Logistics/SCM"),
new SelectItem("Rail Transport"),
new SelectItem("Special")
};
private SelectItem[] specialFunctions = {
new SelectItem("General Management"),
new SelectItem("Quality & Processes")
};
private SelectItem[] supportFunctions = {
new SelectItem("Finance/Controlling"),
new SelectItem("Human Resources"),
new SelectItem("ICT"),
new SelectItem("Legal Affairs"),
new SelectItem("Marketing/Public Relations"),
new SelectItem("Procurement"),
};
public SelectItem[] getFunctions() {
return functions;
}
private void switchSelectedFunctions(int n) {
if (n == 1) {
setFunctions(operationsFunctions);
} else if (n == 2) {
setFunctions(specialFunctions);
} else if (n == 3) {
setFunctions(supportFunctions);
}
}
public void setFunctions(SelectItem[] function) {
this.functions = function;
}
public void changeFamily(ValueChangeEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
int value = Integer.valueOf(((String) event.getNewValue()));
switchSelectedFunctions(value);
context.renderResponse();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.