Update table after load data by code

Hi, i create a container table inside a layout page. The save action function right.
Using a function i load the table by code but the table don’t refresh. How can i refresh table “amortizationTable” after load data? This is the code:

<window xmlns="http://schemas.haulmont.com/cuba/screen/window.xsd"
        caption="msg://editorCaption"
        focusComponent="form">
    <data>
        <instance id="assetDc"
                  class="com.entity.Asset"
                  view="asset-view">
            <loader/>
            <collection id="amortizationDc" property="amortizations"/>
        </instance>
    </data>
    <dialogMode height="600" width="800"/>
    <layout expand="editActions" spacing="true">
        <form id="form" dataContainer="assetDc">
            <hbox spacing="true">
                <pickerField id="productField" property="product" dataContainer="assetDc" caption="msg://nameField"  >
                    <actions>
                        <action id="lookup" type="picker_lookup"/>
                        <action id="clear" type="picker_clear"/>
                    </actions>
                </pickerField>
                <textField id="serialNumberField" property="serialNumber" caption="msg://serialNumberField" width="100px" />
                <textField id="siteField" property="site" caption="msg://siteField" width="AUTO" />
                <pickerField id="categoryField" property="category" dataContainer="assetDc" caption="msg://categoryField"  >
                    <actions>
                        <action id="lookup" type="picker_lookup"/>
                        <action id="clear" type="picker_clear"/>
                    </actions>
                </pickerField>
            </hbox>
        </form>

        <groupBox id="amortizationsBox" caption="msg://amortizationsField" expand="amortizationTable" stylename="light">
            <table id="amortizationTable" dataContainer="amortizationDc" width="100%" height="100%">
                <actions>
                    <action id="create" type="create" openType="DIALOG"/>
                    <action id="edit" type="edit" openType="DIALOG"/>
                    <action id="remove" type="remove" openType="DIALOG"/>
                </actions>
                <columns>
                    <column id="year" caption="msg://yearField"/>
                    <column id="description" caption="msg://descriptionField"/>
                    <column id="rate" caption="msg://rateField"/>
                    <column id="amount" caption="msg://amountField"/>
                </columns>
                <buttonsPanel>
                    <button action="amortizationTable.create" caption="msg://createButton"/>
                    <button action="amortizationTable.edit" caption="msg://changeButton"/>
                    <button action="amortizationTable.remove" caption="msg://deleteButton"/>
                </buttonsPanel>
                <rows />
            </table>
        </groupBox>

        <hbox id="editActions" spacing="true" align="MIDDLE_RIGHT">
            <button id="genAssetPlans" invoke="CalculateAmortizations" caption="msg://genAmortPlanButton" />
            <button action="windowCommitAndClose" caption="msg://commitButton"/>
            <button action="windowClose" caption="msg://cancelButton"/>
        </hbox>
    </layout>
</window>

public void CalculateAmortizations() {
        Asset asset = getEditedEntity();

        List<Amortization> amortizationList = asset.getAmortizations();

        Amortization amortization = metadata.create(Amortization.class);
        amortization.setAsset(asset);
        amortization.setYear(2020);
        amortization.setDescription("Description");
        amortizationList.add(amortization);

        asset.setAmortizations(amortizationList);
    }

Thank’s and best regards,
Massimo

Hello @massimo,

You need to use the getMutableItems() method of CollectionPropertyContainer to return mutable list of entiites. Changes in this list will refresh the data in the underlying property, for example:

@Inject
private CollectionPropertyContainer<Amortization> amortizationDc;

public void CalculateAmortizations() {
    Asset asset = getEditedEntity();

    Amortization amortization = metadata.create(Amortization.class);
    amortization.setAsset(asset);
    amortization.setYear(2020);
    amortization.setDescription("Description");

    amortizationDc.getMutableItems().add(amortization);
}

Regards,
Gleb

1 Like

It works… Perfect, thanks

Hi, unfortunately if I save the “Amortization” record without having first saved the “Asset” record I return the error "IllegalStateException: An attempt to save an entity with reference to some not persisted entity. All newly created entities must be saved in the same transaction. Put all these objects to the CommitContext before commit ". I saw that it is because updating it in the same commit, but I don’t know how to do it and where, can you help me?

Hi,

Instead of Metadata try to inject and use DataContext to create items.

Amortization amortization = dataContex.create(Amortization.class);
1 Like

It works, thanks

1 Like