In my project, on click of checkbox and add more button, I am trying to add one row and on click of remove button, remove exact row.
Sometimes I get wrong parameter values in controller methods. So the whole component generation logic isn't working properly.
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
}
Output :
What I am doing wrong? Is there any logical bug in my code?