Elteroooo
Elteroooo

Reputation: 3081

web2py CRUD.create() field representation in forms

i have this field for example

Field('yourref', type='string',
          label=T('Your reference')),

which is shown as an INPUT in the 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

Upvotes: 3

Views: 1061

Answers (1)

Anthony
Anthony

Reputation: 25536

The best method 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 could also use the server-side DOM to insert the links into the form after it is 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 the custom widget is that it will be applied to all forms created based on db.mytable, whereas the DOM method must be applied separately to each form.

Upvotes: 2

Related Questions