Tin*_*iny 8 jsf selectonemenu primefaces
在MySQL数据库中有三个表category,sub_category和brand(制造商)其中category是其余的父,即sub_category和brand.我希望,菜单之间的关系可以根据表关系更清晰.
所有三个<p:selectOneMenu>都被放置<p:dataTable>在三个相应的列中,如下所示<p:column>.我忽略<p:column>,<p:cellEditor>,<f:facet name="output">,<f:facet name="input">,<p:rowEditor>和所有这些滋扰赘述.
row对应于这是一个JPA管理实体product在这种情况下,通过指定var="row"在<p:dataTable>关联的.
这是实际的问号:当一个项目(第一个)null的值categoryList被选择(母公司),其子清单subCategoryList和brandList应该休息了个空.
分类清单:
<p:selectOneMenu id="categoryList"
value="#{row.category}"
required="#{param['javax.faces.source'] ne component.clientId}">
<f:selectItem itemLabel="Select"
itemValue="#{null}"/>
<!-- When this item is selected, its children below should be reset to empty. -->
<f:selectItems var="category"
value="#{productManagedBean.categories}"
itemLabel="Select"
itemValue="#{category}"/>
<p:ajax update="subCategoryList brandList"/>
<!-- The listener functionality is left incomplete here. -->
</p:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
子类别列表:
<p:selectOneMenu id="subCategoryList"
value="#{row.subCategory}">
<f:selectItem itemLabel="Select"
itemValue="#{null}"/>
<f:selectItems var="subCategory"
value="#{productManagedBean.getSubCategories(row.category)}"
itemLabel="#{subCategory.subCatName}"
itemValue="#{subCategory}"
rendered="true"/>
</p:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
品牌(制造商)清单:
<p:selectOneMenu id="brandList"
value="#{row.brand}">
<f:selectItem itemLabel="Select"
itemValue="#{null}"/>
<f:selectItems var="brand"
value="#{productManagedBean.getBrands(row.category)}"
itemLabel="#{brand.brandName}"
itemValue="#{brand}"
rendered="true"/>
</p:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
托管bean(在此问题的上下文中可以忽略惰性数据模型):
@Named
@ViewScoped
public class ProductManagedBean extends LazyDataModel<Product> implements Serializable {
@Inject
private Service service;
// Associated with <p:selectOneMenu id="categoryList">.
private List<Category> categories; // Getter & setter.
// These are merely helper maps to reduce possible database calls.
private Map<Category, List<SubCategory>> subCategoriesByCategory;
private Map<Category, List<Brand>> brandByCategory;
public ProductManagedBean() {}
@PostConstruct
private void init() {
// This can be application scoped somewhere else as per business requirement.
categories = service.getCatgeoryList();
subCategoriesByCategory = new HashMap<Category, List<SubCategory>>();
brandByCategory = new HashMap<Category, List<Brand>>();
}
// This method populates <f:selectItems> associated with <p:selectOneMenu id="brandList">.
public List<SubCategory> getSubCategories(Category category) {
// category is never null here unless something is broken deliberately.
if (category == null) {
return null;
}
List<SubCategory> subCategories = subCategoriesByCategory.get(category);
if (subCategories == null) {
subCategories = service.findSubCategoriesByCategoryId(category.getCatId());
subCategoriesByCategory.put(category, subCategories);
}
return subCategories;
}
// This method populates <f:selectItems> associated with <p:selectOneMenu id="brandList">.
public List<Brand> getBrands(Category category) {
// category is never null here unless something is broken deliberately.
if (category == null) {
return null;
}
List<Brand> brands = brandByCategory.get(category);
if (brands == null) {
brands = service.findBrandsByCategoryId(category.getCatId());
brandByCategory.put(category, brands);
}
return brands;
}
}
Run Code Online (Sandbox Code Playgroud)
在任何情况下,任何这些菜单中的选定值都不会提供给相应的辅助bean.它仅适用于由JPA(支持的模型value="#{row.category}",value="#{row.subCategory}"并value="#{row.brand}"分别).
►如何选择辅助bean,null选择父菜单中带有值(标记为"选择")的第一个项目,以便将其子列表重置为空?如果不可行,这应该以任何可行的方式发生.
我正在使用PrimeFaces 5.2 final(社区发布)和Mojarra 2.2.12.
除非在底层数据库表中存在空外键,否则不需要这样做,具体使用供应商特定ON DELETE SET NULL选项,允许每个(或某些)相应子行中包含可选父项.
就这一点而言,您需要确保使用参数<f:selectItem>调用 getter null。换句话说,#{row.category}必须是null。鉴于您要#{row.category}使用此答案中所示的模型,请根据 ap:dataTable 的每一行中的另一个 p:selectOneMenu 填充 p:selectOneMenu,最有可能如下所示,
@Transient
private Category category;
public Category getCategory() {
return (category == null && subCategory != null) ? subCategory.getCategory() : category;
}
Run Code Online (Sandbox Code Playgroud)
那么当存在 时,#{row.category}实际上永远不会存在。当现有数据条目呈现在视图中时就会出现这种情况。nullsubCategory
当瞬态属性显式设置为 null时,您基本上需要显式将subCategory( 和) 清空。与此同时,这一疏忽已在上述答案中得到修复。您的新方法应如下所示:brandcategorysetCategory()
public void setCategory(Category category) {
this.category = category;
if (category == null) {
subCategory = null;
brand = null;
}
}
Run Code Online (Sandbox Code Playgroud)
这样,意志getCategory()就会正确返回null,因此传入的#{row.category}也会正确返回。