DisableableSelectWidget

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from django import forms
from django.utils.encoding import force_unicode
from django.utils.html import conditional_escape, escape

class DisableableSelectWidget(forms.Select):
    def __init__(self, attrs=None, disabled_choices=(), choices=()):
        super(DisableableSelectWidget, self).__init__(attrs, choices)
        self.disabled_choices = list(disabled_choices)

    def render_option(self, selected_choices, option_value, option_label):
        option_value = force_unicode(option_value)
        if option_value in selected_choices:
            selected_html = u' selected="selected"'
            if not self.allow_multiple_selected:
                # Only allow for a single selection.
                selected_choices.remove(option_value)
        else:
            selected_html = ''
        if option_value in self.disabled_choices:
            disabled_html = u' disabled="disabled"'
        else:
            disabled_html = ''
        return u'<option value="%s"%s%s>%s</option>' % (
            escape(option_value), selected_html, disabled_html,
            conditional_escape(force_unicode(option_label)))

More like this

  1. Allow disabling options in a select widget by scjody 2 years, 10 months ago
  2. DropDownMultiple widget by marinho 5 years, 11 months ago
  3. TableSelectMultiple Widget by insin 6 years, 4 months ago
  4. labeled hidden input with choices by eliasfernandez 1 year, 5 months ago
  5. CheckboxMultiSelect with interable checkboxes by pyramids16 1 year, 5 months ago

Comments

(Forgotten your password?)