Reputation: 493
I am trying to make a custom edit page. I grab the object ID and make an instance of the object. But when I try to specify a value for the select box, the box renders as multi-selectable. I need it to default to the selection based on the child, but only be single select. I have tried multiple="false" but my program is just ignoring it.
My object is a sample that contains parameters (children) that are associated with each sample. I have an algorithm that grabs each parameter and builds lists of each unique name, value and information. Some samples only have parameters with a value and no information, so they go into one map, while samples that have a value and information go into another map.
here is the algorithm and my action:
def updateSample = {
def sid = params.sample.id // get the id of the sample object
def sampleInstance = Sample.get(sid)// creates instance
def children = sampleInstance?.sampleParameters
/* ------ gets a list of unique parameter names ------*/
HashMap<String, ArrayList<SampleParameter>> oldMap = new HashMap<String, ArrayList<SampleParameter>>(); // for single parameter options
HashMap<String, HashMap<String, ArrayList<SampleParameter>>> map = new HashMap<String, HashMap<String, ArrayList<SampleParameter>>>(); // for multiple parameter options
for (def result : SampleType.get(sampleInstance.sampleType.id).sampleParameters.sort {it.value}) { // only iterate over assigned sample paramters
if (result.information != null) { // we are working with 3 parameters
if (!map.containsKey(result.name)) { // if map does not already contain the key
map.put(result.name, new HashMap<String, ArrayList<SampleParameter>>()); //add the name and a map to hold the values from the table's information column
}
if (!map.get(result.name).containsKey(result.value)) {
map.get(result.name).put(result.value, new ArrayList<SampleParameter>());
}
map.get(result.name).get(result.value).add(result);
} else {// otherwise we are only working with two parameters
if (!oldMap.containsKey(result.name)) { // if the name does not already exist in oldMap, add it
oldMap.put(result.name, new ArrayList<SampleParameter>()); // holds values
}
oldMap.get(result.name).add(result); //adds value to the list
}
}
/* invokes template and passes a map to be rendered inside of <div id="parameter"> in sample.gsp */
render(template:'updatesample' , model:[sid:sid,sampleInstance:sampleInstance,children:children,
oldMap:oldMap, map:map])
}
The lists build just fine if I don't specify a value. But when i specify a value, the correct value is highlighted but the box is multiselectable.
Here is my template code that gets rendered on the gsp;
<g:each in="${oldMap?.sort()}">
<tr>
<td align="right" valign="top"><b>${it.getKey()}:</b></td>
<td><g:select optionKey="id" optionValue="value" name="sampleParameters" id="parameter" value="${sampleInstance?.sampleParameters}" from='${it.getValue()}' /></td>
</tr>
</g:each>
<g:each var="valueMap" in="${map?.sort()}">
<tr><td align="right" valign="top"><b>${valueMap.getKey()}:</b></td></tr>
<g:each var="infoMap" in="${valueMap?.getValue()?.sort()}">
<tr>
<td align="right" valign="top">${infoMap.getKey()}:</td>
<td><g:select multiple ="false" noSelection="${['':'Select One...']}"optionKey="id" optionValue="information" name="sampleParameters" id="parameter" value="${sampleInstance?.sampleParameters}" from="${infoMap?.getValue()?.sort(){it.information}}" /></td>
</tr>
</g:each>
</g:each>
Upvotes: 0
Views: 937
Reputation: 3881
sampleInstance?.sampleParameters
returns a Collection
object and so, if Grails detects a Collection
in the value
attribute and the multiple
attribute hasn't been set, it will set the multiple
attribute for you. Thus rendering your select as a multi-select.
Setting multiple="false"
won't help as Grails just allows the attribute to pass straight through so you get a <select multiple="false" ...>
tag and just the presence of the attribute may cause the browser to render it as a multi-select.
Try passing in only a single instance from sampleParameters
as the value
attribute to have a single select field.
Upvotes: 2