So on the screen I’m building, I have a Transient field, age, which is calculated from the patient’s birthdate. The problem is, it doesn’t update visually “live.” I change the birthdate, the age field does not change. Leave the screen and come back in, and it’s fine. I need to update every time the birthdate is changed. I’ve subscribed to the birthdate field’s ValueChangeEvent, but I’m not finding a way I can use that to force update of that field.
I can advise you to subscribe to ItemPropertyChangeEvent for the data container, and also use the discard function to update data from the data container.
Example of using:
getAge() function in Customer class.
public Integer getAge() {
if (birthdate != null) {
LocalDate now = LocalDate.now();
Period diff = Period.between(birthdate, now);
return diff.getYears();
}
return null;
}
Make ageField field buffered in the XML descriptor so that you can use the discard function. I attach a link to the documentation for more information about discard.
Subscribe to ItemPropertyChangeEvent
public class CustomerEdit extends StandardEditor<Customer> {
@Inject
private TextField<Integer> ageField;
@Subscribe(id = "customerDc", target = Target.DATA_CONTAINER)
public void onCustomerDcItemPropertyChange(InstanceContainer.ItemPropertyChangeEvent<Customer> event) {
if ("birthdate".equals(event.getProperty())) {
ageField.discard();
}
}
}
I created a test project for Customer entity with birthdate and age fields. update-transient-field.zip (339.1 KB)