Please your users with default filter values

In your never-ending quest to make your Nitro App Builder-based apps as user-friendly as possible, reducing the amount of typing and clicking required for users to get to the data they’re seeking can go a long way. If you know your users will be routinely setting a grid filter to, say, a specific date or date range relative to the current date, or a specific department based on where they work, etc., there are many ways to save them the hassle of having to manually set the filter values each time.

Filter Settings

The mechanisms in place in NAB to set filter values include hard-coding a value in the app, setting a date relative to the current date, calling a simple RPG routine to programmatically set filters based on the user’s business role, or pulling a value from an App Variable. In this month’s tip we’ll take a quick gander at each option.

Hard-coding an initial value

The easiest way to set a default on a user filter is by simply specifying something on the filter’s Initial Value setting. This is done in Nitro App Builder at the widget-level by going to the Filters tab and clicking on the “Initial Value” cell, which brings up a window for specifying the value. Any value set here will be applied universally for all users. This is sometimes used to ensure auto-loading widgets with lots of data are limited in what they show initially for performance reasons.

Hard-coded value - Grid

For filters over date columns, the Initial Value window is a bit more elaborate. You can of course set a specific date, but as the months go a hard-coded date may fall too far in the past. So as an alternative you can configure a default value that is relative to the current date. For example, you could take the current date, subtract a month, then move to the first day of the month to ensure the default value is always the first day of the prior month…

Hard-coded value - Grid Date

Calling an RPG program to set initial values

Another widget-level option for defaulting filter values is calling an RPG program modeled after source member EXNABIV. Your program might determine the current user and then set values according to the user’s location or business role.

To use this approach, simply make a copy of the source for example program EXNABIV, giving the copied member a name appropriate for the widget. Then, in the “Process” procedure, insert a “setValue” line for each filter you want to default. The first parameter is the filter name, as shown in the list of columns in the widget; The second parameter is the desired default value for the filter, which is specified as a character string, regardless of type. So for a numeric value you’d specify ‘123’, and for a date value you’d supply a string of the date in *ISO format, i.e. ‘2022-05-23’.

EXNABIV

After compiling this program, you must link it to the widget by clicking on the Settings drawer on the right side of the Filters tab, then specifying the program name in the Initial Value PGM field…

Initial Value PGM

Setting an initial value through an App Variable

You may sometimes encounter cases where you wish to set a filter’s default value at the app-level instead of at the widget-level. For example, if you have a widget that is brought up inside your app as a pop-up after clicking inside another widget, it may be more appropriate to set default filter values for the pop-up widget based on an earlier filter action. In such instances, App Variables can be used to set the initial value for the filter.

Once you’ve got an appropriate App Variable defined and set through an appropriate Behaviors action, the process of linking its value to a filter in the widget entails calling up the widget’s settings (again, this is while editing the app, not the widget), then clicking the “Link to App Variables” button. Scroll down to the Set Filters section and assign App Variables as desired.

App Variable to Set Filter

Note that if your app has a startup program specified in the Startup/Close drawer in Behaviors, then you could have your startup program effectively set default filter values through App Variables as well. This is effectively an app-level equivalent to the widget-level EXNABIV program approach described above.

One final tidbit to mention regarding default values: On the widget-level Settings drawer there is an “Allow Reset” checkbox that controls whether a Reset button appears alongside the filter fields. If activated, clicking on the Reset button will cause all filter fields to return to the initial values that are either hard-coded in the widget or set via the Initial Value program. Note that this does not apply to App Variable-based filter values, which are only applied when an App Variable value is actively changed.