Mathias | 17 Aug 2012 13:52
Picon

Catch rows in Javascript

Hi,


How can I catch rows in Javascript code ?

Thanks

Mathias

--
 
 
 
Anthony | 17 Aug 2012 18:56
Picon

Re: Catch rows in Javascript

How can I catch rows in Javascript code ?

What does this mean? Can you give an example? 

--
 
 
 
Alan Etkin | 19 Aug 2012 15:20
Picon
Gravatar

Re: Catch rows in Javascript

Do you want to access DAL Row objects in the browser with javascript?

I think that the simplest way is to convert a Rows object to dict and do a json dump of it, then assign the data to a js variable in the view

controller

import simplejson
myrows
= db(query).select().as_dict()
return dict(data=simplejson.dumps(myrows))

view

{{=SCRIPT("var mydata = %s;" % data)}}



El viernes, 17 de agosto de 2012 08:52:56 UTC-3, Mathias escribió:
Hi,

How can I catch rows in Javascript code ?

Thanks

Mathias

--
 
 
 
Mathias Van Daele | 19 Aug 2012 19:09
Picon

Re: Re: Catch rows in Javascript

Hello Alan, Anthony

Thanks both for your reply !!!

In the meantime I also discovered that I have to use JSON for this specific situation.

I did it like this :


Controller :

def locations():
    locations=db(db.value.cat==2).select().as_list()
    import gluon.contrib.simplejson
    return gluon.contrib.simplejson.dumps(locations)

In the script from the view, I could catch the locations like this :

VIEW:

 var locations;

...

 $.getJSON('/cascade/default/locations',
      function(data){ locations=data;
      var select = document.getElementById("reaction_value");      
      select.options.length = 0; // clear out existing items
      for(var i=0; i < locations.length; i++) {
                var d = locations[i];
                select.options.add(new Option(d.val,d.id));
                 }                
             });


....

Best regards,

Mathias





2012/8/19 Alan Etkin <spametki-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:
> Do you want to access DAL Row objects in the browser with javascript?
>
> I think that the simplest way is to convert a Rows object to dict and do a
> json dump of it, then assign the data to a js variable in the view
>
> controller
>
> import simplejson
> myrows = db(query).select().as_dict()
> return dict(data=simplejson.dumps(myrows))
>
> view
>
> {{=SCRIPT("var mydata = %s;" % data)}}
>
>
>
> El viernes, 17 de agosto de 2012 08:52:56 UTC-3, Mathias escribió:
>>
>> Hi,
>>
>> How can I catch rows in Javascript code ?
>>
>> Thanks
>>
>> Mathias
>
> --
>  
>  
>  

--
 
 
 

Gmane