Project

General

Profile

0001-forms-add-option-to-use-select2-autocomplete-on-list.patch

Frédéric Péters, 19 Mar 2019 12:04 PM

Download (3.49 KB)

View differences:

Subject: [PATCH] forms: add option to use select2/autocomplete on list items
 (#19271)

 tests/test_fields.py                                  | 11 +++++++++++
 wcs/fields.py                                         |  3 +++
 wcs/qommon/form.py                                    |  6 ++++++
 wcs/qommon/templates/qommon/forms/widgets/select.html |  3 +++
 4 files changed, 23 insertions(+)
tests/test_fields.py
270 270
        assert str(form.render()).count('<option value="">Bla bla bla</option>') == 1 # ---
271 271
        assert str(form.render()).count('<option') == 1
272 272

  
273
def test_item_render_as_autocomplete():
274
    field = fields.ItemField(
275
            id='1',
276
            label='Foobar',
277
            items=['aa', 'ab', 'ac'],
278
            display_mode='autocomplete')
279
    form = Form(use_tokens=False)
280
    field.add_to_form(form)
281
    assert str(form.render()).count('<option') == 3
282
    assert 'select2' in str(form.render())
283

  
273 284
def test_item_render_as_radio():
274 285
    items_kwargs = []
275 286
    items_kwargs.append({'items': ['aa', 'ab', 'ac']})
wcs/fields.py
1250 1250
            if len(kwargs['options']) > 3 or length_first_items > 40:
1251 1251
                # TODO: absence/presence of delimitor should be an option
1252 1252
                kwargs['delim'] = htmltext('<br />')
1253
        elif self.display_mode == 'autocomplete':
1254
            kwargs['select2'] = True
1253 1255

  
1254 1256
    def get_display_value(self, value):
1255 1257
        real_value = value
......
1341 1343
                value=self.in_filters, advanced=True)
1342 1344
        options = [('list', _('List')),
1343 1345
                   ('radio', _('Radio buttons')),
1346
                   ('autocomplete', _('Autocomplete')),
1344 1347
                  ]
1345 1348
        form.add(RadiobuttonsWidget, 'display_mode',
1346 1349
                title=_('Display Mode'),
wcs/qommon/form.py
1523 1523

  
1524 1524
    def __init__(self, name, value=None, **kwargs):
1525 1525
        self.options_with_attributes = kwargs.pop('options_with_attributes', None)
1526
        self.select2 = kwargs.pop('select2', None)
1526 1527
        super(SingleSelectHintWidget, self).__init__(name, value=value, **kwargs)
1527 1528

  
1529
    def add_media(self):
1530
        if self.select2:
1531
            get_response().add_javascript(['jquery.js', '../../i18n.js', 'qommon.forms.js', 'select2.js'])
1532
            get_response().add_css_include('../js/select2/select2.css')
1533

  
1528 1534
    def separate_hint(self):
1529 1535
        return (self.hint and len(self.hint) > 80)
1530 1536

  
wcs/qommon/templates/qommon/forms/widgets/select.html
13 13
    {% endif %}
14 14
  {% endfor %}
15 15
</select>
16
{% if widget.select2 %}
17
<script>$('#form_{{widget.name}}').select2();</script>
18
{% endif %}
16 19
{% endblock %}
17
-