Complete code:
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.function,args=request.args,
vars=dict(_ajax_add=field),ajax=True),_id='TB_%s' % key,_class='hidden'))
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)))
...