简体   繁体   中英

primefaces datatable not working properly

Following XHTML code for primefaces datatable.

<h:panelGroup id="mode">
                    <p:panelGrid columns="2">
                        <p:panelGrid columns="2">
                            <p:outputLabel style="font-weight: bold;"
                                value="Mode Of Payments" />
                            <p:selectOneRadio value="#{invoiceBean.modeOfPayment}"
                                layout="pageDirection">
                                <f:ajax render="mode" />
                                <f:selectItem itemLabel="Cash" itemValue="Cash" />
                                <f:selectItem itemLabel="Cheque" itemValue="Cheque" />
                            </p:selectOneRadio>
                            <p:outputLabel value="Enter Bank Name :" />
                            <p:inputText value="#{invoiceBean.bankName}"
                                disabled="#{invoiceBean.modeOfPayment == 'Cash'}" />
                            <p:outputLabel value="Enter Cheque Number :" />
                            <p:inputText value="#{invoiceBean.chequeNumber}"
                                disabled="#{invoiceBean.modeOfPayment == 'Cash'}" />
                            <p:outputLabel value="Enter Amount :" />
                            <p:inputText value="#{invoiceBean.chequeAmount}" />
                        </p:panelGrid>
                        <p:panelGrid columns="1">
                            <p:dataTable id="transactionTable"
                                value="#{invoiceBean.transactions}" var="transaction">
                                <p:column headerText="Mode Of Payment">
                                    <p:outputLabel value="#{transaction.modeOfPayment}" />
                                </p:column>
                                <p:column headerText="Bank Name">
                                    <p:outputLabel value="#{transaction.bankName}" />
                                </p:column>
                                <p:column headerText="Amount">
                                    <p:outputLabel value="#{transaction.chequeAmount}" />
                                </p:column>
                                <p:column headerText="Balance">
                                    <p:outputLabel value="#{transaction.balance}" />
                                </p:column>
                                <p:summaryRow>
                                    <p:column colspan="3">
                                        <p:outputLabel value="Remaining Balance" />
                                    </p:column>
                                    <p:column>
                                        <p:outputLabel value="#{transaction.balance}" />
                                    </p:column>
                                </p:summaryRow>
                            </p:dataTable>
                        </p:panelGrid>
                    </p:panelGrid>
                </h:panelGroup>
                <p:commandButton value="Save New Invoice"
                    action="#{invoiceBean.addRow}"
                    update=":form:invoiceTable :form:transactionTable growl"
                    process="@form invoiceTable" onclick="PF('addInvoice').hide();">
                    <f:ajax render=":form:transactionTable" />
                    <f:ajax render=":form:invoiceTable" />
                </p:commandButton>

Following managed beans code for transactionTable :

@PostConstruct
public void init() {
    session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    transactionDao = new TransactionDao();
    invoiceDao = new InvoiceDao();
    invoices = invoiceDao.getInvoiceData(invoiceNumber);
    transactions = transactionDao.getTransactions(invoices.get(0).getId());
    invoiceProductsServicesDetails = invoiceDao
            .getInvoiceProductsServicesDetailDatas();
}

When I add new record in HTML table it will display in transactionTable when click on "Save New Invoice". Its work first time properly but when I click on radio button and select "Cheque" option new data not display and its replace old data.

You're not supposed to perform business logic in getters/setters.

A normal getter/setter pair looks like this (exactly like as the average IDE would autogenerate for you):

public List<Transaction> getTransactions() {
    return transactions;
}

public void setTransactions(List<Transaction> transactions) {
    this.transactions = transactions;
} 

You should never change them unless you really know what you're doing. In your particular case, the <p:dataTable> calls the getter on every iteration round. You're basically calling the DAO for every single row. This doesn't make sense. This has a huge performance impact if there are lots of records.

In order to preload the data for view, one of the ways is a @PostConstruct method (the bean should preferably be @ViewScoped for this kind of view):

@PostConstruct
public void init() {
    transactions = transactionDao.getTransactions(invoices.get(0).getId());
}

In order to save/update the data after edit, just use the action(listener) method.

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM