daryn
(Daryn Leaity)
May 2, 2018, 4:51am
#1
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.
mario
(Mario David)
May 2, 2018, 5:06am
#2
mario
(Mario David)
May 2, 2018, 5:09am
#3
daryn
(Daryn Leaity)
May 2, 2018, 6:07am
#4
Hi Mario,
But how do I change it to a look up field in the standard entity editor?
mario
(Mario David)
May 3, 2018, 7:56am
#5
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