javascript - Programmatically select the first suggestion in django-autocomplete-light -
how can select first given option? i'm feeding autocomplete widget data reverse geocoding results (city) basing on user's location. have database cities , need select 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 help.
screenshots:
- http://cl.ly/image/2v0i373e2c0m (achieved)
- http://cl.ly/image/3s2p382o2n12 (goal)
references:
here's how auto select first choice if there 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]); } } });
Comments
Post a Comment