开发者

Mutual exclusive jsf selectOneMenu items

Suppose I have two selectOneMenu tags bound to two different开发者_开发知识库 lists (let's say Products and Departments).

I want to prevent choosing some Departments depending on the Product selected and vice versa. Where could I put that logic?


Not sure if this is the way you want to go about for this. But you can load the Department list base on the selection of Product. To do this you need to send Ajax request to update the Department list when you select a Product. To illustrate this, I will use PrimeFaces to send out ajax request

<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>

Here is my bean

@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.
        }
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜