我希望根据之前<p:selectOneMenu>
的选定值在<ui:repeat>
内显示多个<f:selectItems>
组件,直到到达树的叶子。
E.g。 <p:selectOneMenu>
中的第一个国家/地区列表。现在我选择一个国家说巴基斯坦。现在在<ui:repeat>
内,将有第二个<p:selectOneMenu>
组件与巴基斯坦城市。现在我选择一个城市,然后应该有第三个<p:selectOneMenu>
城市的部门,依此类推,直到没有从数据库中找到记录。
我怎样才能做到这一点?
答案 0 :(得分:0)
<ui:repeat>
是不可或缺的。首先,DB中没有无限数量的表。只是有条件地根据数据库表中的可用选择项呈现后代下拉列表。
<p:selectOneMenu value="#{bean.country}">
<f:selectItem itemValue="#{null}" itemLabel="--select country--" />
<f:selectItems value="#{bean.countries}" />
<p:ajax listener="#{bean.changeCountry}" update="cities" />
</p:selectOneMenu>
<h:panelGroup id="cities">
<p:selectOneMenu value="#{bean.city}" rendered="#{not empty bean.cities}">
<f:selectItem itemValue="#{null}" itemLabel="--select city--" />
<f:selectItems value="#{bean.cities}" />
<p:ajax listener="#{bean.changeCity}" update="sectors" />
</p:selectOneMenu>
</h:panelGroup>
<h:panelGroup id="sectors">
<p:selectOneMenu value="#{bean.sector}" rendered="#{not empty bean.sectors}">
<f:selectItem itemValue="#{null}" itemLabel="--select sector--" />
<f:selectItems value="#{bean.sectors}" />
</p:selectOneMenu>
</h:panelGroup>
在@ViewScoped
bean中使用类似的东西:
private List<String> countries;
private String country;
private List<String> cities;
private String city;
private List<String> sectors;
private String sector;
@EJB
private LocationService locationService;
@PostConstruct
public void init() {
countries = locationService.getCountries();
}
public void changeCountry() {
cities = locationService.getCities(country);
city = null;
sectors = null;
}
public void changeCity() {
sectors = locationService.getSectors(city);
sector = null;
}
当然,您可以代替String
使用Country
,City
和Sector
个实体以及(通用)转换器。