web2py 中用于 SQLFORM 小部件输入的自定义 CSS 类

发布于 2024-12-23 11:37:03 字数 948 浏览 1 评论 0原文

给定控制器中的 SQLFORM:

form = SQLFORM.factory(db.source_server, db.target_server)

具有以下表定义:

db.define_table('target_server',
                Field('target_url', 'string'),
                Field('target_user', 'string'),
                Field('target_password', 'password'))
db.define_table('target_server',
                Field('target_url', 'string'),
                Field('target_user', 'string'),
                Field('target_password', 'password'))

如何为每个表单小部件 元素应用自定义 CSS 类?

  {{=form.custom.begin}}
  <fieldset>
    <legend>Source Server</legend>
    <div class="clearfix">
      <label for="source_url">URL:</label>
      <div class="input">

        <input class="bla bla" name="field_name">

      </div>
    </div>
  ...
  {{=form.custom.end}}

Given this SQLFORM in the controller:

form = SQLFORM.factory(db.source_server, db.target_server)

with the following table definition:

db.define_table('target_server',
                Field('target_url', 'string'),
                Field('target_user', 'string'),
                Field('target_password', 'password'))
db.define_table('target_server',
                Field('target_url', 'string'),
                Field('target_user', 'string'),
                Field('target_password', 'password'))

how can I apply custom CSS classes for each form widget <input> element?

  {{=form.custom.begin}}
  <fieldset>
    <legend>Source Server</legend>
    <div class="clearfix">
      <label for="source_url">URL:</label>
      <div class="input">

        <input class="bla bla" name="field_name">

      </div>
    </div>
  ...
  {{=form.custom.end}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2024-12-30 11:37:03

请注意,所有小部件都已经有一个以小部件类型命名的类(例如“字符串”、“整数”、“日期”等)以及“tablename_fieldname”形式的 id,因此您可以在 CSS 中使用这些内容,无需添加自定义类。请参阅此处了解更多详细信息。

如果您确实需要自定义类,请注意 web2py FORM 和 SQLFORM 对象及其子组件是 HTML 帮助器对象并且可以像任何 HTML 帮助器一样进行操作。特别是,助手对于其组件而言充当列表,对于其属性而言充当字典(输入类是 INPUT 对象的属性)。因此,您可以在创建表单的控制器或视图中执行类似的操作:

form.custom.widget.field_name['_class'] = 'bla bla'

如果您需要更改特定类型的所有小部件的类,您也可以通过 .elements() 搜索功能

for input in form.elements('input', _class='string'):
    input['_class'] = 'my-string'

另一个选项是明确 为表定义中的字段定义一个小部件,可以使用自定义小部件或简单地传递 _class 属性到内置小部件之一:

Field('target_user', 'string', widget = lambda field, value:
    SQLFORM.widgets.string.widget(field, value, _class='my-string'))

此方法将影响包含此字段的所有表单(除非您在创建表单之前明确删除或替换该小部件)。

Note, all widgets already have a class named for the type of widget (e.g., "string", "integer", "date", etc.) as well as an id of the form "tablename_fieldname", so you might be able to make use of those in your CSS without needing to add custom classes. See here for more details.

If you do need to customize the classes, note that web2py FORM and SQLFORM objects and their sub-components are HTML helper objects and can be manipulated like any HTML helpers. In particular, helpers act as lists with respect to their components and dictionaries with respect to their attributes (an input class is an attribute of the INPUT object). So, you can do something like this, either in the controller that creates the form or in the view:

form.custom.widget.field_name['_class'] = 'bla bla'

If you need to change the class of all widgets of a particular type, you can also do that via the .elements() search functionality:

for input in form.elements('input', _class='string'):
    input['_class'] = 'my-string'

An additional option is to explicitly define a widget for the Field in the table definition, either using a custom widget or simply passing a _class attribute to one of the built-in widgets:

Field('target_user', 'string', widget = lambda field, value:
    SQLFORM.widgets.string.widget(field, value, _class='my-string'))

This method will affect all forms that include this field (unless you explictly remove or replace the widget before creating the form).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文