Dropdown list for entity editor string value

I have an entity say Apples.class

And apples has a field String Color

Is it possible, when using the generic editor, to have a dropdown list of colors that are generated from a database query?

e.g. instead of typing in color:Blue, have it be a dropdown list which is populated on init() with the various colors like it does for an enum. Basically I just want what it does for enums but with the separate values populated from a database query.

Hi,

This is possible, have a look here:
https://demo.cuba-platform.com/sampler/open?screen=custom-options-lookupfield

Bye
Mario

Or if it is expressable via jpql then take a look at this example: https://demo.cuba-platform.com/sampler/open?screen=related-entity-lookupfield

Hi Mario,

But how do I change it to a look up field in the standard entity editor?lookup

Hi,

in the FieldGroup it is a little different.

I attached you an example application.

The example is Customer entity with “preferredColor” attribute which is a String and should be selectable. The CustomerEdit code looks like this:

public class CustomerEdit extends AbstractEditor<Customer> {

@Inject
protected FieldGroup fieldGroup;

@Inject
protected ComponentsFactory componentsFactory;

@Inject
protected Datasource<Customer> customerDs;

private final String PREFERRED_COLOR_ATTRIBUTE_NAME = "preferredColor";

@Override
public void init(Map<String, Object> params) {
    initPreferredColorLookupField();
}

private void initPreferredColorLookupField() {

    LookupField preferredColorLookupField = createLookupFieldComponent();
    preferredColorLookupField.setOptionsMap(createPreferredColorOptions());

    preferredColorLookupField.setDatasource(customerDs, PREFERRED_COLOR_ATTRIBUTE_NAME);
    setLookupFieldToFieldGroup(preferredColorLookupField);
}

private void setLookupFieldToFieldGroup(LookupField preferredColorLookupField) {
    FieldGroup.FieldConfig preferredColorField = fieldGroup.getField(PREFERRED_COLOR_ATTRIBUTE_NAME);
    preferredColorField.setComponent(preferredColorLookupField);
}

private LookupField createLookupFieldComponent() {
    return (LookupField) componentsFactory.createComponent(LookupField.NAME);
}

private Map<String, String> createPreferredColorOptions() {
    Map<String, String> preferredColorOptions = new HashMap<>();
    preferredColorOptions.put("Green", "Green");
    preferredColorOptions.put("Blue", "Blue");
    preferredColorOptions.put("Yellow", "Yellow");
    preferredColorOptions.put("Red", "Red");
    return preferredColorOptions;
}
}

cuba-example-fieldgroup-custom-dropdown.zip (93.3 KB)

I hope this example helps you.

Bye
Mario