使用对话框在PrimeFaces ver 5 dataTable中添加无法正常工作

时间:2014-08-06 07:08:57

标签: jsf primefaces datatable

点击'保存新发票'我想更新primefaces数据表对话框按钮。 但它会在primefaces数据表中更新对话框的旧数据。

XHTML文件:

<h:form id="form">
    <p:dataTable id="invoiceTable" var="ipsDetail"
        value="#{invoiceBean.ipsDetails}" border="1" editable="true">

        <p:column headerText="Sr. No.">
            <h:outputText value="#{ipsDetail.serialNumber}" />
        </p:column>

        <p:column headerText="Description of Goods">
            <h:outputText value="#{ipsDetail.descriptionOfGoodsOrService}" />
        </p:column>

        <p:column headerText="HSN Code">
            <h:outputText value="#{ipsDetail.hsnCode}" />
        </p:column>

        <p:column headerText="Quantity">
            <h:outputText value="#{ipsDetail.quantity}" />
        </p:column>

        <p:column headerText="Rate">
            <h:outputText value="#{ipsDetail.rate}" />
        </p:column>

        <p:column headerText="Percentage Discount">
            <h:outputText value="#{ipsDetail.percentDiscount}" />
        </p:column>

        <p:column headerText="Amount">
            <h:outputText value="#{invoiceBean.amount}" />
        </p:column>

        <p:column>
            <p:rowEditor />
        </p:column>

        <f:facet name="footer">
            <p:commandButton value="Add Invoice" type="button"
                onclick="PF('addInvoice').show();" />
        </f:facet>
    </p:dataTable>

    <p:dialog header="Add Invoice" widgetVar="addInvoice" minHeight="40"
        showEffect="explode" hideEffect="fold">
        <table border="1">
            <tr>
                <td><h:outputText value="Sr. No." /></td>
                <td><h:outputText value="Description Of Goods Or Services" /></td>
                <td><h:outputText value="HSN Code" /></td>
                <td><h:outputText value="Quantity" /></td>
                <td><h:outputText value="Rate" /></td>
                <td><h:outputText value="Percentage Discount" /></td>
                <td><h:outputText value="Amount" /></td>
            </tr>
            <tr>
                <td><p:inputText value="#{invoiceBean.serialNumber}" size="3"
                        styleClass="Alingment" /></td>

                <td><h:inputTextarea value="#{invoiceBean.descriptionOfGoodsOrService}" cols="45"
                        required="true" label="Description" requiredMessage="Description Require Entry" /></td>

                <td><h:inputText value="#{invoiceBean.hsnCode}" size="6"
                        styleClass="Alingment" /></td>

                <td><h:inputText id="quaintity"
                        value="#{invoiceBean.quantity}" size="3" styleClass="Alingment"
                        required="true" label="Quantity" requiredMessage="Quantity Require Entry" /></td>

                <td><h:inputText id="rate" value="#{invoiceBean.rate}"
                        styleClass="Alingment" required="true" label="Rate" /></td>

                <td><h:inputText value="#{invoiceBean.percentDiscount}"
                        size="2" styleClass="Alingment" requiredMessage="Rate Require Entry" /></td>

                <td><h:inputText value="#{invoiceBean.amount}"
                        styleClass="Alingment" /></td>
            </tr>
        </table>

        <p:commandButton value="Save New Invoice"
            action="#{invoiceBean.addRow}" update="invoiceTable growl"
            process="@this invoiceTable" onclick="PF('addInvoice').hide();" />
        <p:growl id="growl" showDetail="true" sticky="true" />
    </p:dialog>

    <p:commandButton value="Save Invoices" ajax="false"
        action="#{invoiceBean.saveInvoiceData}" onclick="tickAllRows()" />
</h:form>

Managed Bean方法:

此方法用于在primefaces数据表中添加动态行。我想要新的数据从primefaces对话框输入新的动态添加行代码在下面,但它不能正常工作。 invoiceTable无法更新。

public void addRow() {

    ipsDetail = new InvoiceProductsServicesDetail();
    invoiceDao = new InvoiceDao();
    FacesContext facesContext = FacesContext.getCurrentInstance();

    try {
        if (descriptionOfGoodsOrService != "" && descriptionOfGoodsOrService != null && rate != 0 && quantity != 0) {
            ipsDetail.setSerialNumber(serialNumber);
            ipsDetail
                    .setDescriptionOfGoodsOrService(descriptionOfGoodsOrService);
            ipsDetail.setHsnCode(hsnCode);
            ipsDetail.setPercentDiscount(percentDiscount);
            ipsDetail.setQuantity(quantity);
            ipsDetail.setRate(rate);

            ipsDetails.add(ipsDetail);
            invoiceDao.insertIpsDetail(ipsDetail); // This method is used to store ipsDetail in database table.

            DataTable table = (DataTable) facesContext.getViewRoot()
                    .findComponent("form:invoiceTable");

            UIComponent uiTable = ComponentUtils.findParentForm(
                    facesContext, table);

            final AjaxBehavior behavior = new AjaxBehavior();
            RowEditEvent rowEditEvent = new RowEditEvent(uiTable, behavior,
                    table.getRowData());

            rowEditEvent.setPhaseId(PhaseId.UPDATE_MODEL_VALUES);
            table.broadcast(rowEditEvent);
        } 

    } catch (AbortProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

不是更新服务器端的当前组件,而是更新<p:dataTable>中的值,这意味着只需更新invoiceBean.ipsDetails的值即可。让框架更新<p:dataTable>的值。此外,您应该在:form:invoiceTable中使用您的组件<p:commandButton update>的全名。