2

I try to pass an array from a controller to a javascript code into a gsp page. I tried with
Controller :

           def stat(){
           def listSubject = [["Physique", 10, 85, 1], ["Arabe", 20, 14, 3]] 

            [listSubject]
                             }

stat.gsp :

               var data=${listSubject} ;

But it looks like considering the data as if it was a string and not an array. I tried also to add

             var data=${listSubject.encodeAsJavaScript()} ; 

But No result is matched. Please i need your helps :( Thanks

2
  • Look at the actual HTML of the resulting page. That will say a good bit about the problem, and such output should be included in the question. Commented Aug 20, 2014 at 21:58
  • [listSubject] has no meaning outside of the context of the controller action. It will have a meaning if the model (of MVC) is tailored appropriately, which means [listSubject: listSubject] will create a key<>value in the model which can then be accessed in the gsp as ${listSubject}. Commented Aug 20, 2014 at 22:10

1 Answer 1

4

The best way to pass data in gsp to javascript is JSON. Try this:

def stat(){
    def listSubject = [["Physique", 10, 85, 1], ["Arabe", 20, 14, 3]] as grails.converters.JSON
    [listSubject:listSubject]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.