Suggestion : manage OptionsGroup within FieldGroup

Hi
The FieldGroup is very handy to manage most entity fields - i.e most of DATATYPE attributes, associations, but for some reason there is no support of enum attributes.

A quite common case is having an entity field that is an enum, that one want to have in the GUI along the fields of the FieldGroup

Currently, to manage this case, it is necessary to use an OptionsGroup, which means e.g that if you want to display :

  • 3 String attributes (TextField)
  • 1 enum attribute (Options / Radio)
  • 5 String attributes

For that case, you will have to lay out 1 field group for the first 3, 1 options group for the 4th, and another field group for the last 5 attributes. For a simple case this is cumbersome.

As Studio’s fields editor within FieldGroup is already able to manage different kind of components, it could very well generate an options group mapped to an enum attribute. Maybe with limited choice in layout, e.g horizontal or vertical without further customisation. And if one wants to customize more the options group, he could revert to define manually the options group, as for any component.

Hi Michael,

By default FieldGroup generates LookupField components for enumeration fields.
For now, if you want to use OptionsGroup (or even OptionsList) instead of LookupField you can only replace automatically generated field with custom field.
In this case you can use one FieldGroup for all fields.

Example XML:


<fieldGroup id="fieldGroup"
            datasource="planDs">
    <column width="250px">
        <field id="title"/>
        <field id="status" custom="true"/> 
    </column>
</fieldGroup>

Here we set custom=“true” for status field. And then we add custom field generator in the init method of Java Controller:


public class PlanEdit extends AbstractEditor<Plan> {
    @Inject
    private FieldGroup fieldGroup;
    @Inject
    private ComponentsFactory componentsFactory;

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

        fieldGroup.addCustomField("status", (datasource, propertyId) -> {
            OptionsGroup optionsGroup = componentsFactory.createComponent(OptionsGroup.class);
            optionsGroup.setDatasource(datasource, propertyId);
            return optionsGroup;
        });
    }
}

I’m still on the opinion that being able to choose between lookup and options for an enum in fieldgroup within studio is an interesting step forward. But in the meantime I would certainly use this solution of you, which is far more elegant that the workaround I was about to implement.
Thank you Yuriy

We will consider adding some option for FieldGroup field to change LookupField to OptionsGroup, but now you can always use custom fields to change default behaviour.

Hi
Is this API (addCustomField) being deprecated? What is the new API replacing this?

Hey!

See also the generator attribute

Thanks. Where should I put this code to? in the controller or it somewhere that can be reused?

public Component generateDescriptionField(Datasource datasource, String fieldId) {
    TextArea textArea = componentsFactory.createComponent(TextArea.class);
    textArea.setRows(5);
    textArea.setDatasource(datasource, fieldId);
    return textArea;
}

It should be added to screen controller. Also, you can use imperative way with setComponent() method: FieldGroup - CUBA Platform. Developer’s Manual

PasswordField passwordField = componentsFactory.createComponent(PasswordField.class);
passwordField.setDatasource(userDs, "password");
fieldGroup.getFieldNN("password").setComponent(passwordField);

sorry for resurrecting the dead, but this will work for generating an options group in a FieldGroup whether you are using an enum or an association. Hopefully others will find this useful. I was searching for a quick answer and this was the first post to come up.

public Component generateOptionsGroup(Datasource datasource, String fieldId) {
    OptionsGroup optionsGroup = componentsFactory.createComponent(OptionsGroup.class);
    optionsGroup.setDatasource(datasource, fieldId);
    //set Option Group orientation
    optionsGroup.setOrientation(OptionsGroup.Orientation.HORIZONTAL);
    //set edibility 
    optionsGroup.setEditable(true);
    //set required
    optionsGroup.setRequired(true);
    return optionsGroup;
}

after placing this in the screen controller, return to studio and enter the name in the generator field on the properties screen on the applicable field(s)

image

final product
image

1 Like