- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个<p:dataTable>
带有惰性负载。在其中两列中,有一个 <p:selectOneMenu>
在他们每个人中。
第一列包含国家/地区列表,第二列包含数据库中的州列表。
我希望第二个菜单(包含州列表的菜单)仅显示数据表的每行中与中第一个菜单中的国家/地区相对应的州数据表的每一行。
在编辑模式下,当菜单中的国家/地区发生更改时,与该国家/地区对应的州应该填充到当前行的菜单中。
如何在数据表的每一行中加载与其国家/地区相对应的州列表?
<小时/>数据表中的这两列不完整,因为我不知道如何实现这一点。
<p:column>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{row.state.country.countryName}"/>
</f:facet>
<f:facet name="input">
<p:selectOneMenu value="#{row.state.country}">
<f:selectItems var="country"
value="#{cityBean.selectedCountries}"
itemLabel="#{country.countryName}"
itemValue="#{country}"/>
<p:ajax update="states" listener="#{cityBean.getStates}"/>
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{row.state.stateName}"/>
</f:facet>
<f:facet name="input">
<p:selectOneMenu id="states">
<f:selectItems var="state"
value="#{cityBean.selectedStates}"
itemLabel="#{state.stateName}"
itemValue="#{state}"/>
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
cityBean.selectedCountries
检索除 cityBean.selectedStates
以外的所有必需国家/地区还从数据库中检索所有州,这是不必要的,应修改为仅检索另一个菜单中与其国家/地区相对应的州。
我该如何继续?
最佳答案
虽然您最初的解决方案有效,但实际上效率很低。这种方法基本上要求将 Country
和 State
表(即使有循环引用)的整个对象图完全加载到每个 JSF View 或 session 的 Java 内存中,即使当您仅同时使用例如150 个国家/地区中的 5 个(因此理论上 5 个国家列表就足够了,而不是 150 个国家列表)。
我不完全了解您的功能和技术要求。也许您实际上同时使用了所有这 150 个国家/地区。也许您有许多页面需要所有(至少“许多”)国家和州。也许您拥有最先进的服务器硬件和充足的内存,以便可以轻松地在内存中的所有 JSF View 和 HTTP session 上复制所有国家/地区和州。
如果情况并非如此,那么不急切地获取每个国家/地区的州列表将是有益的(即 @OneToMany(fetch=LAZY)
应该是用于Country#states
和State#cities
)。鉴于国家和州列表(可能)是静态数据,一年中更改次数很少,至少足以仅在每次部署的基础上进行更改,最好将它们存储在应用程序范围的 bean 中,以便跨应用程序重复使用所有 View 和 session ,而不是在每个 JSF View 或 HTTP session 中重复。
在继续回答之前,我想指出您的代码中存在逻辑错误。鉴于您正在编辑城市列表,因此 #{row}
本质上是 #{city}
,通过州引用国家/地区会很奇怪如下拉输入值中的 #{city.state.country}
所示。虽然这可能适用于显示,但不适用于编辑/保存。基本上,您是在每个州而不是每个城市的基础上更改国家/地区。当前选择的州将获得新的国家/地区,而不是当前迭代的城市。这一变化将反射(reflect)在该州的所有城市!
如果您想继续使用此数据模型,这确实不是一件小事。理想情况下,您希望在 City
上有一个单独的(虚拟)Country
属性,以便更改不会影响城市的 State
属性。您可以将其设置为 @Transient
,这样 JPA 默认情况下不会将其视为 @Column
。
@Transient // This is already saved via City#state#country.
private Country country;
public Country getCountry() {
return (country == null && state != null) ? state.getCountry() : country;
}
public void setCountry(Country country) {
this.country = country;
if (country == null) {
state = null;
}
}
总而言之,您最终应该拥有这个(为简洁起见,省略了不相关/默认/明显的属性):
<p:dataTable value="#{someViewScopedBean.cities}" var="city">
...
<p:selectOneMenu id="country" value="#{city.country}">
<f:selectItems value="#{applicationBean.countries}" />
<p:ajax update="state" />
</p:selectOneMenu>
...
<p:selectOneMenu id="state" value="#{city.state}">
<f:selectItems value="#{applicationBean.getStates(city.country)}" />
</p:selectOneMenu>
...
</p:dataTable>
使用像这样的#{applicationBean}
:
@Named
@ApplicationScoped
public class ApplicationBean {
private List<Country> countries;
private Map<Country, List<State>> statesByCountry;
@EJB
private CountryService countryService;
@EJB
private StateService stateService;
@PostConstruct
public void init() {
countries = countryService.list();
statesByCountry = new HashMap<>();
}
public List<Country> getCountries() {
return countries;
}
public List<State> getStates(Country country) {
List<State> states = statesByCountry.get(country);
if (states == null) {
states = stateService.getByCountry(country);
statesByCountry.put(country, states);
}
return states;
}
}
(这是延迟加载方法;您也可以立即在 @PostConstruct
中获取它们,看看什么更适合您)
关于jsf - 填充 p :selectOneMenu based on another p:selectOneMenu in each row of a p:dataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17613029/
该插件的绝大多数文档表明您可以使用 对其进行初始化 $('#example').dataTable(); 但是http://www.datatables.net/examples/api/multi_
我有一个 SQL 查询,它获取一些数据(dbtable 表中的语言列)。查询使用 GROUP_CONCAT,因此一个单元格有多个结果,例如 "Ajax, jQuery, HTML, CSS". 我想要
我有一个由 jQuery DataTables 增强的动态表,它显示一个与此类似的自定义对象 example . JSON: { "data": [ { "name": "Ti
我有两个数据表。首先是 DataTable NameAddressPhones = new DataTable(); 具有三列姓名、地址和电话号码。但我只想要两列姓名和地址数据,所以我想将这些列(包含
有没有办法将“处理...”语言放在 DataTable 对象的顶部而不是垂直中间?如果我有一张长 table ,它会隐藏在页面之外,因为它的默认位置在中间。 $('#example').dataTab
当我们向 DataTables 添加自定义过滤器时: $.fn.dataTable.ext.search.push(function(settings, data, dataIndex) { ..
我可以更改 dataTables 中搜索文本字段的宽度吗? 我现在正在编写以下代码,但它不起作用。 $('#example').dataTable() .columnFilter(
使用 DataTables plugin 时 如何使分页和 Showing 1 to 8 of 8 entries 出现在顶部而不是底部? 谢谢 最佳答案 每个数据表控件都以唯一的 id 命名计划之后
Angular 版本:6.0.4 ~ 节点版本:10.4.1 ~ NPM 版本:6.1.0 我看到这个问题被问了很多次,但没有回答。 关注这些后instructions to install angu
当单击外部按钮时,我正在尝试使用 DataTable(新版本)修改单元格值。我现在有以下内容: $(document).on('click', '.dropdown-menu li a', funct
引用:http://datatables.net/reference/api/page.info() 我正在尝试获取 ajax POST 生成的 jQuery DataTable 的当前分页信息。 使
以下对我有用: $('#datatable').on('page.dt', function() { alert("changed"); }); 每当我更改页面时,都会显示警报。但是如果我想警
我有一个 HTML 表,我在其中应用了 DataTables 函数。我使用表的第一行并将"template"类应用为我的模板行。然后选择此格式并使用 JSON feed 填充表中的所有行。问题是 Da
我有一个由 DataTables 1.10 驱动的表。过滤已打开。当我在下面谈论“做搜索”时,我指的是使用该表的过滤功能。 问题描述 关闭 stateSave 一切正常。但是,当 stateSave
看看这个例子,http://www.datatables.net/examples/api/multi_filter_select.html ,它使用 DataTable API 中的 columns
我正在使用 jQuery DataTables 在 spring 4.x 应用程序中创建一个报表页面来呈现报表。 通过 Jackson 消息转换器解析后,服务器返回以下对象。 { "data"
我在刷新 Primesfaces 的延迟加载数据表时遇到了一些问题。我正在使用 3.0.M4。 使用过滤器在您的示例中使用三个类。还尝试了仅使用延迟加载的示例,但随后未在 Controller 中重新
在 Blue Prism (BP) 中,有一种叫做 Collection 的东西,它基本上是 C# 中的 DataTable。在 BP 中,您可以在集合中拥有集合。我的问题是,您可以在 C# 的 Da
我正在使用 Flutter DataTables 来显示购物车中的商品列表。现在我想编辑任何选定行的数量。有没有办法获取用户点击的行信息? 以下是我的DataTable的完整代码: class _Da
我有一个关于 primefaces 数据表组件的问题。我想将 DataTable 变量绑定(bind)到 p:dataTable,以便我能够从支持 bean 以编程方式操作第一个、行、rowsPerP
我是一名优秀的程序员,十分优秀!