我无法自动完成工作,我相信这是因为解析。
JavaScript代码:
$('#id_emp_name').autocomplete({
source: '/mycompany/employees.json',
minLength: 1,
dataType: 'json',
delay: 0,
parse: function(data) {
var parsed = [];
for(var i = 0; i < data.fields.length; i++) {
parsed[parsed.length] =
{
data: data.fields[i],
value: data.fields[i].eng_name,
result: data.fields[i].eng_name
};
}
return parsed;
},
formatItem: function(item) {
var name = '';
name = '(' + item.eng_name + ', ' + item.rank + ')';
return name;
}
});
示例JSON输出:url.com/mycompany/employees.json?term=e
:
[
{
"pk": 1,
"model": "system.employees",
"fields": {
"salary": "1234",
"rank": "manager",
"entity_status": "n1",
"chi_name": "n/a",
"eng_name": "Eli",
"department": "marketing",
"employment_end_date": null,
"employment_date": "2011-09-20"
}
}
]
查看:
def employeeJSON(request):
if request.method == 'GET' and request.GET['term']:
q_term = request.GET['term']
emp_list = Employees.objects.filter(eng_name__icontains=q_term)
json_serializer = serializers.get_serializer('json')()
json_data = json_serializer.serialize(emp_list, ensure_ascii=False)
return HttpResponse(
json_data, mimetype='application/json; charset=utf-8'
)
我正在使用jQuery UI自动填充功能。当我在文本框中输入时,我没有得到任何值,自动完成弹出窗口部分弹出没有值。
但是,每当我输入时,我都会调用GET请求:
[23/Sep/2011 18:59:51] "GET /mycompany/employees.json?term=e HTTP/1.1" 200 241
[23/Sep/2011 18:59:53] "GET /mycompany/employees.json?term=el HTTP/1.1" 200 241