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的评论,我遵循了以下步骤:
- 我在
JBoss AS 7.1
升级了Mojarra
(jsf-api-2.2.9.jar
,jsf-impl-2.2.10.jar
)。 请参阅此 - 我在JSF页面中将
c:forEach
循环更改为ui:repeat
和h:dataTable
。 - 在方法参数中传递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); }
注意:其余代码与上面提到的相同。