Ticket #5116: newforms_select_disable.diff
File newforms_select_disable.diff, 5.3 KB (added by , 17 years ago) |
---|
-
tests/regressiontests/forms/widgets.py
392 392 <option value="4">4</option> 393 393 </select> 394 394 395 You can disable some choices by passing a subset of choices: 396 >>> w = Select(choices=get_choices(), disable=[0]) 397 >>> print w.render('num', 2) 398 <select name="num"> 399 <option value="0" disabled="disabled">0</option> 400 <option value="1">1</option> 401 <option value="2" selected="selected">2</option> 402 <option value="3">3</option> 403 <option value="4">4</option> 404 </select> 405 395 406 # NullBooleanSelect Widget #################################################### 396 407 397 408 >>> w = NullBooleanSelect() … … 522 533 <option value="3">3</option> 523 534 </select> 524 535 536 You can disable a subset of choices: 537 >>> w = SelectMultiple(choices=[(1, 1), (2, 2), (3, 3)], disable=[1]) 538 >>> print w.render('nums', None) 539 <select multiple="multiple" name="nums"> 540 <option value="1" disabled="disabled">1</option> 541 <option value="2">2</option> 542 <option value="3">3</option> 543 </select> 544 545 525 546 If 'choices' is passed to both the constructor and render(), then they'll both be in the output: 547 >>> w = SelectMultiple(choices=[(1, 1), (2, 2), (3, 3)]) 526 548 >>> print w.render('nums', [2], choices=[(4, 4), (5, 5)]) 527 549 <select multiple="multiple" name="nums"> 528 550 <option value="1">1</option> -
django/newforms/widgets.py
171 171 return u'<input%s />' % flatatt(final_attrs) 172 172 173 173 class Select(Widget): 174 def __init__(self, attrs=None, choices=() ):174 def __init__(self, attrs=None, choices=(), disable=()): 175 175 super(Select, self).__init__(attrs) 176 176 # choices can be any iterable, but we may need to render this widget 177 177 # multiple times. Thus, collapse it into a list so it can be consumed 178 178 # more than once. 179 179 self.choices = list(choices) 180 180 self.disable = set(disable) 181 181 182 def render(self, name, value, attrs=None, choices=()): 182 183 if value is None: value = '' 183 184 final_attrs = self.build_attrs(attrs, name=name) 184 185 output = [u'<select%s>' % flatatt(final_attrs)] 185 186 str_value = force_unicode(value) # Normalize to string. 186 187 for option_value, option_label in chain(self.choices, choices): 188 disable_html = (option_value in self.disable) and ' disabled="disabled"' or '' 187 189 option_value = force_unicode(option_value) 188 190 selected_html = (option_value == str_value) and u' selected="selected"' or '' 189 output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(force_unicode(option_label)))) 191 output.append(u'<option value="%s"%s%s>%s</option>' % ( 192 escape(option_value), selected_html, disable_html, escape(force_unicode(option_label)))) 190 193 output.append(u'</select>') 191 194 return u'\n'.join(output) 192 195 … … 210 213 return {u'2': True, u'3': False, True: True, False: False}.get(value, None) 211 214 212 215 class SelectMultiple(Widget): 213 def __init__(self, attrs=None, choices=() ):216 def __init__(self, attrs=None, choices=(), disable=()): 214 217 super(SelectMultiple, self).__init__(attrs) 215 218 # choices can be any iterable 216 219 self.choices = choices 217 220 self.disable = set(disable) 221 218 222 def render(self, name, value, attrs=None, choices=()): 219 223 if value is None: value = [] 220 224 final_attrs = self.build_attrs(attrs, name=name) 221 225 output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)] 222 226 str_values = set([force_unicode(v) for v in value]) # Normalize to strings. 223 227 for option_value, option_label in chain(self.choices, choices): 228 disable_html = (option_value in self.disable) and ' disabled="disabled"' or '' 224 229 option_value = force_unicode(option_value) 225 230 selected_html = (option_value in str_values) and ' selected="selected"' or '' 226 output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(force_unicode(option_label)))) 231 output.append(u'<option value="%s"%s%s>%s</option>' % ( 232 escape(option_value), selected_html, disable_html, escape(force_unicode(option_label)))) 227 233 output.append(u'</select>') 228 234 return u'\n'.join(output) 229 235 -
docs/newforms.txt
1660 1660 url = forms.URLField() 1661 1661 comment = CommentInput() 1662 1662 1663 Select Widgets 1664 -------------- 1665 1666 The constructor of ``Select`` and the ``SelectMultiple`` widgets can 1667 be given a subset of choices which should be disabled. 1668 1669 Example:: 1670 1671 >>> w = SelectMultiple(choices=[(1, 1), (2, 2), (3, 3)], disable=[1]) 1672 >>> print w.render('nums', None) 1673 <select multiple="multiple" name="nums"> 1674 <option value="1" disabled="disabled">1</option> 1675 <option value="2">2</option> 1676 <option value="3">3</option> 1677 </select> 1678 1679 1663 1680 Generating forms for models 1664 1681 =========================== 1665 1682