0

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.

Hardik Visa
  • 323
  • 6
  • 23

1 Answers1

0

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you, but i got one error "An error occurred performing resource injection on managed bean invoiceBean". Now how to solve it ? – Hardik Visa Aug 19 '14 at 11:24
  • Apparently `transactions` or `invoices` is `null` or `get(0)` didn't exist. Just check the root cause of the stack trace for clues, it should be a very basic non-JSF problem. I can't tell more on that as you didn't show that part of the code. But that part is not relevant. You should at least make sure that you don't touch getters/setters. Then the concrete problem as stated in the current question is solved. – BalusC Aug 19 '14 at 11:25