How to force visual update of Transient fields?

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.

Any advice? :slight_smile:

Hi, @jon.craig!

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:

  1. 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;
    }
  1. 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.
  2. 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)

Regards,
Gleb

2 Likes

Thank you for the answer, Gleb!

I did fix it in the meantime, though I used a different approach.

I did (in the PatientEdit controller):

    @Inject
    private TextField<Long> ageField;

    @Subscribe("dateOfBirthField")
    public void onDateOfBirthFieldValueChange(HasValue.ValueChangeEvent<Date> event) {
        ageField.setValue(getEditedEntity().getAge());
    }

This gave me the desired behavior as well.