How to make a table field with dynamic dropdown and force unique values #266
Replies: 2 comments
-
fields:
- name: portPairs
type: enum
outputObject: true
values:
- hostType: ANY
primaryPort: CL1-C
secondaryPort: CL2-C
- hostType: Windows
primaryPort: CL1-A
secondaryPort: CL2-A
multiple: true
placeholderColumn: "*"
- name: flat
type: expression
runLocal: true
expression: $(portPairs).flatMap(({ primaryPort, secondaryPort }) => [primaryPort, secondaryPort])
- name: filtered
type: expression
runLocal: true
ignoreIncomplete: true
expression: ( (a1=[],a2=[]) => { return a1.filter(x => !a2.some(y => y.storagePort === x))} )($(flat),$(serverPaths))
- name: serverPaths
label: Server WWN Mapping
type: table
required: true
dependencies:
- name: portPairs
isValid: true
tableFields:
- name: serverWWPN
label: Server WWPN
type: text
- name: storagePort
label: Storage Port
type: enum
from: filtered let me break this down: PortpairsAn array of dictionaries, FlatAn example of how to flatten the portpair "ports" to an array => not 100% relevant to this example at first, but i will cover another issue that you might encounter at some point. Because flat will become FilteredHere we use an inline function that we feed with field references. Examine it, it's cooler that it looks at first sight. Example of inline functions // as simple inline function
(x, y) => { return x + y }
// here we also use the function with references to fields f1 and f2
( (x, y) => { return x + y } )( $(f1), $(f2) ) Problem : if a referenced field can not be evaluated or is undefined, the expression does not run by default. Let's go back to our inline function now ( (x, y) => { return x + y } )( $(f1), $(f2) )
// now that f1 and f2 can return undefined, due to ignoreIncompete: true, we should concider function defaults
// if not, the function can break, because undefined is not a number.
( (x = 1, y = 2) => { return x + y } )( $(f1), $(f2) )
// now the inline function will work because of defaults
( (x = 1, y = 2) => { return x + y } )( undefined, undefined ) => 3 Now let's analyze the expression of filtered. ( (a1=[],a2=[]) => { return a1.filter(x => !a2.some(y => y.storagePort === x))} )($(flat),$(serverPaths))
// inline function :
(a1 = [], a2 = []) => { return ... } // a1 = array1 and a2 = array2
// return logic
return a1.filter(x => !a2.some(y => y.storagePort === x)) // reduce array1 if one of it's values is used as storagePort in array2
// feed the function with values from fields "flat" & "serverPaths" => due to ignoreIncomplete it can be undefined
( (a1=[],a2=[]) => { return a1.filter(x => !a2.some(y => y.storagePort === x))} )(undefined,undefined) // result empty array [] serverPathsA table, with an enum field for storagePort, coming from the field |
Beta Was this translation helpful? Give feedback.
-
Now the dropdown does not contain "CL1-A" anymore |
Beta Was this translation helpful? Give feedback.
-
let me break this down:
Portpairs
An array of dictionaries,
multiple
is true, so it becomes an arrayFlat
An example of how to flatten the portpair "ports" to an array => not 100% relevant to this example at first, but i will cover another issue that you might encounter at some point. Because flat will become
undefined
as long as no portpairs as selected. Now sometimes that's the goal. In this case, that's fine.Filtered
Here we use an inline function that we feed with field references. Examine it, it's cooler that it looks at first sight.
Example of inline functions
Problem : if a referenced field can not be evaluated or is undefined, the expression does not run by default.
This is covered by adding property
ignoreIncomplete
. This will ignore unevaluated references and set the value toundefined
Let's go back to our inline function now
Now let's analyze the expression of filtered.
serverPaths
A table, with an enum field for storagePort, coming from the field
filtered
. Each time serverPath has an entry, the used storagePort is removed from the dropbox and thus can only be used onceBeta Was this translation helpful? Give feedback.
All reactions