to have autocompletion on an input,I do this:
in views.py:
def getpositions(request):
if request.is_ajax():
query = request.GET.get("term", "")
positions=Position.objects.filter(name__icontains=query)
results = []
for position in positions:
position_json={}
position_json['name']=position.name
results.append(position_json)
data=simplejson.dumps(results)
else:
data = 'error'
return HttpResponse(data, mimetype='application/json')
in template:
$(document).ready(function(){
$("#positions").autocomplete({
source: "{% url CompanyHub.views.getPositions%}",
success: function( data ) {
response( $.map( data, function( item ) {
return {
label: item.name,
value: item.name
}
}));
},
minLength: 2,
});
});
and #positions is : <input type="text" id="positions" />
every thing is ok,but it just show Undefined instead of showing list of results,I tryed many things but no way!!
jQuery UI autocomplete doesnt have a
successoption you are using to format your data. Check the option list here http://jqueryui.com/demos/autocomplete/ . Instead You can use a callback function forsourceoption and handle your own ajax call yourself and format data like you want.