How to commit and refresh a datagrid

In v7 how can I commit and refresh using a datagrid? Previously I could directly invoke the commit and refresh from a datasource, but now those methods do not exist.

I’m following this and this example but I can not get it done commit and refresh.

I appreciate any help.

Regards,

Nelson F.

Hi,

In v7 you need to use getScreenData().getDataContext().commit() in order to commit entities and getScreenData().loadAll() to reload all or DataLoader#load on a specific loader to reload only a particular container.

Gleb

1 Like

Thanks @gorelov, but it does not work for me. I have the following code

@UiController("trz_GNRCurrency.browse")
@UiDescriptor("gnr-currency-browse.xml")
@LookupComponent("gNRCurrencyTable")
@LoadDataBeforeShow
public class GNRCurrencyBrowse extends TrzStandardLookup<GNRCurrency> {

    @Inject
    private DataGrid<GNRCurrency> gNRCurrencyTable;
    @Inject
    private CollectionContainer<GNRCurrency> gNRCurrencyCollectionDc;

    @Subscribe
    private void onInit(InitEvent event) {
        CreateAction createAction = (CreateAction) actions.create(CreateAction.ID);
        createAction.withHandler(actionPerformedEvent -> {
            GNRCurrency entity = metadata.create(GNRCurrency.class);
            gNRCurrencyCollectionDc.getMutableItems().add(entity);
            gNRCurrencyTable.edit(entity);
        });

        EditAction editAction = (EditAction) actions.create(EditAction.ID);
        editAction.withHandler(actionPerformedEvent -> {
            GNRCurrency entity = gNRCurrencyTable.getSingleSelected();
            if (entity != null)
                gNRCurrencyTable.edit(entity);
        });
        gNRCurrencyTable.addAction(createAction);
        gNRCurrencyTable.addAction(editAction);
    }

    @Subscribe("gNRCurrencyTable")
    private void onGNRCurrencyTableEditorPostCommit(DataGrid.EditorPostCommitEvent event) {
        getScreenData().getDataContext().commit();
        getScreenData().loadAll();
    }    

}

My xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/screen/window.xsd"
        caption="msg://browseCaption"
        focusComponent="gNRCurrencyTable"
        messagesPack="com.treze.sga.web.general.gnrcurrency">
    <data>
        <collection id="gNRCurrencyCollectionDc"
                    class="com.treze.sga.entity.general.GNRCurrency"
                    view="_local">
            <loader id="gNRCurrencyCollectionDl">
                <query>
                    <![CDATA[select e from trz_GNRCurrency e]]>
                </query>
            </loader>
        </collection>
    </data>
    <dialogMode height="600"
                width="800"/>
    <layout expand="gNRCurrencyTable"
            spacing="true">
        <filter id="filter"
                applyTo="gNRCurrencyTable"
                dataLoader="gNRCurrencyCollectionDl"
                useMaxResults="false">
            <properties include=".*"/>
        </filter>
        <dataGrid id="gNRCurrencyTable"
                  dataContainer="gNRCurrencyCollectionDc"
                  editorEnabled="true"
                  settingsEnabled="false"
                  selectionMode="MULTI"
                  width="100%">
            <actions>
                <action id="create" type="create"/>
                <action id="edit" type="edit"/>
                <action id="remove" type="remove"/>
                <action id="refresh" type="refresh"/>
                <action id="excel" type="excel"/>
            </actions>
            <columns>
                <column property="id"/>
                <column property="name"/>
                <column property="inactive"/>
            </columns>
            <rowsCount/>
            <buttonsPanel id="buttonsPanel"
                          alwaysVisible="true">
                <button id="createBtn" action="gNRCurrencyTable.create"/>
                <button id="editBtn" action="gNRCurrencyTable.edit"/>
                <button id="removeBtn" action="gNRCurrencyTable.remove"/>
                <button id="refreshBtn" action="gNRCurrencyTable.refresh"/>
                <button id="excelBtn" action="gNRCurrencyTable.excel"/>
            </buttonsPanel>
        </dataGrid>
        <hbox id="lookupActions" spacing="true" visible="false">
            <button action="lookupSelectAction"/>
            <button action="lookupCancelAction"/>
        </hbox>
    </layout>
</window>

What am I ignoring or not knowing?

Unfortunately, I can’t reproduce the problem. In my case, everything is commited.

Could you attach a demo project along with reproduction scenario?

I’m attaching what you requested. Is there an option like in studio v6 to export?

modules.zip (379.5 KB)

Unfortunately, I can’t open the project as it contains only modules.

I attach the complete project

treze.zip (1.1 MB)

Hi,

Thank you for the project. I reproduced the problem. You need to merge an entity before adding it to the container and edit. Below altered code:

createAction.withHandler(actionPerformedEvent -> {
    GNRCurrency entity = metadata.create(GNRCurrency.class);
    // Merge an entity before adding to the container
    GNRCurrency merged = getScreenData().getDataContext().merge(entity);
    // Add merged entity
    gNRCurrencyCollectionDc.getMutableItems().add(merged);
    // Edit merged entity
    gNRCurrencyTable.edit(merged);
});

Regards,
Gleb

Starting from CUBA 7.0.3, DataContext has create() method that creates an instance and merges it. So your code can be simpler:

@Inject
private DataContext dataContext;

createAction.withHandler(actionPerformedEvent -> {
    // Create and immediately merge an entity
    GNRCurrency entity = dataContext.create(GNRCurrency.class);
    // Add merged entity
    gNRCurrencyCollectionDc.getMutableItems().add(entity);
    // Edit merged entity
    gNRCurrencyTable.edit(entity);
});

Hi @gorelov,

I followed the above approach, and it works perfectly fine with create action, but it did not reflect any changes on edit action. The data is not committed and the screen reloads without changes.

Have I missed something? Is it related to this issue? I am also using cuba 7.0.7 version.

Regards,
Sanchit

1 Like

I suppose that the <data> element is defined with the readOnly="true" attribute. That’s why the commit functionality doesn’t work.

Gleb

1 Like

Hi Gleb,

Calling the loader again will reorder the records. If you add a new record at the end of the table, how can ensure when calling the reload, the order of the records in DataGrid stays the same?

Thanks,
Samy