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)
# 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)
http://www.web2py.com/AlterEgo/default/show/258
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.
when inserted in company works well, but not in person.
Jose
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]
--
Kuba
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)
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
correct?
which browser?
sounds like your browser is not executing the jQuery(...).html(...)
part of the code.
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.
> 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.
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
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
comment everything related to the ajax_create function. Can you insert
records?
> http://www.hiboox.es/go/images/informatica/ajax,91d2bf2f3f78a5841241e...
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
Massimo
--
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.