我正在尝试制作一个自定义编辑页面。我获取对象ID并创建该对象的实例。但是,当我尝试为选择框指定一个值时,该框将呈现为多选。我需要它默认为基于子项的选择,但只能是单选。我已经尝试过multiple =“false”,但是我的程序只是忽略了它。
我的对象是一个样本,其中包含与每个样本相关联的参数(子代)。我有一个算法,可以抓取每个参数并构建每个唯一名称,值和信息的列表。一些样本仅具有带值的参数,而没有信息,因此它们进入一个图,而具有值和信息的样本进入另一个图。
这是算法和我的 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])
}
如果我不指定值,则列表会很好。但是,当我指定一个值时,正确的值将突出显示,但此框是可多次选择的。
这是我在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>
最佳答案
sampleInstance?.sampleParameters
返回一个Collection
对象,因此,如果Grails在Collection
属性中检测到value
并且尚未设置multiple
属性,它将为您设置multiple
属性。因此将您的选择呈现为多选。
设置multiple="false"
将无济于事,因为Grails仅允许该属性直接通过,因此您获得了<select multiple="false" ...>
标记,仅该属性的存在可能会导致浏览器将其呈现为多选。
尝试仅从sampleParameters
中传入单个实例作为value
属性,以具有单个选择字段。
关于grails - 从数据库中加载Grails的选择框使其成为多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12116802/