Skip to main content

Provides a grouped Dropdown widget for zope.formlib

Project description

Introduction

horae.groupselect provides a grouped Dropdown widget for zope.formlib using the default optgroup elements provided by HTML.

Usage

To use a grouped dropdown widget the previously used Choice field of the schema has to be replaced by a GroupedChoice field provided in horae.groupedselect.field. A GroupedChoice field takes the exactly same parameters as the stock Choice field but additionally an instance of a groups provider (implementing horae.groupedselect.interfaces.IGroups) has to be provided over the parameter groups. The mentioned groups provider converts the vocabulary of the field into a list of groups.

Imagine field to select a country and you would like to group them by region:

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()
    )

Dependencies

Changelog

1.0a1 (2012-01-16)

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

horae.groupselect-1.0a1.tar.gz (4.0 kB view details)

Uploaded Source

File details

Details for the file horae.groupselect-1.0a1.tar.gz.

File metadata

File hashes

Hashes for horae.groupselect-1.0a1.tar.gz
Algorithm Hash digest
SHA256 6f7285b54b9b9c5fbf8b17e1d1c804eda67d29cb3fd8b654b4dfed0dd004cf4d
MD5 ae73bd524abb15ba9cc97ae932dcea1e
BLAKE2b-256 ab6877a81f53f8db60a187a7dac1682e82b47abe485a2255a942330030a5b334

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page