Pergunta

How do I display the selected value of the drop down list on gsp?

<g:select id="plantselect"
name="plant" from="${plantList.list()}" 
value="${plant.id}" />

Can't I to do something like ${plant} to display the selected value on view?

Foi útil?

Solução

You can use the following code in order to display the drop down value:

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Sample title</title>
  <script>
  function getValue(value)
  {
    alert(value);
    document.getElementById("test").style.display = "inline";
    document.getElementById("test").innerHTML  = value;

   // $("#test").html(value);

  }
 </script>
 </head>
 <body>
  <h1>Sample line</h1>
  <g:select id="plantselect"
        name="plant" from="${plantList.list()}" 
        optionKey="id" optionValue="id" onchange="getValue(this.value)"/>
 <!-- if you want to other field than id then change id to that field.-->
  <br/>

  <div id="test" style="display: none">
  </div>
 </body>

</html>

Outras dicas

you should pass that selected id simply to your gsp form controller if it another domain value then access it using the

domain.thislistdomain.id in your jsp Student.course.id or some value selected and pass that id to the value of the g:select

:)

or on gsp itself you can do the following :

<g:javascript>
    $( "gselectidhere" ).change(function() {
      alert( "You selected"+this.val());
      //if you want to process and communicate at selection with the controller please add ajax post or get call here ...
    });

</g:javascript>

//or you can youse g:remote tag and please refer here :

Grails remotefunction to populate dropdownlist with Jquery

Sure, try this:

<g:select id="plaintselect" name="plant.id" from="${Plant.list()}" optionKey="id" required="" value="${plant?.id}" class="many-to-one"/>

Don't forget to override toString() in Plant class, cos this value is used by defolt to build optionValue for select.

See http://grails.org/doc/latest/ref/Tags/select.html for more info

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top