Django Autocomplete Light - Check all options on click

146 views Asked by At

I have a django autocomplete light, with more than 200 options. Therefore, i want to include a button "select all".

I almost managed to do so, with the following code.

$('#facility-types').val(['2', '5', '6', '10', '11', '12', '13', '14']);
$('#facility-types').trigger('change'); // Notify any JS components that the value changed

The problem is, that with django autocomplete light, the option-tags for the select fields are only then created, when the user has manually selected an option. Furthermore, the option-tag has a data-select-2-id that appears me to be kind of random.

<option value="12" data-select2-id="127">xxxx</option>

Any ideas, how to programatically select all options of a django-autocomplete-light select field?

1

There are 1 answers

0
Elsa On

I have found a great solution here: https://stackoverflow.com/a/65521385/15857447

Basically you should create the option using as id the instance pk and as name the instance __str__.

There is no problem if creating this option is redundant (it already exists in the dropdown).