2

I'm using django-autocomplete-light and want to add action buttons do update/delete choices. So, I don't understand clearly how I can update/delete value and trigger change specific field.

Is there a clear documentation about that somewhere ?

Akira
  • 160
  • 1
  • 12

2 Answers2

1

My answer is based on the following link: https://github.com/yourlabs/django-autocomplete-light/issues/1092

(change) This work for me:

$.ajax({
    type: 'GET',
    url: `your-url`
}).then(function (response) {
    response.results.forEach((element, _, __) => {
        var option = new Option(element.text, element.id, true, true);
        field.append(option);
    });

    field.trigger('change');
});

My GET RESPONSE is:

response = {
    results: [
        {id: 1, text: "label"}
    ]
}
0

I don't think this solution is complete, but this is what I've been doing for programmatically selecting an option for Select2QuerySetViews:

$('#select2-<field_id>-container').val(<object_key>).html(<object_str>).trigger('change')

So to get this to work you need:

  • field_id: ID of the input element
  • object_key: primary key of the selected model
  • object_str: string representation of the model (whatever is returned by __str__).

I know this doesn't fully answer your question, but I'm hoping there's some value. There wasn't much documentation on how to do this either. Would love if someone could improve on this.