sql - How to traverse Grails maps in jQuery -


so, obtain set of results in controller via sql , pass gsp, stick data hidden field , looks so: -

[ {forms_idx=0, form_id=21, events_idx=0, event_id=110, object_id=2, value=null}, {forms_idx=0, form_id=21, events_idx=1, event_id=109, object_id=3, value=null},  {forms_idx=1, form_id=22, events_idx=0, event_id=112, object_id=2, value=null},  {forms_idx=1, form_id=22, events_idx=1, event_id=111, object_id=1, value=null},  {forms_idx=2, form_id=23, events_idx=0, event_id=114, object_id=2, value=null},  {forms_idx=2, form_id=23, events_idx=1, event_id=113, object_id=3, value=null} ] 

i pass through jquery function via this: -

$(document).ready(function () { testing($("#myresults").val()); }); 

i know works because i've tried simple static value , put alert testing function.

my question is, how put simple loop of kind function address results in data i've passed it?! i've tried each etc not working , i'm missing basic, suggestions? i'm trying...

function testing(results){     $.each(results, function(index, value){         alert(index + " " + value)     }) } 

i tried got value undefined back...

function testing(results){ alert(results); } 

so problem here hidden field holds string of data whilst want use json. options:

  • have separate method in controller returns json data:

    import grails.converters.json;  class mycontroller {     def showdata() {         def mydata = [some:'map']         render mydata json     } } 

    then in javascript:

    $.getjson('my/showdata',function(jsondata){     console.log( "data ready used: " + json.stringify(jsondata) ); }); 
  • or print out json in javascript variable instead of hidden field:

    <%@ page import="grails.converters.json" %> <r:script disposition="head">     var mydata = ${datafrommodel json};     console.log( "data ready used: " + json.stringify(mydata) ); </r:script> 
  • or have custom javascript parser convert string json object

    // todo create parser :) 

Comments