如何更改 Web2py 自动完成小部件的“_class" [英] How to change '_class' of Web2py autocomplete widget
问题描述
我遍历表单对象以更改所有类:
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'])
这是有效的 - 但是:subject
是一个自动完成小部件:
This is working - but: subject
is an autocomplete widget:
db.messages.subject.widget = SQLFORM.widgets.autocomplete(...)
这里 _class
没有改变(或之后又改变了?)
and here _class
is not changed (or altered afterwards again?)
如何解决这个问题?谢谢!
How can this be fixed? Thanks!
推荐答案
自动完成小部件是一个 TAG
对象,它包含两个组件,第一个是 INPUT
元素.因此,请执行以下操作:
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)
注意,您可以使用 add_class
方法向具有现有类的元素添加类.
Note, you can use the add_class
method to add classes to an element that has an existing class.
此外,您可以查看将 crud.settings.formstyle
设置为bootstrap3_stacked"或bootstrap3_inline"是否适合您,而不是手动更改所有类.如果没有,您也可以编写自定义的 formstyle 函数来生成您想要的确切表单布局.
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 自动完成小部件的“_class"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!