Acumatica - Show / hide custom data fields on the fly via code in events

Viewed 582

I have two custom fields, one is a drop down list, and the other is a text box, one of the items in the drop down list is "Other", I wanted to show the custom field only if "Other" is selected, I added an event handler on the FieldUpdated event for the drop down control, and got the following code:

protected void CROpportunity_MyCheckbox_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
{
  var row = (CROpportunity)e.Row;
}

Let's assume the first field name is UsrComb, and second as UsrText.

How can I show/hide other controls from this method?

It is a very similar situation to: Acumatica - FieldUpdated - Enable / Disable another control when a checkbox is ticked / un-ticked

1 Answers

For this, I would build a StringList class to define your list and values, and then use attributes to control the behavior.

First, the List class (combs)

public class Combs
{
    //build string list attribute
    public class ListAttribute : PXStringListAttribute
    {
        public ListAttribute() : base(new[]
            {
                Pair(Comb, "Comb"),
                Pair(Other, "Other")
            })
        { }
    }

    //declare constant values
    public const string Comb = "Comb";
    public const string Other = "Other";

    //build constant for values you want to compare in BQL
    public class other : PX.Data.BQL.BqlString.Constant<other>
    {
        public other() : base(Other) {; }
    }
}

Next, are the properties/attributes:

#region UsrComb 
[PXDBString]
[Combs.List]        
[PXUIField(DisplayName = "Comb")]
public virtual string UsrComb { get; set; }
public abstract class usrComb : PX.Data.BQL.BqlBool.Field<usrComb> { }
#endregion


#region UsrText 
[PXDBString]
[PXUIField(DisplayName = "Text")]
//pxdefault is required if you have a PXUIRequired attribute.
[PXDefault(PersistingCheck = PXPersistingCheck.Nothing)]
[PXUIRequired(typeof(Where<usrComb, Equal<Combs.other>>))]
[PXUIVisible(typeof(Where<usrComb, Equal<Combs.other>>))]
[PXUIEnabled(typeof(Where<usrComb, Equal<Combs.other>>))]
public virtual String UsrText { get; set; }
public abstract class usrText : PX.Data.BQL.BqlString.Field<usrText> { }
#endregion

PXUIRequired/Enabled/Visible attributes then check to see if the list equals the constant. You can use any combination of the values.

Put your fields on a screen, and ensure UsrComb has CommitChanges set to True. You will change the value to Other and it will then show the text field, and hide it when set back.

If you wish to blank the value after the dropdown is changed away from other, you can use the following code:

protected virtual void CROpportunity_UsrComb_FieldUpdating(PXCache sender, PXFieldUpdatingEventArgs e)
{
    if (e.NewValue == null)
        return;
    string Comb = (string)e.NewValue;

    if (Comb != Combs.Other)
        sender.SetValueExt<CROpportunityExt.usrText>(e.Row, "");
}
Related