How to set link attribute of a field programmatically?

In XML for a field the attribut “link” and (“linkScreenOpenType”) can be set, which generates a link to the associated entity:


<fieldGroup id="fieldGroup" datasource="vehicleDs"  editable="false">
<column width="250px">
    <field id="id"/>
    <field id="brand" link="true" linkScreenOpenType="NEW_TAB"/>
   ....

How to set this attribute programmatically? FieldGroup.FieldConfig has no appropriate setter method.

Hello, Stefan.

I can suggest two ways to customize an entity link field.

  • If you set link=“true” in XML, then you can inject entityLinkField in screen controller and set screenOpenType.

@Named("fieldGroup.customer")
private EntityLinkField customerField;

@Override
public void init(Map<String, Object> params) {
   customerField.setScreenOpenType(WindowManager.OpenType.NEW_TAB);
}
  • If you want to create entityLinkField programmatically, then you need to add a custom field to fieldGroup in XML (field with any name and ‘custom’ attribute set to true):

<fieldGroup id="fieldGroup" datasource="orderDs">
   <column width="250px">
      <field id="date"></field>
      <field id="customerLink" custom="true"></field>
   </column>
</fieldGroup>

After that, inject fieldGroup in screen controller and add custom field generator:


@Inject
private FieldGroup fieldGroup;
@Inject
private ComponentsFactory componentsFactory;

@Override
public void init(Map<String, Object> params) {
   fieldGroup.addCustomField("customerLink", (datasource, propertyId) -> {
      EntityLinkField linkField = componentsFactory.createComponent(EntityLinkField.class);
      linkField.setDatasource(datasource, "customer");
      linkField.setScreenOpenType(WindowManager.OpenType.NEW_TAB);
      linkField.setCaption(messages.getTools().getPropertyCaption(datasource.getMetaClass(), "customer"));
      return linkField;
   });
}

You can see the full demo project at github.

Regards,

Gleb

Thx a lot!