How to change '_class' of Web2py autocomplete widget

会有一股神秘感。 提交于 2020-01-01 19:56:14

问题


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.



来源:https://stackoverflow.com/questions/27570755/how-to-change-class-of-web2py-autocomplete-widget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!