How can I select the first given option? I'm feeding the autocomplete widget with the data from reverse geocoding results (city) basing on user's location. I have a database with cities and I need to select the first suggested option.
autocomplete_light_registry.py
autocomplete_light.register(
City,
search_fields=('^name',),
autocomplete_js_attributes={'placeholder': _('Start typing...')}
)
forms.py
class CustomerForm(forms.ModelForm):
city = forms.ModelChoiceField(City.objects.all(), label=_('City'), widget=autocomplete_light.ChoiceWidget('CityAutocomplete'))
locations.js
$('#id_city_text').val(ymaps.geolocation.city);
var autocomplete = $('#id_city_text').yourlabsAutocomplete();
autocomplete.refresh();
Thanks for your help.
Screenshots:
References:
Here's how to auto select the first choice if there is only one:
$(document).ready(function() {
var autocomplete = $('#id_city_text').yourlabsAutocomplete();
autocomplete.show = function(html) {
yourlabs.Autocomplete.prototype.show.call(this, html)
var choices = this.box.find(this.choiceSelector);
if (choices.length == 1) {
this.input.trigger('selectChoice', [choices, this]);
}
}
});