Ticket #6873: SelectMultiple-default-size-00.diff

File SelectMultiple-default-size-00.diff, 4.3 KB (added by , 17 years ago)

First shot at adding a default size to SelectMultiple, includes test changes

  • django/newforms/widgets.py

     
    375375    def render(self, name, value, attrs=None, choices=()):
    376376        if value is None: value = []
    377377        final_attrs = self.build_attrs(attrs, name=name)
    378         output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)]
     378        output = []
     379        choice_count = 0
    379380        str_values = set([force_unicode(v) for v in value]) # Normalize to strings.
    380381        for option_value, option_label in chain(self.choices, choices):
     382            choice_count += 1
    381383            option_value = force_unicode(option_value)
    382384            selected_html = (option_value in str_values) and ' selected="selected"' or ''
    383385            output.append(u'<option value="%s"%s>%s</option>' % (
    384386                    escape(option_value), selected_html,
    385387                    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, 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
    386394        output.append(u'</select>')
    387395        return mark_safe(u'\n'.join(output))
    388396
  • tests/regressiontests/forms/extra.py

     
    259259>>> w = ComplexMultiWidget()
    260260>>> print w.render('name', 'some text,JP,2007-04-25 06:24:00')
    261261<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">
    263263<option value="J" selected="selected">John</option>
    264264<option value="P" selected="selected">Paul</option>
    265265<option value="G">George</option>
     
    300300>>> f = ComplexFieldForm()
    301301>>> print f
    302302<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">
    304304<option value="J">John</option>
    305305<option value="P">Paul</option>
    306306<option value="G">George</option>
     
    311311>>> f = ComplexFieldForm({'field1_0':'some text','field1_1':['J','P'], 'field1_2_0':'2007-04-25', 'field1_2_1':'06:24:00'})
    312312>>> print f
    313313<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">
    315315<option value="J" selected="selected">John</option>
    316316<option value="P" selected="selected">Paul</option>
    317317<option value="G">George</option>
  • tests/regressiontests/forms/forms.py

     
    475475...     composers = MultipleChoiceField()
    476476>>> f = SongForm(auto_id=False)
    477477>>> print f['composers']
    478 <select multiple="multiple" name="composers">
     478<select multiple="multiple" name="composers" size="0">
    479479</select>
    480480>>> class SongForm(Form):
    481481...     name = CharField()
    482482...     composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')])
    483483>>> f = SongForm(auto_id=False)
    484484>>> print f['composers']
    485 <select multiple="multiple" name="composers">
     485<select multiple="multiple" name="composers" size="2">
    486486<option value="J">John Lennon</option>
    487487<option value="P">Paul McCartney</option>
    488488</select>
     
    490490>>> print f['name']
    491491<input type="text" name="name" value="Yesterday" />
    492492>>> print f['composers']
    493 <select multiple="multiple" name="composers">
     493<select multiple="multiple" name="composers" size="2">
    494494<option value="J">John Lennon</option>
    495495<option value="P" selected="selected">Paul McCartney</option>
    496496</select>
Back to Top