为zope.formlib提供分组下拉小部件

horae.groupselect的Python项目详细描述


简介

horae.groupselectzope.formlib使用 html提供的默认optgroup元素。

用法

要使用分组下拉小部件,请使用前面使用的Choice字段 必须用提供的GroupedChoice字段替换架构的 在horae.groupedselect.field中。一个GroupedChoice字段接受 与stockChoice字段的参数完全相同,但是 组提供程序的实例(实现 horae.groupedselect.interfaces.IGroups)必须在 参数groups。上述组提供程序转换 将字段的词汇表放入一个组列表中。

想象一下选择一个国家的字段,您希望按 地区:

from zope import interface, implements
from zope.schema import vocabulary

from horae.groupedselect import field, interfaces

class Country(object):
    def __init__(self, region, country):
        self.region = region
        self.country = country

class CountryGroups(object):
    implements(interfaces.IGroups)

    def groups(self, vocabulary):
        """
        Converts the given vocabulary into a list of groups:

            return (('Group 1', (term1, term2, term3)),
                    ('Group 2', (term6, term5, term6)),)
        """
        groups = {}
        for term in vocabulary:
            if not term.value.region in groups:
                groups[term.value.region] = ()
            groups[term.value.region] = groups[term.value.region] + \
                (term,)
        return groups.items()

class ICountrySelection(interface.Interface):

    country = field.GroupedChoice(
        title = u'Country',
        vocabulary = vocabulary.SimpleVocabulary((
            # ...
            vocabulary.SimpleTerm(
                Country(u'Europe', u'Switzerland'),
                'ch',
                u'Switzerland'
            ),
            vocabulary.SimpleTerm(
                Country(u'Europe', u'Germany'),
                'de',
                u'Germany'
            ),
            vocabulary.SimpleTerm(
                Country(u'Europe', u'Austria'),
                'at',
                u'Austria'
            ),
            vocabulary.SimpleTerm(
                Country(u'Europe', u'France'),
                'fr',
                u'France'
            ),
            # ...
            vocabulary.SimpleTerm(
                Country(u'North America', u'USA'),
                'us',
                u'USA'
            ),
            vocabulary.SimpleTerm(
                Country(u'Europe', u'Canada'),
                'ca',
                u'Canada'
            ),
            # ...
        )),
        groups = CountryGroups()
    )

更改日志

1.0a1(2012-01-16)

  • 初始版本

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何使用字符串将字符串居中。总体安排   java获取错误:可能存在从double到int的有损转换。。。。但我没有用替身?   java简单嵌套for循环示例   java有命令在我的Android上运行JavaScript项目吗?   java如何向图像中添加文本并让用户在安卓中定位文本?   java无法在listview中显示项目   java Spring接口转换器   java我可以在Dart中使用GWT库吗?   java使用选择器和SelectionKeys委托给线程池   安全Java从SSL HTTP url下载zip(带密钥库/信任库)   java Android查看旋转动画并设置旋转未按预期工作   java在使用参数时无法检索文件   java多文本选择句柄Android Studio