<p:outputLabel id="state" value="State Name" />
<p:selectOneMenu id="statemenu" style="width:300px;"
value="#{DropDown.selectedState}">
<f:selectItem itemLabel="Select One" />
<f:selectItems value="#{DropDown.stateList}" />
<p:ajax process="@this" listener="#{DropDown.stateChange}"
update="dist" partialSubmit="true" />
</p:selectOneMenu>
<br />
<p:outputLabel value="District" />
<p:selectOneMenu id="dist" style="width:300px;"
value="#{DropDown.selectedDistrict}" immediate="true">
<f:selectItem itemLabel="Select One" />
<f:selectItems value="#{DropDown.districtList}" />
<p:ajax process="@this" listener="#{DropDown.setDisrict}"
update="@none" partialSubmit="true" />
</p:selectOneMenu>
這裏有兩個下拉菜單。區的價值是根據國家的變化而變化的。問題是區的價值被返回爲空。即使在選擇一個值之後。根據第一個下拉菜單中的選擇更改第二個下拉值
<p:commandButton id="btnSubmit" value="Submit" type="submit"
process="statemenu,dist,@this"
actionListener="#{DropDown.setVisiblity}"
action="#{DropDown.getValues}" title="Submit"
update=":form:render" partialSubmit="true"
style='font-family: Baskerville, "Baskerville Old Face",
"Hoefler Text", Garamond, "Times New Roman", serif; font-size: 14px;
font-weight: normal'>
<f:ajax execute="@this" render=":form:render" />
</p:commandButton>
這裏是支持bean:
public class DropDown implements Serializable {
private ArrayList<String> StateList;
private ArrayList<String> DistrictList;
// To store the values of selected state and district from the drop down
public String SelectedState;
public String SelectedDistrict;
public static String State;
public static String District;
public DropDown() {
StateList = new ArrayList<String>();
StateList = DBConnector.StateList();
}
public void setSelectedDistrict(String selectedDistrict) {
District = selectedDistrict;
this.SelectedDistrict = selectedDistrict;
}
// Method to update the district list when the state is changed
public void stateChange(AjaxBehaviorEvent event) {
DistrictList = DBConnector.DistrictList(SelectedState);
}
// Method to set the value of district for the datatable
public void setDisrict(AjaxBehaviorEvent event) {
System.out.println("District called" + " " + SelectedDistrict);
}
public void getValues() {
System.out.println(State);
System.out.println(District);
/*
* System.out.println(SelectedState);
* System.out.println(SelectedDistrict);
*/
}
public void setVisiblity(ActionEvent event) {
DataTable.Filter = false;
DataTable.isLoaded = true;
}
// All the getters and setter are exclude
}
只要我從statemenu選擇狀態。 dist菜單中的值應該由ajax更新,當我提交值時,selectedstate和selectedDistrict都應該反映選定的值。
這是行不通的。所選狀態確實反映所選值。但是selectdistrict顯示爲空。
你可能想使用一個更具描述性標題你的問題,這要求人們downvote它... – Novocaine
是否定義下拉作爲後臺Bean的應用程序中的任何地方? – Adarsh