Ticket #5116: newforms_widgets_selectm_disable.diff

File newforms_widgets_selectm_disable.diff, 1.6 KB (added by Thomas Güttler <hv@…>, 8 years ago)
  • newforms/widgets.py

     
    200201        return {u'2': True, u'3': False, True: True, False: False}.get(value, None)
    201202
    202203class SelectMultiple(Widget):
    203     def __init__(self, attrs=None, choices=()):
     204    def __init__(self, attrs=None, choices=(), disabled=None):
    204205        # choices can be any iterable
    205206        self.attrs = attrs or {}
    206207        self.choices = choices
    207 
     208        self.disabled = disabled or set()
     209       
    208210    def render(self, name, value, attrs=None, choices=()):
    209211        if value is None: value = []
    210212        final_attrs = self.build_attrs(attrs, name=name)
    211213        output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)]
    212214        str_values = set([force_unicode(v) for v in value]) # Normalize to strings.
    213215        for option_value, option_label in chain(self.choices, choices):
     216            disabled_html = (option_value in self.disabled) and ' disabled="disabled"' or ''
    214217            option_value = force_unicode(option_value)
    215218            selected_html = (option_value in str_values) and ' selected="selected"' or ''
    216             output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(force_unicode(option_label))))
     219            output.append(u'<option value="%s"%s%s>%s</option>' % (
     220                escape(option_value), selected_html, disabled_html, escape(force_unicode(option_label))))
    217221        output.append(u'</select>')
    218222        return u'\n'.join(output)
    219223
Back to Top