I am trying to search for some elements using jquery ui autocomple in a django application. I looked through this question and I am following the exact same way as the autocomplete document. I'm not interested in plugins or anything else. I got it.
In views.py:
def search_view(request):
q = request.GET['term']
ret = []
listado = Model.objects.filter(descripcion__istartswith=q).order_by("descripcion")
for l in listado:
ret.append({'label':l.descripcion, 'value':l.id})
return HttpResponse(simplejson.dumps(ret), mimetype='application/json')
In my template, I have something like this
js:
$("#auto_material").autocomplete({
source:'{% url search_view %}',
minLength: 2,
select: function( event, ui ) {
$("#auto_material").val(ui.item.label);
$("#id_material").val(ui.item.value);
}
});
html:
<input type="text" id="auto_material" name="material" class="campo" style="width:99%;"/>
<input type="hidden" id="id_material" />
Everything in finding elements works fine, but when I try to set the text input value using ui.item.label, it keeps putting ui.item.value on the text input, not the label.
Any idea? Is the "select" event on an autocomplete object redefined? Any idea?
Thanks in advance.
source
share