为zope.formlib提供分组下拉小部件
horae.groupselect的Python项目详细描述
简介
horae.groupselect为 zope.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)
- 初始版本