0

カスタム編集ページを作成しようとしています。オブジェクト ID を取得し、オブジェクトのインスタンスを作成します。しかし、選択ボックスの値を指定しようとすると、ボックスは複数選択可能としてレンダリングされます。子に基づいてデフォルトで選択する必要がありますが、単一選択のみです。multiple="false" を試しましたが、プログラムはそれを無視しています。

私のオブジェクトは、各サンプルに関連付けられたパラメーター (子) を含むサンプルです。各パラメーターを取得し、それぞれの一意の名前、値、および情報のリストを作成するアルゴリズムがあります。一部のサンプルには、値を持つパラメーターのみがあり、情報がないため、1 つのマップに入りますが、値と情報を持つサンプルは別のマップに入ります。

ここにアルゴリズムと私の行動があります:

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>
4

1 に答える 1

2

sampleInstance?.sampleParametersはオブジェクトを返すCollectionため、Grails が属性に を検出し、属性Collectionが設定されていない場合は、属性が設定されます。したがって、選択を複数選択としてレンダリングします。valuemultiplemultiple

multiple="false"Grailsは属性をそのまま通過させるだけなので、設定は役に立ちません。<select multiple="false" ...>タグを取得すると、属性が存在するだけでブラウザがそれを複数選択としてレンダリングする可能性があります。

から単一のインスタンスのみを属性sampleParametersとして渡して、単一の選択フィールドを持つようにしてください。value

于 2012-08-25T03:59:36.800 に答える