Ticket #5116: newforms_widgets_selectm_disable.diff
File newforms_widgets_selectm_disable.diff, 1.6 KB (added by , 17 years ago) |
---|
-
newforms/widgets.py
200 201 return {u'2': True, u'3': False, True: True, False: False}.get(value, None) 201 202 202 203 class SelectMultiple(Widget): 203 def __init__(self, attrs=None, choices=() ):204 def __init__(self, attrs=None, choices=(), disabled=None): 204 205 # choices can be any iterable 205 206 self.attrs = attrs or {} 206 207 self.choices = choices 207 208 self.disabled = disabled or set() 209 208 210 def render(self, name, value, attrs=None, choices=()): 209 211 if value is None: value = [] 210 212 final_attrs = self.build_attrs(attrs, name=name) 211 213 output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)] 212 214 str_values = set([force_unicode(v) for v in value]) # Normalize to strings. 213 215 for option_value, option_label in chain(self.choices, choices): 216 disabled_html = (option_value in self.disabled) and ' disabled="disabled"' or '' 214 217 option_value = force_unicode(option_value) 215 218 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)))) 217 221 output.append(u'</select>') 218 222 return u'\n'.join(output) 219 223