2013-03-29 73 views
2

我正在使用JSF2.0。我有三個下拉菜單:如何管理下拉菜單之間的依賴關係?

  1. masterDropDown。
  2. childDropDownA
  3. childDropDownB

需要強制執行的業務規則是:

  1. 每當masterDropDown的變化,我需要填充childDropDownAchildDropDownB
  2. childDropDownAchildDropDownB的內容是相同除了使得當用戶從childDropDownAchildDropDownB選擇東西應具有除所述一個由用戶在childDropDownA選擇其舊的內容。當用戶首先選擇childDropDownB時,也適用相同的規則。

在JSF中實現這一點最簡潔的方法是什麼?

回答

4

平原JSF 2:

<h:form> 
    <h:selectOneMenu id="ddlMaster" value="#{bean.ddlMasterSelected}"> 
     <f:listItems value="#{bean.ddlMasterData}" /> 
     <f:ajax listener="#{bean.fillChildren}" render="ddlChildA ddlChildB" /> 
    </h:selectOneMenu> 
    <h:selectOneMenu id="ddlChildA" value="#{bean.ddlChildASelected}"> 
     <f:listItems value="#{bean.ddlChildAData}" /> 
     <f:ajax listener="#{bean.refillChildB}" render="ddlChildB" /> 
    </h:selectOneMenu> 
    <h:selectOneMenu id="ddlChildB" value="#{bean.ddlChildBSelected}"> 
     <f:listItems value="#{bean.ddlChildBData}" /> 
     <f:ajax listener="#{bean.refillChildA}" render="ddlChildA" /> 
    </h:selectOneMenu> 
</h:form> 

託管豆

@ManagedBean 
@ViewScoped 
public class Bean { 
    private Map<String, String> ddlMasterData; 
    private Map<String, String> ddlChildAData; 
    private Map<String, String> ddlChildBData; 
    private String ddlMasterSelected; 
    private String ddlChildASelected; 
    private String ddlChildBSelected; 

    @PostConstruct 
    public void init() { 
     //fill initial values for the drop down lists... 
     //this is a raw idea, you must refine it 
     DDLService ddlService = new DDLService(); 
     ddlMasterData = ddlService.getDDLMasterData(); 
     //empty data for children ddls 
     ddlChildAData = new LinkedHashMap<String, String>(); 
     ddlChildBData = new LinkedHashMap<String, String>(); 
    } 

    public void refillChildB(AjaxBehaviorEvent event) { 
     //filling the children ddls 
     DDLService ddlService = new DDLService(); 
     ddlChildASelected = ddlService.getDDLChildData(); 
     //same method to fill child data on ddlChildBSelected because is same data 
     ddlChildBSelected = ddlService.getDDLChildData(); 
    } 

    public void fillChildren(AjaxBehaviorEvent event) { 
     //when the user selects something from childDropDownA 
     //childDropDownB should have its old contents 
     //I assume it must be cleared, you can change this behavior though 
     ddlChildBData = new LinkedHashMap<String, String>(); 
    } 

    public void refillChildA(AjaxBehaviorEvent event) { 
     //The same rule applies when the user selects childDropDownB first 
     //I assume it must be cleared, you can change this behavior though 
     ddlChildAData = new LinkedHashMap<String, String>(); 
    } 
} 
+0

什麼* *呈現在''工作? – Inquisitive

+1

@Inquisitive它會更新'ddlChildA'組件的內容,您應該在ajax調用完成時提供您想要更新的組件的id。 –

+0

@好奇你可以找到更多關於''的信息[這裏](http://www.jsftoolbox.com/documentation/help/12-TagReference/core/f_ajax.html) –