Primefaces从外部对话框更新rowExpansion内子数据表行且不关闭扩展行
你当前的问题根源是idCommandButtonUpdate按钮配置的update属性更新了整个父级产品表idTableDetailProduct,父表全局重绘后所有展开的rowExpansion都会自动收起,只需要把全局更新改成精准更新指定子行/子表即可解决。
第一步:修正子表基础配置
你当前子表idTableDetailProductChild的rowKey配置错误,子表遍历的对象是productChild,要改成子对象的唯一主键:
<p:dataTable id="idTableDetailProductChild" paginator="false" value="#{productDetail.productDTO.listProductsChild}" var="productChild" rowKey="#{productChild.id}" <!-- 修正这里,换成子产品的唯一ID字段 --> emptyMessage="#{diccBean.msg['product.productChildNotFound']}" scrollable="true">
第二步:后台新增变量存储编辑行的定位信息
在productController中新增两个字段存储需要更新的行标识,打开编辑对话框时赋值:
// 存储正在编辑的父级产品行ID(对应父表rowKey) private Long editedParentProductId; // 存储正在编辑的子产品行ID(对应子表rowKey) private Long editedChildProductId; // 省略get/set方法
修改打开对话框的按钮,把父行ID也传过去:
<p:commandButton id="idCommandButtonDetailProductChild" title="#{diccBean.msg['product.detailProduct']}" icon="fa fa-fw fa-search" action="#{productController.initDetailProduct}" oncomplete="PF('widgetVarDetailProduct').show();" update="tabViewDetalle:idDialogDetailProduct"> <f:setPropertyActionListener value="#{productChild.productDetailDTO.id}" target="#{productController.productDetailId}"/> <!-- 新增传值:父行ID和子行ID --> <f:setPropertyActionListener value="#{productDetail.id}" target="#{productController.editedParentProductId}"/> <f:setPropertyActionListener value="#{productChild.id}" target="#{productController.editedChildProductId}"/> </p:commandButton>
第三步:修改保存按钮逻辑,实现精准局部更新
首先删除idCommandButtonUpdate按钮上的update="tabViewDetalle:idTableDetailProduct"配置,改成在后台保存逻辑执行完成后,手动指定要更新的组件路径:
PrimeFaces 6.0+ 版本写法
public void actionUpdate() { // 原有保存业务逻辑不变 ... // 拼接要更新的组件路径,格式:父表ID:父行索引:子表ID:子行索引 // 先根据editedParentProductId获取父行在父表中的索引,根据editedChildProductId获取子行在子表中的索引 int parentRowIndex = getParentRowIndexById(editedParentProductId); // 自己实现遍历父表列表取索引的逻辑 int childRowIndex = getChildRowIndexById(editedParentProductId, editedChildProductId); // 自己实现遍历对应子表列表取索引的逻辑 // 按需二选一: // 方案1:仅更新子表的指定单行 String updatePath = String.format("tabViewDetalle:idTableDetailProduct:%d:idTableDetailProductChild:%d", parentRowIndex, childRowIndex); // 方案2:更新当前展开行的整个子表(适合子行关联数据有变化的场景,性能也远高于更新整个父表) // String updatePath = String.format("tabViewDetalle:idTableDetailProduct:%d:idTableDetailProductChild", parentRowIndex); PrimeFaces.current().ajax().update(updatePath); // 关闭对话框 PrimeFaces.current().executeScript("PF('widgetVarDetailProduct').hide();"); }
PrimeFaces 6.0以下旧版本写法
把上面的PrimeFaces调用换成RequestContext即可:
RequestContext context = RequestContext.getCurrentInstance(); context.update(updatePath); context.execute("PF('widgetVarDetailProduct').hide();");
注意事项
- 如果不想自己算行索引,也可以给子表的每一行套一个
<p:outputPanel id="childRowPanel">,更新的时候直接定位到这个panel即可 - 不要更新父表任何层级的外层容器,确保仅更新子表范围内的组件,rowExpansion就不会自动收起
内容的提问来源于stack exchange,提问作者Borja
相关产品推荐
相关产品推荐

