Dynamic Dropdown Options
In this section you will learn how to make the dropdown options dynamic.
In addition to providing a fixed array of valid options in the code snippet, it is also possible to dynamically pick the options to be shown in the dropdown. These options will be retrieved when the user clicks a cell in the dropdown-controlled column and can be determined in any way. Rather than providing an array for the `validOptions` attribute, it’s also possible to provide a function. This function has the following signature:
1
(rowValues: (string | null)[]) => Promise<string[]>
Copied!
The single argument, `rowValues`, contains all of the current user-provided and generated fields for the output row that the user has selected. Here’s an example of a code snippet with two fields that builds upon the first. It uses the existing “Payment Type” field from above, but adds on an additional “Payment Provider” column that should contain the issuer of the credit or debit card if the purchase was made using a card:
1
{
2
name: 'a',
3
displayName: 'Payment Type',
4
description:
5
'The medium of payment which was used to complete the transaction',
6
validOptions: ['cash', 'credit', 'debit', 'paypal'],
7
},
8
{
9
name: 'b',
10
displayName: 'Payment Provider',
11
description:
12
'If the payment was made using a credit or debit card, contains the provider of that card',
13
validOptions: async (rowValues) => {
14
const paymentType = rowValues[0];
15
16
// If the user hasn't yet filled out the first column, do not let them populate this field yet
17
if (!paymentType) {
18
return [];
19
}
20
21
// If the payment wasn't made with a card, force the user to fill this field with "N/A"
22
if (paymentType !== 'credit' && paymentType !== 'debit') {
23
return ['N/A'];
24
}
25
26
// Allow the user to pick from the set of available card providers
27
return [
28
'Discover',
29
'Visa',
30
'MasterCard',
31
'Chase',
32
'American Express',
33
];
34
},
35
// If the card provider is other than the list we have available, allow the user to pick a custom option for this field and override the dropdown options we present them with
36
allowCustomOptions: true,
37
}
Copied!
In this scenario, the “Payment Type” column is dropdown controlled using the snippet above. Depending on the value the user enters (or the autocomplete engine synthesis) for the “Payment Type” column, the dropdown options available to the second “Payment Provider” will vary.
Following the logic provided in the custom `validOptions` provider function, the dropdown options for when “cash” is selected for “Payment Type” are limited to “N/A”:
Dropdown options for when “cash” is selected for “Payment Type” are limited to “N/A”:
When “credit” is entered for “Payment Type”, the various card providers are populated instead:
When “credit” is entered for “Payment Type”, the various card providers are populated instead
Using this pattern, it’s possible to construct complex logic to represent whatever kind of business rules may exist for your uploader. Since the function that can be provided to `validOptions` is asynchronous, it’s even possible to do things like make API requests, look things up in a database, or perform other external operations to determine what values to show to users.
Last modified 4mo ago
Copy link