Dynamic,  Functionality

Search and autofill in text box based on List values

We are all used to web searches giving suggestions of possible values you want and Combo Boxes have a Search function, but Text inputs do not have anything built-in for this, so it should be useful to have this function as well.

In the example below, the user starts typing and all matching values come up below. At any time they can press Enter and have the first (or only) matching value populate automatically in the Text Box.

The process involves only three things – changes to the OnChange and Default of the Text box and a Label below. This example is from a test list I have, but will work on any list subject to size Delegation on the Distinct function in the Label (I am sure there are work-arounds if this is an issue).

Firstly, the OnChange of the example Text Box (txtManName) is: –

With(
   {
      wMatch: 
      If(
         !IsBlank(Self.Text),
         LookUp(
            Devices,
            StartsWith(
               ManufacturerName,
               Self.Text
            )
         ).ManufacturerName
      )
   },
   UpdateContext(
      {
         varMatch: 
         If(
            !IsBlank(wMatch),
            wMatch,
            Self.Text
         )
      }
   )
)

So a Variable is being updated to the first matching value (if present) in the list or if not present the existing text.

The Default of the Text box is: –

If(
   !IsBlank(varMatch),
   varMatch
)

If this is being used in a form to also display existing records, you would add: –

If(
   !IsBlank(varMatch),
   varMatch,
   Parent.Default
)

and also reset varMatch at Screen OnVisible and possibly on Form submit: –

UpdateContext({varMatch:Blank()})

The last bit is the Label Text: –

If(
   !IsBlank(txtManName.Text),
   With(
      {
         wResult: 
         Concat(
            Distinct(
               Filter(
                  Devices,
                  StartsWith(
                     ManufacturerName,
                     txtManName.Text
                  )
               ),
               ManufacturerName
            ),
            Result & ", "
         )
      },
      Left(
         wResult,
         Len(wResult) - 2
      )
   )
)

The reason for the With() statement is to get rid of the last comma.

I hope this is useful for you as an alternative to using a Combo Box.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *