Fork me on GitHub

select2

Select2 widget in single selection mode.

vocab = sorted((u'Weißburgunder', u'Welschriesling',
                u'Sauvingnon Blanc', u'Sämling', u'Scheurebe',
                u'Traminer', u'Morrilon', u'Muskateller'))

select2 = factory('#field:select2', value='Traminer', props={
    'label': 'Select some items',
    'placeholder': 'Select some items',
    'vocabulary': vocab,
})

Select2 widget in multi selection mode.

value = [u'Sauvingnon Blanc', u'Sämling']
select2 = factory('#field:select2', value=value, props={
    'label': 'Select some items',
    'placeholder': 'Select some items',
    'vocabulary': vocab,
    'multiple': True,
})

Select2 widget in tag mode.

value = [u'Sämling', u'Scheurebe']
select2 = factory('#field:select2', value=value, props={
    'label': 'Select or add some items',
    'inputtag': True,
    'placeholder': 'Select or add some items',
    'minimumInputLength': 1,
    'multiple': True,
    'tags': vocab,
})

Select2 single selection widget fetching data from URL.

select2 = factory('#field:select2', value='Welschriesling', props={
    'label': 'Select ajax item',
    'inputtag': True,
    'placeholder': 'Select some item',
    'ajaxurl': 'yafowil.widget.select2.json',
})

The server answers with a JSON response, here the example does it using WSGI and webob way. This code needs modification depending on the framework used

def json_response(environ, start_response):
    data = lipsum
    if environ['QUERY_STRING'].startswith('q='):
        term = environ['QUERY_STRING'][3:]
        data = list()
        if term:
            for val in vocab:
                if val.lower().startswith(term.lower()):
                    data.append({
                        'id': val,
                        'text': val,
                    })
    response = Response(content_type='application/json',
                        body=json.dumps(data))
    return response(environ, start_response)

Select2 multi selection widget fetching data from URL.

value = [u'Sämling', u'Traminer']
select2 = factory('#field:select2', value=value, props={
    'label': 'Select multiple ajax items',
    'inputtag': True,
    'minimumInputLength': 1,
    'multiple': True,
    'placeholder': 'Select some items',
    'ajaxurl': 'yafowil.widget.select2.json',
})

Select2 tagging selection widget fetching data from URL. Also provide a vocabulary defining terms for values.

value = ['a', 'b', 'c']
vocabulary = [('a', u'Sämling'),
              ('b', u'Traminer'),
              ('c', u'Welschriesling')]
select2 = factory('#field:select2', value=value, props={
    'label': 'Select tags',
    'vocabulary': vocabulary,
    'inputtag': True,
    'minimumInputLength': 1,
    'multiple': True,
    'placeholder': 'Select tags',
    'ajaxurl': 'yafowil.widget.select2.json',
    'tags': True,
})