How to change '_class' of Web2py autocompl

2019-04-17 08:35发布

问题:

I loop through a form object to change all the classes:

form = crud.create(db.messages, next = URL('index'))
parts = ['title', 'body', 'subject'] # corresponding fields
classes = 'form-control col-md-12' # my classes
for p in parts:
    form.custom.widget[p]['_class'] = '%s %s' % (classes, form.custom.widget[p]['_type'])

This is working - but: subject is an autocomplete widget:

db.messages.subject.widget = SQLFORM.widgets.autocomplete(...)

and here _class is not changed (or altered afterwards again?)

How can this be fixed? Thanks!

回答1:

The autocomplete widget is a TAG object that contains two components, the first of which is the INPUT element. So, do something like:

if p == 'subject':
    form.custom.widget[p][0].add_class(classes)

Note, you can use the add_class method to add classes to an element that has an existing class.

Also, instead of manually changing all the classes, you might see if setting crud.settings.formstyle to "bootstrap3_stacked" or "bootstrap3_inline" works for you. If not, you can also write a custom formstyle function to generate the exact form layout you want.



标签: web2py