Hardik Visa
Hardik Visa

Reputation: 323

how can inputText disabled using old data entry?

When I click on "Add Invoice" button then open dialog box. fill some data and save it in database table. When I click on "Add Invoice" button second time primefaces inputText value is old data and inputText disable at that time. But following code does not working properly. Always inputText blank with disable true ... Please help me. Thanks

<p:dataTable id="invoiceTable" var="invoiceProductsServicesDetail"
    value="#{invoiceBean.invoiceProductsServicesDetails}" border="1"
    editable="true">
    <p:column headerText="Sr. No.">
        <p:outputLabel
            value="#{invoiceProductsServicesDetail.serialNumber}" />
        </p:column>
        <p:column headerText="HSN Code">
            <p:outputLabel value="#{invoiceProductsServicesDetail.hsnCode}" />
        </p:column>
        <p:column headerText="Quantity">
            <p:outputLabel value="#{invoiceProductsServicesDetail.quantity}" />
        </p:column>
        <p:column headerText="Rate">
            <p:outputLabel value="#{invoiceProductsServicesDetail.rate}" />
        </p:column>
        <p:column headerText="Percentage Discount">
            <p:outputLabel
                value="#{invoiceProductsServicesDetail.percentDiscount}" />
        </p:column>
        <f:facet name="footer">
            <p:commandButton value="Add Invoice" type="button"
                onclick="PF('addInvoice').show();" />
        </f:facet>
 </p:dataTable>

Following Dialoag box open when click on "Add Invoice" Button. And it get input.

<p:dialog id="invoiceDialog" header="Add Invoice"
    widgetVar="addInvoice" minHeight="40" showEffect="explode"
    hideEffect="fold">
    <table border="1" id="dialogTable">
        <tr>
            <td><p:outputLabel value="HSN Code" /></td>
            <td><p:outputLabel value="Quantity" /></td>
            <td><p:outputLabel value="Rate" /></td>
            <td><p:outputLabel value="Percentage Discount" /></td>
        </tr>
        <tr>
            <td><p:inputText value="#{invoiceBean.hsnCode}" size="6"
                disabled="#{invoiceBean.hsnCode != null}" /></td>
            <td><p:inputText id="quaintity"
                value="#{invoiceBean.quantity}" size="3" styleClass="Alingment"
                required="true" label="Quantity"
                requiredMessage="Quantity Require Entry" autocomplete="off"
                disabled="#{invoiceBean.quantity != 0}" /></td>
            <td><p:inputText id="rate" value="#{invoiceBean.rate}"
                styleClass="Alingment" required="true" label="Rate"
                requiredMessage="Rate Require Entry" autocomplete="off"
                disabled="#{invoiceBean.rate != 0}" /></td>
            <td><p:inputText value="#{invoiceBean.percentDiscount}"
                size="2" styleClass="Alingment" autocomplete="off"
                disabled="#{invoiceBean.percentDiscount != 0}" /></td>
            </tr>
    </table>
    <p:commandButton value="Save New Invoice"
        action="#{invoiceBean.addRow}" update=":form:invoiceTable growl"
        process="@form invoiceTable" onclick="PF('addInvoice').hide();">
        <p:confirm header="Confirmation" message="Are You Sure ?"
                    icon="ui-icon-alert" />
        <f:ajax render=":form:invoiceTable" />
    </p:commandButton>
</p:dialog>

Manage Bean : invoiceBean Here, I following method is used to store data in specific object.

public void addRow() {
    int lastBalance, currentBalance, storeBalance;
    transaction = new Transaction();
    invoiceProductsServicesDetail = new InvoiceProductsServicesDetail();
    invoiceDao = new InvoiceDao();
    transactionDao = new TransactionDao();
    FacesContext facesContext = FacesContext.getCurrentInstance();
    DataTable dataTable = (DataTable) facesContext.getViewRoot()
            .findComponent("form:invoiceTable");
    UIComponent uiTable = ComponentUtils.findParentForm(facesContext,
            dataTable);
    final AjaxBehavior behavior = new AjaxBehavior();
    try {
        if (descriptionOfGoodsOrService != ""
                && descriptionOfGoodsOrService != null && rate != 0
                && quantity != 0) {
            amount = (rate * quantity);
            this.grossTotal = amount = (amount - (amount * (percentDiscount / 100)));
            this.netTotal = ((amount) + (amount * (Constants.VAT / 100)) + (amount * (Constants.SERVICE_TAX / 100)));

            if (chequeAmount <= amount) {
                invoiceProductsServicesDetails = invoiceDao.getInvoiceProductsServicesDetailData(invoices.get(
                        0).getId());

                transactions = transactionDao.getTransactions(invoices.get(
                        0).getId());
                invoiceProductsServicesDetail.setSerialNumber(dataTable
                        .getRowCount() + 1);
                invoiceProductsServicesDetail.setHsnCode(hsnCode);
                invoiceProductsServicesDetail
                        .setPercentDiscount(percentDiscount);
                invoiceProductsServicesDetail.setQuantity(quantity);
                invoiceProductsServicesDetail.setRate(rate);
                invoiceProductsServicesDetail.setInvoiceId(invoices.get(0)
                        .getId());

                invoiceProductsServicesDetails
                        .add(invoiceProductsServicesDetail);

                if (transactions.size() != 0) {
                    setTransactions(transactions);
                    lastBalance = transactions.get(transactions.size() - 1)
                            .getBalance();
                } else {
                    lastBalance = 0;
                }
                currentBalance = amount - chequeAmount;
                storeBalance = lastBalance + currentBalance;

                transaction.setModeOfPayment(modeOfPayment);
                if (modeOfPayment.equals("Cheque")) {
                    transaction.setBankName(bankName);
                    transaction.setChequeNumber(chequeNumber);
                    transaction.setBalance(storeBalance);
                } else {

                    transaction.setBalance(storeBalance);

                }
                transaction.setChequeAmount(chequeAmount);
                transaction.setReceiptNumber(String.valueOf(new Date()
                        .getTime()));
                transactionDao.setTransaction(transaction, invoices.get(0).getId());
                this.transactions = transactionDao.getTransactions(invoices.get(0).getId());
                invoiceDao.insertInvoiceProductsServicesDetail(invoiceProductsServicesDetail);
                RowEditEvent rowEditEvent = new RowEditEvent(uiTable,behavior, invoiceProductsServicesDetail);
                rowEditEvent.setPhaseId(PhaseId.UPDATE_MODEL_VALUES);
                dataTable.broadcast(rowEditEvent);
            } else {

                FacesMessage message = new FacesMessage(
                        FacesMessage.SEVERITY_INFO, "System Error",
                        "Your Enter Amout Is Must Less Then Total Amount");
                facesContext.addMessage(null, message);
            }

        }
    } catch (AbortProcessingException e) {
        e.printStackTrace();
    } finally {
        invoiceProductsServicesDetail = null;
    }
}

Upvotes: 0

Views: 583

Answers (1)

BalusC
BalusC

Reputation: 1108722

You're not clearing out the dialog before opening it.

Change

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

To

<p:commandButton value="Add Invoice" type="button"
    action="#{invoiceBean.createInvoice}" update="invoiceDialog"
    oncomplete="PF('addInvoice').show();" />

With

public void createInvoice() {
    hsnCode = null;
    quantity = null;
    rate = null;
    percentDiscount = null;
}

That said, the design of your model is broken. You're flattening and repeating the model's properties straight in the backing bean instead of using the model directly. Fix it by removing the hsnCode, quantity, rate and percentDiscount properties and then adding this:

private InvoiceProductsServicesDetail invoiceProductsServicesDetail;

public void createInvoice() {
    invoiceProductsServicesDetail = new InvoiceProductsServicesDetail();
}

With

<p:inputText value="#{invoiceBean.invoiceProductsServicesDetail.hsnCode}" ... />
<p:inputText value="#{invoiceBean.invoiceProductsServicesDetail.quantity}" ... />
...

See also:

Upvotes: 1

Related Questions