Jsf dynamic add/remove components in c:forEach loop

后端 未结 1 1656
误落风尘
误落风尘 2021-01-24 22:02

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 valu

相关标签:
1条回答
  • 2021-01-24 22:48

    There is no logical bug in my code. It was just JSF bug in older version.

    As per BalusC's comment, I have followed these steps :

    1. I have upgraded Mojarra(jsf-api-2.2.9.jar,jsf-impl-2.2.10.jar) in JBoss AS 7.1. Refer this
    2. I changed c:forEach loop to ui:repeat and h:dataTable in JSF page.
    3. Passed direct var instead of index in method parameter.

    Now, the new changed code is looked something like this :

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

    Note : Rest of the code is same exactly mentioned in question.

    0 讨论(0)
提交回复
热议问题