Hello!
I'm trying to create a dynamic field that gets converted to a single select list by the code below.
Does anyone know the exact format of the map this code is expecting?
The list needs to be dynamic because it is generated from a continuously changing list from a large SQL table.
I was able to get it working with a rest endpoint but since the sql request is already elsewhere I would like to generate the map at that time.
Thank you!
Here is the code I'm trying to use.
def bPartNum = getFieldById("");
def map = [:]
def result = []
for (int i = 0; i < aItemNo.size() ;i++)
{
result << [value:aItemNo[i], html:aItemNo[i],label:aItemNo[i]]
}
//Response.ok(JsonOutput.toJson(map)).build()
map.put('items', result)
bPartNum.convertToSingleSelect(map)
There are 2 ways to perform the conversion
1) have a rest endpoint that returns a structure like:
[ items: [ [value: valueString, html: valueHtml, label: labelString, icon: iconHref] ],
total: numberofItems,
footer: "Prompt when empty
]
The convertToSingleSelect needs to include ajaxOption parameter like specified in the documentation page.
2) use a simple map object.
//using your aItemNo assuming it is an array of items you want in the map
def map = aItemNo.inject(['':'None']){ map, item ->
map[item]= item
map
}
field.convertToSingleSelect().setFieldOptions( map)
The map will end up looking like this:
[ '': 'None', item1: "item1", item2: "item2", item3: "item3"]
If aItemNo includes key/id and label, you could form the map to look like this:
[ '': 'None', item1key: "Label for item1", item2key: "label for item2", item3Key: "labe for litem3"]
Thank you! I have it working now!
java.util.Map map = values.inject(['':'None']){ map, item ->
map[item]= item
map
}
def bPartNum = getFieldById("");
bPartNum.convertToSingleSelect().setFieldOptions(map)
It didn't work at first with def map so I had to define the map type as java.util.Map
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Glad it helped. I rarely bother with static typing. The "static type check" errors can often be ignored.
Please remember to accept the answer if it helped you. "Solved" questions are easier for others to find.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.