How to access grails Arraylist of Object from JavaScript -


my grails controller mycontroller.groovy has following action:

def index = {        def mymodel = new mymodel()     // res below list of mychild objects service call     def childlist = jacksonobjectmapper.readvalue(res, new typereference<list<mychild>>() {})     mymodel.setmychildlist(childlist)      render(view: 'index', model: [mymodelinstance: mymodel]) } 

this renders index.gsp having access mymodel through mymodelinstance.

in index.gsp, have javascript method below:

<g:javascript>     function somejavascriptfunction(){         var message = $.parsejson("${mymodelinstance.mychildlist grails.converters.json}");         console.log("i grails: "+message);     } </g:javascript> 

considering have 2 elements in list, prints:

i grails: [object object],[object object] 

but not want. how can access mychild objects in list , properties/values within mychild objects inside javascript function ?

do modifications in groovy , js code.

def index = {                        def mymodel = new mymodel()                     // res below list of mychild objects service call                     def childlist = jacksonobjectmapper.readvalue(res, new typereference<list<mychild>>() {})                     mymodel.setmychildlist(childlist)                      render(view: 'index', model: [mymodelinstance: mymodel grails.converters.json])             } 

in javascript

  // model rendered groovy action.   var mymodelinstance = ${mymodelinstance};   //now iterate on json object values   for(var = 0 ; < mymodelinstance.length; i++){      console.log(mymodelinstance[i]);     /*if list contain nested object (groovy map inside list)     key , values as,*/     console.log("key:",mymodelinstance[i].keyname);     console.log("value:",mymodelinstance[i].valuename);   }  </script> 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -