假设我有两个selectOneMenu标签绑定到两个不同的列表(例如,产品和部门)。
我想防止根据所选产品选择某些部门,反之亦然。我该把逻辑放在哪里?
最佳答案
不知道这是否是您要解决的方法。但是您可以根据产品的选择加载部门列表。为此,您需要在选择产品时发送Ajax请求以更新“部门”列表。为了说明这一点,我将使用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.
}
}
}
关于java - 互斥的jsf selectOneMenu项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5419715/