1 # -*- coding: utf-8 -*-
2 from django.forms import widgets
3 from django.utils.safestring import mark_safe
4 from django.utils.datastructures import MultiValueDict
5 from django.newforms.util import flatatt
7 TPL_OPTION = """<option value="%(value)s" %(selected)s>%(desc)s</option>"""
10 <select class="dropdown_select" %(attrs)s>
17 $('span#%(id)s>select.dropdown_select').change(function(){
18 var pattern = 'span#%(id)s>select.dropdown_select';
19 var last_item = $(pattern+':last');
21 if (last_item.val()) {
22 last_item.clone(true).appendTo($('span#%(id)s'));
23 $('span#%(id)s').append(' ');
28 for (var i=$(pattern).length-1; i>=0; i--) {
29 if (values.indexOf($($(pattern).get(i)).val()) >= 0) {
30 $($(pattern).get(i)).remove();
32 values.push($($(pattern).get(i)).val());
40 <span class="dropdown_multiple" id="%(id)s">
46 class DropDownMultiple(widgets.Widget):
49 def __init__(self, attrs=None, choices=()):
50 self.choices = choices
52 super(DropDownMultiple, self).__init__(attrs)
54 def render(self, name, value, attrs=None, choices=()):
55 if value is None: value = []
56 final_attrs = self.build_attrs(attrs, name=name)
59 id = final_attrs['id']
63 choices = [('','---')] + list(self.choices)
68 opts = "\n".join([TPL_OPTION %{'value': k, 'desc': v, 'selected': val == k and 'selected="selected"' or ''} for k, v in choices])
70 items.append(TPL_SELECT %{'attrs': flatatt(final_attrs), 'opts': opts})
73 opts = "\n".join([TPL_OPTION %{'value': k, 'desc': v, 'selected': ''} for k, v in choices])
74 items.append(TPL_SELECT %{'attrs': flatatt(final_attrs), 'opts': opts})
76 script = TPL_SCRIPT %{'id': id}
77 output = TPL_FULL %{'id': id, 'values': '\n'.join(items), 'script': script}
79 return mark_safe(output)
81 def value_from_datadict(self, data, files, name):
82 if isinstance(data, MultiValueDict):
83 return [i for i in data.getlist(name) if i]
85 return data.get(name, None)