Web2py CRUD.create () field representation in forms
I have this field, for example
Field('yourref', type='string',
label=T('Your reference')),
which displays as INPUT in HTML
<input id='table_yourref' name='yourref' value=''/>
I want to show it like this
<input id='table_yourref' name='yourref' value=''/>
<a onclick='add()'>Add</a>
<a onclick='remove()'>Remove</a>
add()
and remove()
are jQuery functions to add or remove a field
+3
source to share
1 answer
The best way is probably to create a custom widget :
def mywidget(field, value):
return CAT(INPUT(_name=field.name,
_id='%s_%s' % (field._tablename, field.name),
_class=field.type,
_value=value, requires=field.requires),
A('Add', _onclick='add()'),
A('Remove', _onclick='remove()'))
...
Field('yourref', type='string', label=T('Your reference'),
widget=mywidget)
You can also use the server-side DOM to insert links into the form after it's created:
form = crud.create(db.mytable)
form.element('#mytable_myfield__row .w2p_fw').append(A('Add', _onclick='add()'))
form.element('#mytable_myfield__row .w2p_fw').append(A('Remove', _onclick='remove()'))
The advantage of a custom widget is that it will apply to all forms based on db.mytable, whereas the DOM method has to be applied separately to each form.
+2
source to share