Chris Olin
Chris Olin

Reputation: 43

Hide/Disable other fields based on Input field

I want to hide or update a field on the UI based on conditions of another field.

For example, if I have a field called Color:

[PXUIField(DisplayName="Color")]
[PXStringList("Red,Blue,Other")]
[PXDefault("Red")]

And text field for comments only shown when "Other" is selected, how is this accomplished?

Upvotes: 3

Views: 1732

Answers (1)

RuslanDev
RuslanDev

Reputation: 6778

The requested behavior can either be accomplished either with a series of event handlers or with a bunch of attributes. You can find several examples on how to subscribe to the RowSelected and FieldUpdated events in the T200 training course, available at Acumatica University and Acumatica Open University

Going with field attributes is a more convenient and way easier option for your particular scenario. I would recommend setting CommitChanges to True for the drop-down, so the Comments field is cleared and disabled/enabled immediately after the user updates Color. Also, it's very to have your Color declared after Comments, so the framework will process Comments field first and always clear the current Comments value after the Color field got updated.

public class Other : Constant<string>
{
    public Other() : base("Other") { }
}
public abstract class comments : IBqlField { }
[PXDBString(255, IsUnicode = true)]
[PXUIField(DisplayName = "Comments")]
[PXUIEnabled(typeof(Where<color, Equal<Other>>))]
[PXFormula(typeof(Default<color>))]
[PXDefault(PersistingCheck = PXPersistingCheck.Nothing)]
public string Comments { get; set; }

public abstract class color : IBqlField { }
[PXDBString(10, IsUnicode = true)]
[PXUIField(DisplayName = "Color")]
[PXStringList("Red,Blue,Other")]
[PXDefault("Red")]
public string Color { get; set; }

The only way to conditionally hide/show editor on a form is though the RowSelected event handler:

public void YourDAC_RowSelected(PXCache sender, PXRowSelectedEventArgs e)
{
    YourDAC row = e.Row as YourDAC;
    if (row == null) return;

    PXUIFieldAttribute.SetVisible<YourDAC.comments>(sender, row, row.Color == "Other");
}

I believe, in the T200 training course, there are several examples on the PXUIFieldAttribute.SetVisible method.

Upvotes: 3

Related Questions