简体   繁体   English

手风琴中的JSF dataTable未刷新面板数据

[英]JSF dataTable in accordionPanel data not refreshed

I have a page with a PrimeFaces Accordion containing a dataTable. 我有一个带有PrimeFaces手风琴的页面,其中包含一个dataTable。 Accordion Tabs and Table rows can be added dynamically with buttons. 可以使用按钮动态添加“手风琴”选项卡和“表格”行。 Now I observe strange things regarding data binding. 现在,我观察到有关数据绑定的奇怪事情。 I can see that my accordion tabs (which represent OrderProducts in Java) are bound to my bean and updated at any time. 我可以看到我的手风琴标签(代表Java中的OrderProducts)已绑定到我的bean并随时更新。 The dataTable rows are not always bound to their corresponding bean (OrderDestination) and therefore the data is not always updated after submitting. dataTable行并不总是绑定到其对应的bean(OrderDestination),因此,提交后并不总是更新数据。 The first tab seems to work as expected, but in the second tab (if I add a second one) the binding does not work. 第一个选项卡似乎按预期工作,但是在第二个选项卡(如果添加第二个选项卡)中,绑定无效。 Also I tried to add an empty OrderDestination to every new OrderProduct in the constructor (not visible in the code below). 我也尝试向构造函数中的每个新OrderProduct添加一个空的OrderDestination(在下面的代码中不可见)。 This default OrderDestination is never bound to the bean. 默认的OrderDestination永远不会绑定到bean。 Am I doing something wrong or can you spot any error or misunderstanding in the following code? 我是在做错什么吗,还是可以在以下代码中发现任何错误或误解?

orderCreate.xhtml (simplified) orderCreate.xhtml(简体)

<h:form id="orderPlaceForm">    
    <p:accordionPanel id="productAccordion" value="#{orderCreateModel.orderProducts}" var="product">
        <p:tab>
            <h:dataTable var="item" value="#{product.orderDestinations}">
                <h:column>
                    <h:inputText value="#{item.totalWeightKg}">
                </h:column>
                <!-- other columns -->
            </h:dataTable>

            <!-- button to add another row -->
            <h:commandButton value="add dest" action="#{orderCreate.addDestination}">
                <f:param name="productId" value="#{product.uuid}" />
                <f:ajax render="productAccordion" execute="@this productAccordion"/>
            </h:commandButton>
        </p:tab>
    </p:accordionPanel>

    <!-- button to add another tab -->
    <h:commandButton value="add prod" action="#{orderCreate.addProduct}">
        <f:ajax render=":orderPlaceForm:productAccordion" execute="@this :orderPlaceForm:productAccordion"/>
    </h:commandButton>
</h:form>

OrderCreate.java OrderCreate.java

@ManagedBean
@RequestScoped
public class OrderCreate {

    @ManagedProperty(value="#{orderCreateModel}")
    private OrderCreateModel orderCreateModel;

    public void addProduct() {
        orderCreateModel.addProduct();
    }

    public void addDestination() {
        orderCreateModel.findProduct(prodId).addDestination(); //prodId from FacesContext
    }
}

OrderCreateModel.java OrderCreateModel.java

public class OrderCreateModel {
    private List<OrderProduct> orderProducts = new ArrayList();

    public void addProduct() {
        orderProducts.add(new OrderProduct());
    }

    public OrderProduct findProduct(UUID prodID) {
        //look in orderProducts for prodID
    }
}

OrderProduct.java OrderProduct.java

public class OrderProduct {
    private List<OrderDestination> orderDestinations = new ArrayList();

    public void addDestination() {
        orderDestinations.add(new OrderDestination());
    }
}

OrderDestination.java OrderDestination.java

public class OrderDestination {
    //POJO with some String fields
}

The solution is simple but not easy to find and understand: Using ServerFace elements (eg h:dataTable ) within a Primeface element (p:accordionPanel) leads to unexpected and unpredictable behavior. 解决方案很简单,但不容易找到和理解:在Primeface元素(p:accordionPanel)中使用ServerFace元素(例如h:dataTable )会导致意外和不可预测的行为。 As soon as I replace all ServerFace elements nested within Primeface's accordion panel with the corresponding Primeface elements (eg p:dataTable ), everything works as expected. 一旦我用相应的Primeface元素(例如p:dataTable )替换了Primeface的手风琴面板中嵌套的所有ServerFace元素,一切都会按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM