跳转到主要内容

为z3c.form提供的optgroup小部件。

项目描述

介绍

此包实现了一个将选择值分组到optgroups的小部件。

用法

想象你有一个以下架构

class IMySchema(Interface):
    country = schema.Choice(
        required=True,
        title=u"Country",
        vocabulary='countries',
    )

    subdivision = schema.Choice(
        required=True,
        title=u"State",
        vocabulary='subdivisions',
    )

    region = schema.Choice(
        required=True,
        title=u"County",
        vocabulary='regions',
    )

当你创建你的词汇表(例如,使用SimpleVocabulary)时,而不是添加SimpleTerm

...
for country in pycountry.countries:
    terms.append(SimpleTerm(value=country.alpha2, token=country.alpha2,
                            title=country.name))
...

你添加OptgroupTerm

from z3c.formwidget.optgroup.widget import OptgroupTerm

...
country_list = countries(context)
for item in pycountry.subdivisions:
    parent = country_list.getTermByToken(item.country_code).title
    terms.append(OptgroupTerm(value=item.code, token=item.code,
                              title=item.name, optgroup=parent))
...

在您的表单中,只需分配OptgroupFieldWidget

from z3c.formwidget.optgroup.widget import OptgroupFieldWidget

class MyForm(form.Form):
    fields = field.Fields(IMySchema)

    fields['subdivision'].widgetFactory = OptgroupFieldWidget
    fields['region'].widgetFactory = OptgroupFieldWidget

贡献者

托马斯·马斯曼,作者

变更日志

1.2 (2012-05-01)

  • 修复了显示模式模板中错误的HTML标签。[托马斯·马斯曼]

1.1 (2012-04-26)

  • 始终将无值消息作为第一条显示。[托马斯·马斯曼]

1.0.1 (2012-04-14)

  • MANIFEST.in缺少一些条目。[托马斯·马斯曼]

1.0 (2012-04-14)

  • 首次发布。[托马斯·马斯曼]

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

z3c.formwidget.optgroup-1.2.tar.gz (13.2 kB 查看哈希值)

上传时间: 源代码

由以下支持