the ajax callback action return a list [URGENT]

110 views
Skip to first unread message

sunda...@gmail.com

unread,
May 24, 2017, 4:23:05 AM5/24/17
to web2py-users
Hello,


i have a form, when i fill it, it will callback an ajax code, which will call an action, that action returns a list and it puts it on the target,

I can see the target when i put <div id="target"> </div> And it display the list perfectly ...

The problem is that i don't wan't the list being displayed, i just want to work with it in the view, i would like to be able to have the list in the view without having to pass by a div.




Thanks

Leonel Câmara

unread,
May 24, 2017, 9:52:42 AM5/24/17
to web2py-users
This is more of a javascript question than a web2py one. If you don't want to display the list in HTML just don't do it, just get the list using $.getJSON or something and do whatever you want to do with it in javascript.

Anthony

unread,
May 24, 2017, 3:06:30 PM5/24/17
to web2py-users
If you are using the ajax() function, note the third argument can be:
  • ":eval", in which case, you can return Javascript code to be executed (this code could, for instance, define a Javascript array).
  • A Javascript function, in which case, the returned data will be passed to that function.

Also, please don't label all of your posts as "URGENT". We get it -- everyone wants fast answers.


Anthony

Paco Bernal

unread,
May 26, 2017, 11:37:53 AM5/26/17
to web...@googlegroups.com
You have to use a view to work with the list returned by the function. Both, the view and the funcion with the same name or using response.view="view_name.html" This is the easier way.

Paco Bernal

unread,
May 26, 2017, 11:52:31 AM5/26/17
to web2py-users
Or you can use ':eval'  as the third argument as Anthony says and return '%s%s' % (ASIGNJS(your_js_var=your_python_list), 'your_js_code_for_manipulating_the_list')

This way you can manipulate your list with js in client side, but I prefer using a view if the html code is more than one or two lines :)


El miércoles, 24 de mayo de 2017, 10:23:05 (UTC+2), sunda...@gmail.com escribió:

sunda...@gmail.com

unread,
May 30, 2017, 8:46:08 AM5/30/17
to web2py-users

In my controller, i have a list which i called tools which containes values.

I would like to return this list like you said.

In my view i would like to retrieve the value and to put the values inside variable( with loop i think)

But when i put eval and i call the function javascription from the controler : the name of the list is displayed ( it takes the string and not the content :'( )

Paco Bernal

unread,
May 30, 2017, 10:06:25 AM5/30/17
to web2py-users
mmm... maybe you are looking for ASSIGNJS(js_var=python_list) ????

It depends on the way you want to do whatever you wanna do :)

if you get the name of the list is because you return dict(your_list=your_list) and in the view you have {{=your_list}} you need to return ASSIGNJS(my_js_var=your_list}} and in the view you can manage my_js_var with javascript.

But you can use the other case instead of ':eval' that will return the view you have for that controller, inside a div and then represent the information as you want and  with your own js script for that view.

Paco Bernal

unread,
May 30, 2017, 10:12:47 AM5/30/17
to web2py-users
Here you have an example about what I told you without using ':eval'

It's easier to user alert('{{=client_details.name}}'); of course, it's only to show you ASSIGNJS and that you always can do things in many several ways :)




El martes, 30 de mayo de 2017, 14:46:08 (UTC+2), sunda...@gmail.com escribió:
default.py
db.py
client_details.html
index.html

sunda...@gmail.com

unread,
Jun 1, 2017, 2:49:46 AM6/1/17
to web2py-users
Hey paco,

the problem is that i am using script, so the the action in the controller is a callback, it means that it dont have his html page.

Junior Phanter

unread,
Jun 1, 2017, 3:09:15 AM6/1/17
to web...@googlegroups.com
<script>
var list_sunda = "";
ajax("your_echo_url", [], ':eval');
</script>

def echo_url():
    list_sunda=[1,2,3,4,5]
    return "list_sunda=%s;" %list_sunda

2017-06-01 3:49 GMT-03:00 <sunda...@gmail.com>:
Hey paco,

the problem is that i am using script, so the the action in the controller is a callback, it means that it dont have his html page.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paco Bernal

unread,
Jun 1, 2017, 4:41:46 AM6/1/17
to web2py-users
But.... what are you returning in the controller??

sunda...@gmail.com

unread,
Jun 1, 2017, 9:44:06 AM6/1/17
to web2py-users
I totally agree with that but then how do you use that list in  the view as a python variable ???


Le jeudi 1 juin 2017 09:09:15 UTC+2, Junior Phanter a écrit :
<script>
var list_sunda = "";
ajax("your_echo_url", [], ':eval');
</script>

def echo_url():
    list_sunda=[1,2,3,4,5]
    return "list_sunda=%s;" %list_sunda
2017-06-01 3:49 GMT-03:00 <sunda...@gmail.com>:
Hey paco,

the problem is that i am using script, so the the action in the controller is a callback, it means that it dont have his html page.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

Paco Bernal

unread,
Jun 1, 2017, 9:55:55 AM6/1/17
to web2py-users

In server side when rendering the view you use python vars with {{=my_python_var}}
If you are asking how to use a python var in client side with javascript you can use ASSIGNJS(js_var=python_var)

Take a look at the files I uploaded before.

def mycontroller():
    .....
    return ASSIGNJS(js_var=python_list)

if you use ':eval' you will have a new js_var var to use with javascript.
if you use a view to fill a div you can do the same thing, but then you will return dict(whatever=whatever) and in the view you can use {{=ASSIGN(js_var=whatever)}} inside a script tag.
Reply all
Reply to author
Forward
0 new messages