Generic Behavior For Multiple TextField

Hi all,

Basically I have some requirements that calculate two fields and store the result in another field.
Example given

In some-ui.xml

 <textField id="sumUnitField" property="propertyA" dataContainer="someDc" />
 <textField id="pricePerUnitField" property="propertyB" dataContainer="someDc""/>

As far as I know, we can create Event Listener for each field and subscribe it to each component

@Subscribe("sumUnitField")
protected void onSumUnitFieldValueChange(HasValue.ValueChangeEvent<Integer> event) {
    if(event.getValue()!=null) {
       doingFunction();
    }
}

@Subscribe("pricePerUnitField")
    protected void onSumUnitFieldValueChange(HasValue.ValueChangeEvent<Integer> event) {
        if(event.getValue()!=null) {
          doingFunction();
        }
    }

But problem arise when I have 30 similar fields that have same behavior (calculate two fields), so in a easy way we can create another 30 similar event listener that call same function.

So my question here is, is there any optimal way how to solve this?

Thank you!

Hi!

If all fields connected to the same data container, the recommended way is to add ItemPropertyChange listener, e.g.:

@Subscribe(id = "visitDc", target = Target.DATA_CONTAINER)
private void onVisitDcItemPropertyChange(InstanceContainer.ItemPropertyChangeEvent<Visit> event) {
    if ("propA".equals(event.getProperty())
            || "prob".equals(event.getProperty())) {
        doingFunction();
    }
}

Gleb

1 Like

Awesome, thanks for your support.
Yes, all fields are on the same container