Hi,
I bumped into an unexpected behavior/issue.
Use case
There is an invoice item data grid where I want the user to manually fill in each row whenever they add a new item. The “add item” function is working fine, and the renderer correctly displays the required fields with empty values after I click the “add item” button. However, when I try to add a second item, the values of the first item are automatically reset to their default state. I have checked the items from the InvoiceDc
and invoiceItemDataGrid
. When iterating through the list, the previous values of the items are not being retained. I’m not sure where I’m going wrong.
Any help would be greatly appreciated.
Following is the code
<instance id="invoiceDc"
class="com.example.entity.Invoice">
<fetchPlan extends="_base"/>
<loader/>
<collection id="invoiceItemDc" property="invoiceItems"></collection>
</instance>
<dataGrid id="invoiceItemDataGrid" dataContainer="invoiceItemDc" width="100%" height="150px">
<columns>
<column key="item" header="Item" editable="true" sortable="false" width="300px"/>
<column key="quantity" property="quantity" editable="true" sortable="false" width="50px"/>
<column key="rate" property="rate" editable="true" sortable="false" width="100px"/>
<column key="taxAmount" property="taxPercentage" editable="true" sortable="false" width="80px"/>
<column key="amount" property="amount" editable="true" sortable="false" width="100px"/>
</columns>
</dataGrid>
<button id="addInvoiceItemBtn" text="Add item" />
<button id="checkBtn" text="Check" />
/**
* NumberField Tax amount column renderer
* @return Renderer
*/
@Supply(to = "invoiceItemDataGrid.taxAmount", subject = "renderer")
private Renderer<InvoiceItem> invoiceItemDataGridTaxAmountRenderer() {
return new ComponentRenderer<>(invoiceItem -> {
NumberField taxAmountField = uiComponents.create(NumberField.class);
taxAmountField.setWidth(TAX_AMOUNT_FIELD_WIDTH);
taxAmountField.addValueChangeListener(numberFieldDoubleComponentValueChangeEvent -> {
// log.info("Tax amount field value changed to {}", numberFieldDoubleComponentValueChangeEvent.getValue());
});
if(Optional.ofNullable(invoiceItem.getTaxPercentage()).isPresent()){
taxAmountField.setValue(Double.valueOf(invoiceItem.getTaxPercentage()));
}else{
taxAmountField.setValue(0.0);
}
return taxAmountField;
});
}
/**
* NumberField Amount column renderer
* @return Renderer
*/
@Supply(to = "invoiceItemDataGrid.amount", subject = "renderer")
private Renderer<InvoiceItem> invoiceItemDataGridAmountRenderer() {
return new ComponentRenderer<>(invoiceItem -> {
NumberField amountField = uiComponents.create(NumberField.class);
amountField.setWidth(AMOUNT_FIELD_WIDTH);
amountField.addValueChangeListener(numberFieldDoubleComponentValueChangeEvent -> {
// log.info("Amount field value changed to {}", numberFieldDoubleComponentValueChangeEvent.getValue());
});
if(Optional.ofNullable(invoiceItem.getAmount()).isPresent()){
amountField.setValue(Double.valueOf(invoiceItem.getAmount()));
}else{
amountField.setValue(0.0);
}
return amountField;
});
}
Thanks,
S