简体   繁体   中英

How to get, change or clear a django-autocomplete-light field value with javascript

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 ?

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"}
    ]
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM