Retrieve Value from LookupField that uses an OptionsMap

Im using a KeyValue Entity container for my table I have no entities

image

Looks good but i have no idea how to get the selected value.

DESCRIPTOR

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/screen/window.xsd"
        caption="msg://newScreen.caption"
        messagesPack="com.company.dropdownmap.web.screens">
    <data>
        <keyValueCollection id="fileKc">
            <properties>
                <property name="fileId" datatype="uuid"/>
                <property name="fileName" datatype="string"/>
                <property name="fileTypeId" datatype="uuid"/>
            </properties>
        </keyValueCollection>
    </data>
    <layout expand="tblFile">
        <table id="tblFile" height="100%" width="100%" dataContainer="fileKc" editable="true">
            <columns>
                <column id="fileId"/>
                <column id="fileName"/>
                <column id="fileTypeId"/>
            </columns>
        </table>
        <button id="btnSave" caption="Save"/>
    </layout>
</window>

CONTROLLER

package com.company.dropdownmap.web.screens;

import com.haulmont.cuba.core.entity.KeyValueEntity;
import com.haulmont.cuba.gui.Notifications;
import com.haulmont.cuba.gui.UiComponents;
import com.haulmont.cuba.gui.components.Button;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.components.LookupField;
import com.haulmont.cuba.gui.components.Table;
import com.haulmont.cuba.gui.components.data.TableItems;
import com.haulmont.cuba.gui.model.InstanceContainer;
import com.haulmont.cuba.gui.model.KeyValueCollectionContainer;
import com.haulmont.cuba.gui.screen.*;

import javax.inject.Inject;
import java.util.*;

@UiController("dropdownmap_")
@UiDescriptor("new-screen.xml")
public class NewScreen extends Screen {

    Map<String, UUID> fileTypes;
    @Inject
    private UiComponents uiComponents;
    @Inject
    private KeyValueCollectionContainer fileKc;
    @Inject
    private Notifications notifications;
    @Inject
    private Table tblFile;

    @Subscribe
    public void onInit(InitEvent event) {
        fileTypes = new HashMap<>();
        fileTypes.put("Type 1", UUID.randomUUID());
        fileTypes.put("Type 2", UUID.randomUUID());
        fileTypes.put("Type 3", UUID.randomUUID());

        //INIT DUMMY DATA
        KeyValueEntity file1 = new KeyValueEntity();
        file1.setValue("fileId", UUID.randomUUID());
        file1.setValue("fileName", "Contract");

        KeyValueEntity file2 = new KeyValueEntity();
        file2.setValue("fileId", UUID.randomUUID());
        file2.setValue("fileName", "Performance Review");
        List<KeyValueEntity> keyValueEntities = new ArrayList<>();
        keyValueEntities.add(file1);
        keyValueEntities.add(file2);

        fileKc.setItems(keyValueEntities);
    }

    @Install(to = "tblFile.fileTypeId", subject = "columnGenerator")
    private Component tblFileFileTypeIdColumnGenerator(Object object) {
        LookupField lookupField = uiComponents.create(LookupField.class);
        lookupField.setOptionsMap(fileTypes);
        //THIS CLEARS THE SELECTION FROM THE DROPDOWN
        /*lookupField.addValueChangeListener(valueChangeEvent -> { //valueChangevEvent.getValue does not exist
            file.getItems().get(0).setValue("fileTypeId",//OBJECT ID);
        });*/
        return lookupField;
    }

    @Subscribe("btnSave")
    public void onBtnSaveClick(Button.ClickEvent event) {
        //I WANT TO SEE THE SELECTED VALUE ON THE DROPDOWN
        //I SEE IT ON THE TABLE BUT THE VALUE IS NULL
        //TableItems items = tblFile.getItems();
        //Collection items1 = items.getItems();

        notifications.create().withCaption(fileKc.getItems().get(0).getValue("fileTypeId").toString()).show();
    }

    /* Garbage
    @Subscribe(id = "fileKc", target = Target.DATA_CONTAINER)
    public void onFileKcItemChange(InstanceContainer.ItemChangeEvent<KeyValueEntity> event) {
        fileKc.getItem(event.getItem()).setValue("fileTypeId",event.getItem().getValue("fileTypeId"));
    }
     */
}

Thank you for the Help.

Should use Consumer<HasValue.ValueChangeEvent<UUID>>.

lookupField.addValueChangeListener((Consumer<HasValue.ValueChangeEvent<UUID>>) event -> {
        UUID value = event.getValue();
    });

If the default value of “fileTypeId” is set, data can be obtained. So after selecting the data, the problem can be solved if the “fileTypeId” can be set in.

fileTypes = new HashMap<>();
    fileTypes.put("Type 1", UUID.randomUUID());
    fileTypes.put("Type 2", UUID.randomUUID());
    fileTypes.put("Type 3", UUID.randomUUID());

    //INIT DUMMY DATA
    KeyValueEntity file1 = new KeyValueEntity();
    file1.setValue("fileId", UUID.randomUUID());
    file1.setValue("fileName", "Contract");
    //file1.setValue("fileTypeId", UUID.randomUUID());

    KeyValueEntity file2 = new KeyValueEntity();
    file2.setValue("fileId", UUID.randomUUID());
    file2.setValue("fileName", "Performance Review");
    //file1.setValue("fileTypeId", UUID.randomUUID());

    List<KeyValueEntity> keyValueEntities = new ArrayList<>();
    keyValueEntities.add(file1);
    keyValueEntities.add(file2);

    fileKc.setItems(keyValueEntities);
2 Likes

Thank you very much for your response.

its a better implementation of

 //THIS CLEARS THE SELECTION FROM THE DROPDOWN
        /*lookupField.addValueChangeListener(valueChangeEvent -> { //valueChangevEvent.getValue does not exist
            file.getItems().get(0).setValue("fileTypeId",//OBJECT ID);
        });*/

I have replaced it with…

lookupField.addValueChangeListener((Consumer<HasValue.ValueChangeEvent<UUID>>) event -> {
            UUID value = event.getValue();
            ((KeyValueEntity) object).setValue("fileTypeId", value);
        });

But the dropdown still becomes blank… when I select the same item again… Then the value is stored and I can retrieve it from the KeyValueContainer… Why do I have to select the same value 2 times to store the value I need?

Here’s a video of the annoying disappearance of the selected value…
the value is stored but it disappears from the dropdown.
I have to select it twice so it actually displays the value on the dropdown.
ezgif-4-786f599e13cd

@Install(to = "tblFiles.fileType", subject = "columnGenerator")
private Component tblFilesFileTypeColumnGenerator(KeyValueEntity kve) {
    LookupField lookupField = uiComponents.create(LookupField.class);
    //I HAVE TO SET THE VALUE EVEN AFTER THE VALUE CHANGES
    // seems to trigger regenerate the column
    lookupField.setValue(kve.getValue("fileTypeId"));
    lookupField.setOptionsMap(fileTypes);
    lookupField.addValueChangeListener((Consumer<HasValue.ValueChangeEvent<UUID>>) event -> {
        kve.setValue("fileTypeId", event.getValue());
    });
    return lookupField;
}

When I upload another file… the SAME thing… happens again… that the dropdown value is hidden… but the value is stored properly on the KeyValue Collection Container… Is this a bug or im not doing this right?