Ticket #5116: newforms_select_disable-2.diff
File newforms_select_disable-2.diff, 5.3 KB (added by , 17 years ago) |
---|
-
tests/regressiontests/forms/widgets.py
419 419 <option value="4">4</option> 420 420 </select> 421 421 422 You can disable some choices by passing a subset of choices: 423 >>> w = Select(choices=get_choices(), disable=[0]) 424 >>> print w.render('num', 2) 425 <select name="num"> 426 <option value="0" disabled="disabled">0</option> 427 <option value="1">1</option> 428 <option value="2" selected="selected">2</option> 429 <option value="3">3</option> 430 <option value="4">4</option> 431 </select> 432 422 433 # NullBooleanSelect Widget #################################################### 423 434 424 435 >>> w = NullBooleanSelect() … … 549 560 <option value="3">3</option> 550 561 </select> 551 562 563 You can disable a subset of choices: 564 >>> w = SelectMultiple(choices=[(1, 1), (2, 2), (3, 3)], disable=[1]) 565 >>> print w.render('nums', None) 566 <select multiple="multiple" name="nums"> 567 <option value="1" disabled="disabled">1</option> 568 <option value="2">2</option> 569 <option value="3">3</option> 570 </select> 571 572 552 573 If 'choices' is passed to both the constructor and render(), then they'll both be in the output: 574 >>> w = SelectMultiple(choices=[(1, 1), (2, 2), (3, 3)]) 553 575 >>> print w.render('nums', [2], choices=[(4, 4), (5, 5)]) 554 576 <select multiple="multiple" name="nums"> 555 577 <option value="1">1</option> -
django/newforms/widgets.py
201 201 return super(CheckboxInput, self).value_from_datadict(data, files, name) 202 202 203 203 class Select(Widget): 204 def __init__(self, attrs=None, choices=() ):204 def __init__(self, attrs=None, choices=(), disable=()): 205 205 super(Select, self).__init__(attrs) 206 206 # choices can be any iterable, but we may need to render this widget 207 207 # multiple times. Thus, collapse it into a list so it can be consumed 208 208 # more than once. 209 209 self.choices = list(choices) 210 210 self.disable = set(disable) 211 211 212 def render(self, name, value, attrs=None, choices=()): 212 213 if value is None: value = '' 213 214 final_attrs = self.build_attrs(attrs, name=name) … … 215 216 # Normalize to string. 216 217 str_value = force_unicode(value) 217 218 for option_value, option_label in chain(self.choices, choices): 219 disable_html = (option_value in self.disable) and ' disabled="disabled"' or '' 218 220 option_value = force_unicode(option_value) 219 221 selected_html = (option_value == str_value) and u' selected="selected"' or '' 220 output.append(u'<option value="%s"%s >%s</option>' % (221 escape(option_value), selected_html, 222 output.append(u'<option value="%s"%s%s>%s</option>' % ( 223 escape(option_value), selected_html, disable_html, 222 224 conditional_escape(force_unicode(option_label)))) 223 225 output.append(u'</select>') 224 226 return mark_safe(u'\n'.join(output)) … … 243 245 return {u'2': True, u'3': False, True: True, False: False}.get(value, None) 244 246 245 247 class SelectMultiple(Widget): 246 def __init__(self, attrs=None, choices=() ):248 def __init__(self, attrs=None, choices=(), disable=()): 247 249 super(SelectMultiple, self).__init__(attrs) 248 250 # choices can be any iterable 249 251 self.choices = choices 250 252 self.disable = set(disable) 253 251 254 def render(self, name, value, attrs=None, choices=()): 252 255 if value is None: value = [] 253 256 final_attrs = self.build_attrs(attrs, name=name) 254 257 output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)] 255 258 str_values = set([force_unicode(v) for v in value]) # Normalize to strings. 256 259 for option_value, option_label in chain(self.choices, choices): 260 disable_html = (option_value in self.disable) and ' disabled="disabled"' or '' 257 261 option_value = force_unicode(option_value) 258 262 selected_html = (option_value in str_values) and ' selected="selected"' or '' 259 output.append(u'<option value="%s"%s >%s</option>' % (260 escape(option_value), selected_html, 263 output.append(u'<option value="%s"%s%s>%s</option>' % ( 264 escape(option_value), selected_html, disable_html, 261 265 conditional_escape(force_unicode(option_label)))) 262 266 output.append(u'</select>') 263 267 return mark_safe(u'\n'.join(output)) -
docs/newforms.txt
1767 1767 url = forms.URLField() 1768 1768 comment = CommentInput() 1769 1769 1770 Select Widgets 1771 -------------- 1772 1773 The constructor of ``Select`` and the ``SelectMultiple`` widgets can 1774 be given a subset of choices which should be disabled. 1775 1776 Example:: 1777 1778 >>> w = SelectMultiple(choices=[(1, 1), (2, 2), (3, 3)], disable=[1]) 1779 >>> print w.render('nums', None) 1780 <select multiple="multiple" name="nums"> 1781 <option value="1" disabled="disabled">1</option> 1782 <option value="2">2</option> 1783 <option value="3">3</option> 1784 </select> 1785 1786 1770 1787 Generating forms for models 1771 1788 =========================== 1772 1789