Jsf动态添加/删除c:forEach循环中的组件

在我的项目中,单击复选框并添加更多按钮,我试图添加一行,单击删除按钮,删除确切的行。

有时我在控制器方法中得到错误的参数值。 因此整个组件生成逻辑无法正常工作。

的index.xhtml

   OuterIndex : #{shipmentCount.index}     
InnerIndex : #{shipmentRowCount.index} #{shipmentRow.priceChoice} #{shipmentRow.priceEnable}


PostOffer.java

 @ManagedBean @ViewScoped public class PostOffer implements Serializable { private List shipmentTerms = new ArrayList(); public PostOffer() {} @PostConstruct public void init() { shipmentTerms.add(new ShipmentProxy(1l, "FAS")); shipmentTerms.add(new ShipmentProxy(2l, "CFR")); } public void processPriceDiffChoice(int shipmentIndex, int rowIndex) { ShipmentRow row = shipmentTerms.get(shipmentIndex).getShipmentRowList().get(rowIndex); if (row.getPriceChoice().equals("Above Price")) { row.setPriceEnable(false); } else { row.setPriceEnable(true); } } public void addShipmentTermsRow(int shipmentIndex) { ShipmentProxy proxy = shipmentTerms.get(shipmentIndex); if (proxy.isStatus()) { proxy.getShipmentRowList().add(new ShipmentRow()); } else { proxy.getShipmentRowList().clear(); } } public void removeShipmentTermsRow(int shipmentIndex, int rowIndex) { shipmentTerms.get(shipmentIndex).getShipmentRowList().remove(rowIndex); } //getters and setters } 

ShipmentProxy.java

 public class ShipmentProxy { private Long id; private boolean status; private String name; private List shipmentRowList = new ArrayList(); public ShipmentProxy(Long id, String name) { this.id = id; this.name = name; } //getters and setters } 

ShipmentRow.java

 public class ShipmentRow { private String priceChoice = "Above Price"; private String price = "0"; private boolean priceEnable = false; //getters and setters } 

输出:

在此处输入图像描述

我做错了什么? 我的代码中是否有任何逻辑错误?

我的代码中没有逻辑错误。 这只是旧版本中的JSF错误。

根据BalusC的评论,我遵循了以下步骤:

  1. 我在JBoss AS 7.1升级了Mojarrajsf-api-2.2.9.jarjsf-impl-2.2.10.jar )。 请参阅此
  2. 我在JSF页面中将c:forEach循环更改为ui:repeath:dataTable
  3. 在方法参数中传递direct var而不是index

现在,新更改的代码看起来像这样:

的index.xhtml

        


PostOffer.java

 public void processPriceDiffChoice(ShipmentRow row) { if (row.getPriceChoice().equals("Above Price")) { row.setPriceEnable(false); } else { row.setPriceEnable(true); } } public void addShipmentTermsRow(ShipmentProxy proxy) { if (proxy.isStatus()) { proxy.getShipmentRowList().add(new ShipmentRow()); } else { proxy.getShipmentRowList().clear(); } } public void removeShipmentTermsRow(ShipmentProxy proxy,ShipmentRow row) { proxy.getShipmentRowList().remove(row); } 

注意:其余代码与上面提到的相同。