openxava

How to hide properties depending on a different property value


In OpenXava, I have a class having a "certificateType" property, with a set of fixed values from an enumeration.

In the same class, there is another property that have to be displayed if the certificateType has a specific value, hidden in other cases.

Can you suggest the best approach?

Thanks


Solution

  • You have to add an @OnChange action to your certificateType property. Then in the code of your action you can hide or show any property using getView().setHidden("theProperty", true).

    In your entity write your property:

    enum CertificateType { A, B, C }
    @OnChange(OnChangeCertificateTypeAction.class)
    CertificateType certificateType;
    

    Then write an action like this:

    public class OnChangeCertificateTypeAction extends OnChangePropertyBaseAction { 
     
        public void execute() throws Exception {
            CerificateType newType = (CerificateType) getNewValue(); 
            if (newType == null) return;
            getView().setHidden("propertyNotForA", newType == CerificateType.A);
        }
     
    }
    

    For more information about how to use @OnChange look at the reference documentation, here: https://openxava.org/OpenXavaDoc/docs/view_en.html#View-Property%20customization-Property%20value%20change%20event