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.
On Feb 20, 12:10 am, mdipierro <[email protected]> wrote:
> Reposted here since there are some indentation issues with google:
>
> http://www.web2py.com/AlterEgo/default/show/258
>
> On Feb 20, 12:01 am, mdipierro <[email protected]> wrote:
>
> > 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)
>
> > On Feb 19, 11:52 pm, mdipierro <[email protected]> wrote:
>
> > > On Feb 19, 8:54 pm, Jose <[email protected]> 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)
--
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en.