2015-10-09 22 views
0

我有一個包含數據表的PrimeFaces Accordion的頁面。 Accordion Tabs和Table行可以通過按鈕動態添加。現在我觀察有關數據綁定的奇怪事情。我可以看到我的手風琴選項卡(代表Java中的OrderProducts)綁定到我的bean並隨時更新。 dataTable行並不總是綁定到它們相應的bean(OrderDestination),因此提交後數據並不總是被更新。第一個選項卡似乎按預期工作,但在第二個選項卡中(如果我添加第二個選項卡)綁定不起作用。此外,我試圖在構造函數中添加一個空的OrderDestination給每個新的OrderProduct(在下面的代碼中不可見)。這個默認的OrderDestination永遠不會綁定到bean。我做錯了什麼,或者你能發現以下代碼中的任何錯誤或誤解嗎?JSF數據手風琴表數據表未更新

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

@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

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

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

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

OrderDestination.java

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

我自己試着理解這個問題的次數越多,我的感覺就是這個問題與最初在代碼中創建的元素(OrderProducts或OrderDestinations)有關。在實例化初始OrderCreateModel時,我添加了一個OrderProduct和一個OrderDestination。此OrderDestination不受限制。當我使用按鈕添加額外的OrderDestination時,新創建的OrderDestinations被綁定... – Retolinho

回答

0

的解決方案是簡單的,但不容易找到和理解: 使用ServerFace元件(例如h:dataTable)在Primeface元素(p:accordionPanel)內導致意外和不可預知的行爲。只要將Primeface的accordion面板中嵌套的所有ServerFace元素替換爲相應的Primeface元素(例如p:dataTable),一切都按預期工作。