2014-03-25 99 views
-3
<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顯示爲空。

+0

你可能想使用一個更具描述性標題你的問題,這要求人們downvote它... – Novocaine

+0

是否定義下拉作爲後臺Bean的應用程序中的任何地方? – Adarsh

回答

0

根據您所提供的任何代碼,

  1. 豆必須聲明爲管理bean或者使用個XML或註解。
  2. 更改監聽器方法聲明

    public void stateChange() { DistrictList = DBConnector.DistrictList(SelectedState); }

  3. 檢查是否DBConnector.DistrictList(SelectedState)返回任何東西。同時檢查SelectedState是否具有您在下拉列表中選擇的正確值。

另外,最好在你的類中使用小寫變量。例如。 selectedState代替SelectedState。

+0

是的它確實..區下拉列表正在填充.. –

+0

如果它正在填充,那麼是什麼問題? – Adarsh

+0

支持的區域的選定值顯示爲null ..不會調用selectedDistrict的setter。 。 –

相關問題