相互排斥的jsf selectOneMenu项目

时间:2011-03-24 13:12:48

标签: java jsf

假设我有两个selectOneMenu标签绑定到两个不同的列表(比如说产品和部门)。

我希望根据所选产品禁止选择某些部门,反之亦然。我在哪里可以说出这个逻辑?

1 个答案:

答案 0 :(得分:2)

不确定这是否是你想要的方式。但您可以根据产品的选择加载部门列表。为此,您需要在选择产品时发送Ajax请求以更新Department列表。为了说明这一点,我将使用PrimeFaces发送ajax请求

<h:selectOneMenu id="product" value="#{bean.selectedProduct}">
    <f:selectItem itemLabel="Select Product" itemValue="" />
    <f:selectItems value="#{bean.productList}"/>
    <!-- here is where I send out ajax request -->
    <p:ajax listener="#{bean.loadDepartmentById}" event="change" update="department" />
</h:selectOneMenu>

<h:selectOneMenu id="department" value="#{bean.selectedDepartment}">
    <f:selectItem itemLabel="" itemValue="" />    
    <f:selectItems value="#{bean.departmentList}"/>    
</h:selectOneMenu>

这是我的豆子

@ManagedBean
@ViewScoped
public class bean{
    private List<SelectItem> productList = null;
    private List<SelectItem> departmentList = null;
    private Long selectedProduct = null;
    private Long selectedDepartment = null;

    @PostConstruct
    public void init(){
        //Pre load product list
        productList = new ArrayList<SelectItem>();
        productList.add(new SelectItem("value1", "label1"));  
        productList.add(new SelectItem("value2", "label2"));  
        productList.add(new SelectItem("value3", "label3"));  
        //The values are the keys passed to the selectItem property.  
        //The labels are those what you see on the menu.  
    }

    public void loadDepartmentById(){
        if(selectedProduct != null){
            //Load the appropriate list of department.
        }
    }

}