ajax

36 views
Skip to first unread message

Jose

unread,
Feb 19, 2010, 9:54:16 PM2/19/10
to web2py-users
how to this [1] with webpy?

[1] http://www.vimeo.com/9526668

mdipierro

unread,
Feb 20, 2010, 12:52:31 AM2/20/10
to web2py-users
On Feb 19, 8:54 pm, Jose <jjac...@gmail.com> wrote:
> how to this [1] with webpy?
>
> [1]http://www.vimeo.com/9526668

Here is complete code:

db.define_table('organization',Field('name',notnull=True,unique=True),format='%
(name)s')
db.define_table('person',Field('name'),Field('organization',db.organization))

db.person.organization.comment = \
SPAN(A('add',_class='thickbox',
_href="#TB_inline?
height=200&width=600&inlineId=modal_content"),
DIV(LOAD('default','add_organization',ajax=True),
_id='modal_content',_class='hidden'))

response.files.append('http://jquery.com/demo/thickbox/thickbox-code/
thickbox.js')
response.files.append('http://jquery.com/demo/thickbox/thickbox-code/
thickbox.css')

def index():
return dict(form=crud.create(db.person,request.args(0)))

def add_organization():
def update_select(form):
organizations =
db(db.organization.id>0).select(orderby=db.organization.name)
options = TAG['']
(*[OPTION(o.name,_value=o.id,_selected=form.vars.id==o.id) for o in
organizations])
response.headers['web2py-component-command'] =
"jQuery('#person_organization').html('%s');jQuery('#TB_closeWindowButton').click()"
% options
return crud.create(db.organization,onaccept=update_select)

mdipierro

unread,
Feb 20, 2010, 1:01:12 AM2/20/10
to web2py-users
Here is a little better with a title in the modal and some comments of
explanation

# create a model


db.define_table('organization',Field('name',notnull=True,unique=True),format='%
(name)s')
db.define_table('person',Field('name'),Field('organization',db.organization))

# create link that open a modal window and calls
# add_organization via ajax using web2py ajax trapping
# forms will be processed and stay in the modal window if errors


db.person.organization.comment = \
SPAN(A('add',_class='thickbox',

_title='Add a new organizaiton',
_href='#TB_inline?
height=100&width=600&inlineId=modal_content'),


DIV(LOAD('default','add_organization',ajax=True),
_id='modal_content',_class='hidden'))

# load required ajax libraries


response.files.append('http://jquery.com/demo/thickbox/thickbox-code/
thickbox.js')
response.files.append('http://jquery.com/demo/thickbox/thickbox-code/
thickbox.css')

# this is your main action (nothing special here)
def index():
return dict(form=crud.create(db.person,request.args(0)))

# this is the ajax callback
def add_organization():
def update_select(form):
# this function updates the content of the select dropdown
(web2py trick)
# and closes the modal


organizations =
db(db.organization.id>0).select(orderby=db.organization.name)
options = TAG['']
(*[OPTION(o.name,_value=o.id,_selected=form.vars.id==o.id) for o in
organizations])
response.headers['web2py-component-command'] =
"jQuery('#person_organization').html('%s');jQuery('#TB_closeWindowButton').click()"
% options
return crud.create(db.organization,onaccept=update_select)

mdipierro

unread,
Feb 20, 2010, 1:10:17 AM2/20/10
to web2py-users
Reposted here since there are some indentation issues with google:

http://www.web2py.com/AlterEgo/default/show/258

mdipierro

unread,
Feb 20, 2010, 2:39:29 AM2/20/10
to web2py-users
En even better solution.

Drop this code in your model somewhere:

def ajax_create(field,
value='create',


title='Add a new organizaiton',

height=100, width=600):
if not field.type.startswith('reference'):
raise SyntaxError, "can only be used with a reference field"
if not hasattr(field.requires,'options'):
raise SyntaxError, "cannot determine options from field
validator"
key = str(field).replace('.','_')
if request.get_vars._ajax_add==str(field):
def update_select(form):
options = TAG['']
(*[OPTION(v,_value=k,_selected=str(form.vars.id)==str(k)) \
for (k,v) in
field.requires.options()])
command = "jQuery('#
%s').html('%s');jQuery('#TB_closeWindowButton').click()" \
% (key,options.xml().replace("'","\'"))
response.headers['web2py-component-command'] = command
table = field._db[field.type[10:]]
raise
HTTP(200,crud.create(table,onaccept=update_select).xml(),**response.headers)


response.files.append('http://jquery.com/demo/thickbox/thickbox-
code/thickbox.js')
response.files.append('http://jquery.com/demo/thickbox/thickbox-
code/thickbox.css')

return TAG[''](
A(value,_class='thickbox',_title=title,
_href='#TB_inline?height=%s&width=%s&inlineId=TB_%s' %
(height,width,key)),
DIV(LOAD(request.controller,request.action,args=request.args,
vars=dict(_ajax_add=field),ajax=True),_id='TB_%s' %
key,_class='hidden'))

Then you just do:

db.define_table('organization',Field('name',notnull=True,unique=True),format='%
(name)s')
db.define_table('person',Field('name'),Field('organization',db.organization))

db.person.organization.comment = ajax_create(db.person.organization,
title='Add an Org.')

def index():
return dict(form=crud.create(db.person,request.args(0)))

Will work with any table/field that you already have.

Jose

unread,
Feb 20, 2010, 9:32:38 AM2/20/10
to web2py-users
Thank Massimo,

when inserted in company works well, but not in person.

Jose

mdipierro

unread,
Feb 20, 2010, 9:54:55 AM2/20/10
to web2py-users
What behavior do you see? Can I see the model?

Jose

unread,
Feb 20, 2010, 10:23:42 AM2/20/10
to web2py-users

On 20 feb, 14:54, mdipierro <mdipie...@cs.depaul.edu> wrote:
> What behavior do you see? Can I see the model?
>

The behavior after the submit is normal, but when I go to see the
table person are not records.
Tested on FreeBSD and Windows

The code corresponds to the last posted by you [1]


[1] http://www.pastebin.com/m3573996a

DenesL

unread,
Feb 20, 2010, 10:37:48 AM2/20/10
to web2py-users

Aw crap! and here is little me thinking I know how to use web2py.
This one requires some painfully detailed walk-through.
Am I the only one?.

Kuba Kucharski

unread,
Feb 20, 2010, 10:54:22 AM2/20/10
to web...@googlegroups.com
+1

--
Kuba

mdipierro

unread,
Feb 20, 2010, 11:25:11 AM2/20/10
to web2py-users
which version of web2py?

Jose

unread,
Feb 20, 2010, 11:37:38 AM2/20/10
to web2py-users

On 20 feb, 16:25, mdipierro <mdipie...@cs.depaul.edu> wrote:
> which version of web2py?
>

In windows the latest binary. In FreeBSD, the trunc version (1 hour
ago)

mdipierro

unread,
Feb 20, 2010, 11:38:27 AM2/20/10
to web2py-users
Yes there are some trick in here. Everything happens in one line

db.person.organization.comment = ajax_create(db.person.organization)

the function ajax_create serves a double purpose:
1) it returns a helper instance that when you click on it it open a
modal window that contains a LOAD component. LOAD components in web2py
are loaded via ajax and all form submissions are trapped so they also
submitted via ajax too and the response stays into the modal window.
But where is the ajax callback function?
2) it acts as a callback function! How can it be? well, in web2py you
can raise HTTP. This means any piece of code, not just action can
return HTTP responses. If the ajax_create function detects a request
vars called _ajax_add set to a a value equal to the name of the field
passed as argument, it understand the client is doing the ajax
callback and it raise HTTP thus producing a response without
continuing execution of models and controllers. In this case it acts
as an action (even if defined in a model).

if request.get_vars._ajax_add==str(field):
... # this is acts as an action
raise
HTTP(200,crud.create(table,onaccept=update_select).xml(),...)

When the form is accepted it also sets a header variable called
'web2py-component-command'. This is executed by the ajax form trap
client-side. The command tells the client to replace the content of
the SELECT with the new one and close the modal.

I agree it is not easy but you do not really need to understand it to
use it.

Massimo

mdipierro

unread,
Feb 20, 2010, 12:01:25 PM2/20/10
to web2py-users
If I understand what you tell me.
You click on create and get the modal
add a new organization
submit it (does the modal close?)
go back to person form
there is nothing in the dropdown.

correct?
which browser?
sounds like your browser is not executing the jQuery(...).html(...)
part of the code.

Jose

unread,
Feb 20, 2010, 12:32:59 PM2/20/10
to web2py-users

On 20 feb, 17:01, mdipierro <mdipie...@cs.depaul.edu> wrote:
> If I understand what you tell me.
> You click on create and get the modal
> add a new organization
> submit it (does the modal close?)
> go back to person form
> there is nothing in the dropdown.

No. I added the company, after you submit, it closes the modal window
and refresh the dropdown with the company added. So far everything is
correct. Then when I add a person, complete the fields, I submit
(produces no error), but the person has not still registered, the
register does not appear in the table.

DenesL

unread,
Feb 20, 2010, 12:47:55 PM2/20/10
to web2py-users
On 20 feb, 11:38, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Yes there are some trick in here. Everything happens in one line
>
Thanks. Tricky, yes! very!. It is hard to follow at first but I am
getting it now.

> db.person.organization.comment = ajax_create(db.person.organization)
>
> the function ajax_create serves a double purpose:
> 1) it returns a helper instance that when you click on it it open a
> modal window that contains a LOAD component. LOAD components in web2py
> are loaded via ajax and all form submissions are trapped so they also
> submitted via ajax too and the response stays into the modal window.
> But where is the ajax callback function?
> 2) it acts as a callback function! How can it be? well, in web2py you
> can raise HTTP. This means any piece of code, not just action can
> return HTTP responses. If the ajax_create function detects a request
> vars called _ajax_add set to a a value equal to the name of the field
> passed as argument, it understand the client is doing the ajax
> callback and it raise HTTP thus producing a response without
> continuing execution of models and controllers. In this case it acts
> as an action (even if defined in a model).
>
>     if request.get_vars._ajax_add==str(field):
>         ... # this is acts as an action
>         raise
> HTTP(200,crud.create(table,onaccept=update_select).xml(),...)
>

Searching the group I found only two posts using it and obviously I
did not understand the capabilities of it before.

> When the form is accepted it also sets a header variable called
> 'web2py-component-command'. This is executed by the ajax form trap
> client-side. The command tells the client to replace the content of
> the SELECT with the new one and close the modal.
>

This must be part of "not documented here" in AlterEgo 252.
Is it documented now?

> I agree it is not easy but you do not really need to understand it to
> use it.
>

I prefer to understand.

mdipierro

unread,
Feb 20, 2010, 12:49:04 PM2/20/10
to web2py-users
I am not sure what you refer to is a bug. I think that is the
workflow. Every time you submit the form you get a new empty form.

Make sure the table definitions are models not controller and use
appadmin to see if the records are in the table. They should be there.

Massimo

Jose

unread,
Feb 20, 2010, 1:21:03 PM2/20/10
to web2py-users

On 20 feb, 17:49, mdipierro <mdipie...@cs.depaul.edu> wrote:
> I am not sure what you refer to is a bug. I think that is the
> workflow. Every time you submit the form you get a new empty form.
>
> Make sure the table definitions are models not controller and use
> appadmin to see if the records are in the table. They should be there.
>
> Massimo

You can see in the picture (4) there are no records. Also I checked
with a manager sqlite

http://www.hiboox.es/go/images/informatica/ajax,91d2bf2f3f78a5841241e17b2d385801.png.html

mdipierro

unread,
Feb 20, 2010, 1:45:55 PM2/20/10
to web2py-users
I see. I do not think this has anything to do with the ajax_create
function.
we need to isolate the problem.

comment everything related to the ajax_create function. Can you insert
records?

> http://www.hiboox.es/go/images/informatica/ajax,91d2bf2f3f78a5841241e...

Jose

unread,
Feb 20, 2010, 1:52:30 PM2/20/10
to web2py-users

On 20 feb, 18:45, mdipierro <mdipie...@cs.depaul.edu> wrote:
> I see. I do not think this has anything to do with the ajax_create
> function.
> we need to isolate the problem.
>
> comment everything related to the ajax_create function. Can you insert
> records?
>

I commented:

#def ajax_create

and

#db.person.organization.comment = ajax_create(db.person.organization,


title='Add an Org.')

and now I could add the record.

Jose

mdipierro

unread,
Feb 20, 2010, 7:16:33 PM2/20/10
to web2py-users
Now I am very puzzled. Can you email me the entire app?

Massimo

DenesL

unread,
Feb 21, 2010, 9:29:41 AM2/21/10
to web2py-users

Adding person with ajax_create active works for me on 1.75.1

Tito Garrido

unread,
Mar 20, 2010, 2:37:22 PM3/20/10
to web...@googlegroups.com
I could add the form on the modal window but the date widget and time widget is appearing out of the modal window... it's under the window. Is there a way to put it to appear on the modal window?

Regards,

Tito

--
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To post to this group, send email to web...@googlegroups.com.
To unsubscribe from this group, send email to web2py+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/web2py?hl=en.




--

Linux User #387870
.........____
.... _/_õ|__|
..º[ .-.___.-._| . . . .
.__( o)__( o).:_______

Tito Garrido

unread,
Mar 21, 2010, 10:00:41 AM3/21/10
to web...@googlegroups.com
Not sure if it's a thickbox problem but the widgets (date and time) works using iframe... but the behavior of the "onaccept" function is different and I can't update the page after the record is inserted... so I have to update the page manually to see the new record created by the modal window
Reply all
Reply to author
Forward
0 new messages