Ticket #6873: SelectMultiple-default-size-01.diff
File SelectMultiple-default-size-01.diff, 7.0 KB (added by , 17 years ago) |
---|
-
django/newforms/widgets.py
375 375 def render(self, name, value, attrs=None, choices=()): 376 376 if value is None: value = [] 377 377 final_attrs = self.build_attrs(attrs, name=name) 378 output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)] 378 output = [] 379 choice_count = 0 379 380 str_values = set([force_unicode(v) for v in value]) # Normalize to strings. 380 381 for option_value, option_label in chain(self.choices, choices): 382 choice_count += 1 381 383 option_value = force_unicode(option_value) 382 384 selected_html = (option_value in str_values) and ' selected="selected"' or '' 383 385 output.append(u'<option value="%s"%s>%s</option>' % ( 384 386 escape(option_value), selected_html, 385 387 conditional_escape(force_unicode(option_label)))) 388 # default size attribute for cross-browser consistent rendering 389 if 'size' not in final_attrs: 390 # emulate the behaviour of firefox: adjust the size to the number of choices, with an upper boundary 391 final_attrs['size'] = min(10, max(1, choice_count)) 392 # we have to prepend because choices can be any iterable and we cannot call len() so we have to count them first 393 output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)] + output 386 394 output.append(u'</select>') 387 395 return mark_safe(u'\n'.join(output)) 388 396 -
tests/modeltests/model_forms/models.py
330 330 <option value="2">Pending</option> 331 331 <option value="3">Live</option> 332 332 </select></td></tr> 333 <tr><th>Categories:</th><td><select multiple="multiple" name="categories" >333 <tr><th>Categories:</th><td><select multiple="multiple" name="categories" size="3"> 334 334 <option value="1">Entertainment</option> 335 335 <option value="2">It's a test</option> 336 336 <option value="3">Third test</option> … … 386 386 <option value="2">Pending</option> 387 387 <option value="3">Live</option> 388 388 </select></li> 389 <li>Categories: <select multiple="multiple" name="categories" >389 <li>Categories: <select multiple="multiple" name="categories" size="3"> 390 390 <option value="1">Entertainment</option> 391 391 <option value="2">It's a test</option> 392 392 <option value="3">Third test</option> … … 447 447 <option value="2">Pending</option> 448 448 <option value="3">Live</option> 449 449 </select></li> 450 <li>Categories: <select multiple="multiple" name="categories" >450 <li>Categories: <select multiple="multiple" name="categories" size="3"> 451 451 <option value="1" selected="selected">Entertainment</option> 452 452 <option value="2">It's a test</option> 453 453 <option value="3">Third test</option> … … 563 563 <option value="2">Pending</option> 564 564 <option value="3">Live</option> 565 565 </select></li> 566 <li>Categories: <select multiple="multiple" name="categories" >566 <li>Categories: <select multiple="multiple" name="categories" size="3"> 567 567 <option value="1">Entertainment</option> 568 568 <option value="2">It's a test</option> 569 569 <option value="3">Third</option> … … 589 589 <option value="2">Pending</option> 590 590 <option value="3">Live</option> 591 591 </select></li> 592 <li>Categories: <select multiple="multiple" name="categories" >592 <li>Categories: <select multiple="multiple" name="categories" size="4"> 593 593 <option value="1">Entertainment</option> 594 594 <option value="2">It's a test</option> 595 595 <option value="3">Third</option> -
tests/regressiontests/admin_widgets/models.py
38 38 39 39 >>> w = FilteredSelectMultiple('test', False) 40 40 >>> print conditional_escape(w.render('test', 'test')) 41 <select multiple="multiple" name="test" >41 <select multiple="multiple" name="test" size="1"> 42 42 </select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script> 43 43 <BLANKLINE> 44 44 -
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
475 475 ... composers = MultipleChoiceField() 476 476 >>> f = SongForm(auto_id=False) 477 477 >>> print f['composers'] 478 <select multiple="multiple" name="composers" >478 <select multiple="multiple" name="composers" size="1"> 479 479 </select> 480 480 >>> class SongForm(Form): 481 481 ... name = CharField() 482 482 ... composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')]) 483 483 >>> f = SongForm(auto_id=False) 484 484 >>> print f['composers'] 485 <select multiple="multiple" name="composers" >485 <select multiple="multiple" name="composers" size="2"> 486 486 <option value="J">John Lennon</option> 487 487 <option value="P">Paul McCartney</option> 488 488 </select> … … 490 490 >>> print f['name'] 491 491 <input type="text" name="name" value="Yesterday" /> 492 492 >>> print f['composers'] 493 <select multiple="multiple" name="composers" >493 <select multiple="multiple" name="composers" size="2"> 494 494 <option value="J">John Lennon</option> 495 495 <option value="P" selected="selected">Paul McCartney</option> 496 496 </select>