gpt4 book ai didi

java - Jsf动态添加/删除c中的组件:forEach loop

转载 作者:太空宇宙 更新时间:2023-11-04 13:33:10 27 4
gpt4 key购买 nike

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

有时我在 Controller 方法中得到错误的参数值。所以整个组件生成逻辑无法正常工作。

Index.xhtml

<h:form>
<h:panelGroup id="shipmentTermsBox">
<c:forEach items="#{postOffer.shipmentTerms}" var="shipment" varStatus="shipmentCount">
OuterIndex : #{shipmentCount.index}
<h:selectBooleanCheckbox value="#{shipment.status}">
<f:ajax event="change" listener="#{postOffer.addShipmentTermsRow(shipmentCount.index)}" render="shipmentTermsBox" />
</h:selectBooleanCheckbox>
<label for="stayin">#{shipment.name} </label>
<br/>
<table border="1">
<c:forEach items="#{shipment.shipmentRowList}" var="shipmentRow" varStatus="shipmentRowCount">
<tr>
<td>
InnerIndex : #{shipmentRowCount.index}
</td>
<td>
#{shipmentRow.priceChoice}
<h:selectOneMenu value="#{shipmentRow.priceChoice}">
<f:selectItem itemValue="Above Price" itemLabel="Above Price"/>
<f:selectItem itemValue="+ (more)" itemLabel="+ (more)"/>
<f:selectItem itemValue="- (more)" itemLabel="- (more)"/>
<f:ajax event="change" listener="#{postOffer.processPriceDiffChoice(shipmentCount.index,shipmentRowCount.index)}" render="shipmentTermsBox"/>
</h:selectOneMenu>
</td>
<td>
#{shipmentRow.priceEnable}
<h:panelGroup rendered="#{shipmentRow.priceEnable}">
<h:inputText value="#{shipmentRow.price}">
<f:ajax/>
</h:inputText>
</h:panelGroup>
</td>
<td>
<h:commandButton value="Remove">
<f:ajax event="action" listener="#{postOffer.removeShipmentTermsRow(shipmentCount.index,shipmentRowCount.index)}" render="shipmentTermsBox"/>
</h:commandButton>
</td>
</tr>
</c:forEach>
</table>
<h:panelGroup rendered="#{shipment.status}">
<h:commandButton value="Add More">
<f:ajax event="action" listener="#{postOffer.addShipmentTermsRow(shipmentCount.index)}" render="shipmentTermsBox"/>
</h:commandButton>
<br/><br/>
</h:panelGroup>
</c:forEach>
</h:panelGroup>
</h:form>

PostOffer.java

@ManagedBean
@ViewScoped
public class PostOffer implements Serializable {
private List<ShipmentProxy> shipmentTerms = new ArrayList<ShipmentProxy>();

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<ShipmentRow> shipmentRowList = new ArrayList<ShipmentRow>();

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
}

输出:

enter image description here

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

最佳答案

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

根据 BalusC 的评论,我已按照以下步骤操作:

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

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

Index.xhtml

<h:form id="form">
<h:panelGroup id="shipmentTermsBox">
<ui:repeat value="#{postOffer.shipmentTerms}" var="shipment">
<h:selectBooleanCheckbox value="#{shipment.status}">
<f:ajax event="change" listener="#{postOffer.addShipmentTermsRow(shipment)}" render=":form:shipmentTermsBox" />
</h:selectBooleanCheckbox>
<label for="stayin">#{shipment.name} </label> <br/>
<h:dataTable var="shipmentRow" value="#{shipment.shipmentRowList}">
<h:column>
<h:selectOneMenu value="#{shipmentRow.priceChoice}">
<f:selectItem itemValue="Above Price" itemLabel="Above Price"/>
<f:selectItem itemValue="+ (more)" itemLabel="+ (more)"/>
<f:selectItem itemValue="- (more)" itemLabel="- (more)"/>
<f:ajax event="change" listener="#{postOffer.processPriceDiffChoice(shipmentRow)}" render=":form:shipmentTermsBox"/>
</h:selectOneMenu>
</h:column>
<h:column>
<h:panelGroup rendered="#{shipmentRow.priceEnable}">
<h:inputText value="#{shipmentRow.price}">
<f:ajax/>
</h:inputText>
</h:panelGroup>
</h:column>
<h:column>
<h:commandButton value="Remove">
<f:ajax event="action" listener="#{postOffer.removeShipmentTermsRow(shipment,shipmentRow)}" render=":form:shipmentTermsBox"/>
</h:commandButton>
</h:column>
</h:dataTable>
<h:panelGroup rendered="#{shipment.status}">
<h:commandButton value="Add More">
<f:ajax event="action" listener="#{postOffer.addShipmentTermsRow(shipment)}" render=":form:shipmentTermsBox"/>
</h:commandButton>
<br/><br/>
</h:panelGroup>
</ui:repeat>
</h:panelGroup>
</h:form>

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);
}

注意:其余代码与问题中提到的完全相同。

关于java - Jsf动态添加/删除c中的组件:forEach loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31963015/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com