0
votes

Hey Guys i am trying to populate dynamic fields in groovy GSP by using javascript and Groovy markup
*Here's the Effort 1 ->Works fine using plain html *

<r:script>
  function createField()
  {
    var variable=0; 
    var d="<input type='text' name='item["+variable+"]' id='item["+variable+"]'/>";
    reutrn d;
  } 
</r:script>

*Here's the Effort 2 ->JavaScript Error while using Groovy tags *

<r:script>
  function createField()
  {
    var variable = 0; 
    var d = "<g:textField name='item["+variable+"]' id='item["+variable+"]' />";
    return d;
  }
</r:script>

By Using above function browser ends up with the error :javascript uncaught syntaxerror unexpected token illegal
So i decided to encode the tag using Grails InLine codec encodeAs="JavaScript".

*Here's the Effort 3 ->Error using Groovy tags *

y<r:script>
   function createField()
   {
      var variable=0; 
      var d="<g:textField name='item["+variable+"]' id='item["+variable+"]' encodeAs="JavaScript"/>";
      return d;
   }
</r:script>

Problem with the third effort while encoding the tags, quotes besides the variable are also encoded so the output is something like u003b+variable+\u0026#39, this makes the nonidentical form fields which are unable to process further.
Any help will be appreciated.

1
You've misspelled return statement. - castt
Thanks for letting me know - Saubar
imho, you should go with Effort 1. i don't think you gain much by using g:textField - aldrin
thanks for the response aldrin but sometime theres is a situation where one have to implement groovy tags i;e <g:select from="${classname.list()}" value="${classname.attrib}"/> i am unable to find the alternate solution, please suggest if you have one - Saubar

1 Answers

0
votes

GSP and javascript are evaluated at different times which is causing this not to work. GSP is executed server side then the result is sent back to the client the JS is executed client side. If you view source of the page from your browser you will see that the createField function has the g:textField already replaced.