Ticket #6873: SelectMultiple-default-size-02.diff
File SelectMultiple-default-size-02.diff, 7.6 KB (added by , 16 years ago) |
---|
-
django/forms/widgets.py
345 345 if value is None: value = '' 346 346 final_attrs = self.build_attrs(attrs, name=name) 347 347 output = [u'<select%s>' % flatatt(final_attrs)] 348 options = self.render_options(choices, [value])348 options, dont_care = self.render_options(choices, [value]) 349 349 if options: 350 350 output.append(options) 351 351 output.append('</select>') … … 361 361 # Normalize to strings. 362 362 selected_choices = set([force_unicode(v) for v in selected_choices]) 363 363 output = [] 364 output_length = 0 364 365 for option_value, option_label in chain(self.choices, choices): 366 output_length += 1 365 367 if isinstance(option_label, (list, tuple)): 366 368 output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value))) 367 369 for option in option_label: 370 output_length += 1 368 371 output.append(render_option(*option)) 369 372 output.append(u'</optgroup>') 370 373 else: 371 374 output.append(render_option(option_value, option_label)) 372 return u'\n'.join(output) 375 return u'\n'.join(output), output_length 373 376 374 377 class NullBooleanSelect(Select): 375 378 """ … … 398 401 class SelectMultiple(Select): 399 402 def render(self, name, value, attrs=None, choices=()): 400 403 if value is None: value = [] 404 options, options_length = self.render_options(choices, value) 401 405 final_attrs = self.build_attrs(attrs, name=name) 406 # default size attribute for cross-browser consistent rendering 407 if 'size' not in final_attrs: 408 # emulate the behaviour of firefox: adjust the size to the number of choices, with an upper boundary 409 final_attrs['size'] = min(10, max(1, options_length)) 402 410 output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)] 403 options = self.render_options(choices, value)404 411 if options: 405 412 output.append(options) 406 413 output.append('</select>') -
tests/modeltests/model_forms/models.py
342 342 <option value="2">Pending</option> 343 343 <option value="3">Live</option> 344 344 </select></td></tr> 345 <tr><th>Categories:</th><td><select multiple="multiple" name="categories" >345 <tr><th>Categories:</th><td><select multiple="multiple" name="categories" size="3"> 346 346 <option value="1">Entertainment</option> 347 347 <option value="2">It's a test</option> 348 348 <option value="3">Third test</option> … … 398 398 <option value="2">Pending</option> 399 399 <option value="3">Live</option> 400 400 </select></li> 401 <li>Categories: <select multiple="multiple" name="categories" >401 <li>Categories: <select multiple="multiple" name="categories" size="3"> 402 402 <option value="1">Entertainment</option> 403 403 <option value="2">It's a test</option> 404 404 <option value="3">Third test</option> … … 459 459 <option value="2">Pending</option> 460 460 <option value="3">Live</option> 461 461 </select></li> 462 <li>Categories: <select multiple="multiple" name="categories" >462 <li>Categories: <select multiple="multiple" name="categories" size="3"> 463 463 <option value="1" selected="selected">Entertainment</option> 464 464 <option value="2">It's a test</option> 465 465 <option value="3">Third test</option> … … 575 575 <option value="2">Pending</option> 576 576 <option value="3">Live</option> 577 577 </select></li> 578 <li>Categories: <select multiple="multiple" name="categories" >578 <li>Categories: <select multiple="multiple" name="categories" size="3"> 579 579 <option value="1">Entertainment</option> 580 580 <option value="2">It's a test</option> 581 581 <option value="3">Third</option> … … 601 601 <option value="2">Pending</option> 602 602 <option value="3">Live</option> 603 603 </select></li> 604 <li>Categories: <select multiple="multiple" name="categories" >604 <li>Categories: <select multiple="multiple" name="categories" size="4"> 605 605 <option value="1">Entertainment</option> 606 606 <option value="2">It's a test</option> 607 607 <option value="3">Third</option> -
tests/regressiontests/admin_widgets/models.py
41 41 42 42 >>> w = FilteredSelectMultiple('test', False) 43 43 >>> print conditional_escape(w.render('test', 'test')) 44 <select multiple="multiple" name="test" >44 <select multiple="multiple" name="test" size="1"> 45 45 </select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script> 46 46 <BLANKLINE> 47 47 -
tests/regressiontests/forms/extra.py
259 259 >>> w = ComplexMultiWidget() 260 260 >>> print w.render('name', 'some text,JP,2007-04-25 06:24:00') 261 261 <input type="text" name="name_0" value="some text" /> 262 <select multiple="multiple" name="name_1" >262 <select multiple="multiple" name="name_1" size="4"> 263 263 <option value="J" selected="selected">John</option> 264 264 <option value="P" selected="selected">Paul</option> 265 265 <option value="G">George</option> … … 300 300 >>> f = ComplexFieldForm() 301 301 >>> print f 302 302 <tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" id="id_field1_0" /> 303 <select multiple="multiple" name="field1_1" id="id_field1_1">303 <select multiple="multiple" size="4" name="field1_1" id="id_field1_1"> 304 304 <option value="J">John</option> 305 305 <option value="P">Paul</option> 306 306 <option value="G">George</option> … … 311 311 >>> f = ComplexFieldForm({'field1_0':'some text','field1_1':['J','P'], 'field1_2_0':'2007-04-25', 'field1_2_1':'06:24:00'}) 312 312 >>> print f 313 313 <tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" value="some text" id="id_field1_0" /> 314 <select multiple="multiple" name="field1_1" id="id_field1_1">314 <select multiple="multiple" size="4" name="field1_1" id="id_field1_1"> 315 315 <option value="J" selected="selected">John</option> 316 316 <option value="P" selected="selected">Paul</option> 317 317 <option value="G">George</option> -
tests/regressiontests/forms/forms.py
476 476 ... composers = MultipleChoiceField() 477 477 >>> f = SongForm(auto_id=False) 478 478 >>> print f['composers'] 479 <select multiple="multiple" name="composers" >479 <select multiple="multiple" name="composers" size="1"> 480 480 </select> 481 481 >>> class SongForm(Form): 482 482 ... name = CharField() 483 483 ... composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')]) 484 484 >>> f = SongForm(auto_id=False) 485 485 >>> print f['composers'] 486 <select multiple="multiple" name="composers" >486 <select multiple="multiple" name="composers" size="2"> 487 487 <option value="J">John Lennon</option> 488 488 <option value="P">Paul McCartney</option> 489 489 </select> … … 491 491 >>> print f['name'] 492 492 <input type="text" name="name" value="Yesterday" /> 493 493 >>> print f['composers'] 494 <select multiple="multiple" name="composers" >494 <select multiple="multiple" name="composers" size="2"> 495 495 <option value="J">John Lennon</option> 496 496 <option value="P" selected="selected">Paul McCartney</option> 497 497 </select>