我正在嘗試進行自定義編輯頁面。我抓取對象ID並創建對象的一個實例。但是,當我嘗試爲選擇框指定一個值時,該框呈現爲多選。我需要它默認基於孩子的選擇,但只能是單選。我試過multiple =「false」,但我的程序只是忽略它。Grails從數據庫加載選擇框使其可以多選
我的對象是一個樣本,其中包含與每個樣本關聯的參數(子)。我有一個算法來抓取每個參數,並建立每個唯一名稱,值和信息的列表。有些樣本只有參數有值而沒有信息,所以它們進入一張地圖,而有值和信息的樣本進入另一張地圖。
這裏是算法和我的行動:
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>
您是否嘗試過在標籤中刪除「multiply」? –