JQuery-Ui 自动完成不显示结果
我正在尝试显示经理列表的自动完成结果。我有以下代码:
Application.js
function log(message) {
$( "<div/>" ).text( message ).prependTo("#log");
}
$("#managers").autocomplete({
source : function(request, response) {
$.ajax({
url : "/managerlist",
dataType : "json",
data : {
style : "full",
maxRows : 12,
term : request.term
},
success : function(data) {
var results = [];
$.each(data, function(i, item) {
var itemToAdd = {
value : item,
label : item
};
results.push(itemToAdd);
});
return response(results);
}
});
}
});
项目控制器
def manager_list
list=Project.all.map{|i|i.manager_user_id}
arr= [].concat(list.sort{|a,b| a[0]<=>b[0]}).to_json
render :json =>arr
end
Routes.rb
match '/managerlist' => 'projects#manager_user_id'
_form.html.erb
<p>
<%= f.label :manager_user_id %><br />
<input id="managers" />
</p>
以下代码很好,我不知道在 firebug 中没有收到任何错误。但是,当我尝试输入经理姓名(例如詹姆斯·约翰逊)时,该姓名不会出现。我还尝试将整个函数放入 _form.html.erb 并将其包装在标签中,但这不起作用。是否知道为什么会发生这种情况
所以我设法修复了我的错误,这是由于 Jquery core 和 Jquery ui 的顺序造成的。
我已经列出了经理的名单。在下图中。
从图像中可以看出,当我输入“Arm”时,它会显示整个经理列表,当它应该显示“Deshawn Armstrong”。 有关
我知道这很可能与我下面的项目控制器项目控制器
def manager_list
list=Project.all.map{|i|i.manager_user_id}
arr= [].concat(list.sort{|a,b| a[0]<=>b[0]}).to_json
render :json =>arr
end
I am trying to display autocomplete results for a list of managers. I have the following code:
Application.js
function log(message) {
$( "<div/>" ).text( message ).prependTo("#log");
}
$("#managers").autocomplete({
source : function(request, response) {
$.ajax({
url : "/managerlist",
dataType : "json",
data : {
style : "full",
maxRows : 12,
term : request.term
},
success : function(data) {
var results = [];
$.each(data, function(i, item) {
var itemToAdd = {
value : item,
label : item
};
results.push(itemToAdd);
});
return response(results);
}
});
}
});
Project controller
def manager_list
list=Project.all.map{|i|i.manager_user_id}
arr= [].concat(list.sort{|a,b| a[0]<=>b[0]}).to_json
render :json =>arr
end
Routes.rb
match '/managerlist' => 'projects#manager_user_id'
_form.html.erb
<p>
<%= f.label :manager_user_id %><br />
<input id="managers" />
</p>
The following code is fine, I don't recieve no errors in firebug. However when I try to enter a managers name for example James Johnson the name won't appear. I have also tried putting the whole function in the _form.html.erb and wrapped it in tags this didn't work. Is there any idea why this is happening
So I've managed to fix my error, which was because of the ordering of Jquery core and Jquery ui.
I've got the managers to be listed. In the image below.
From the image it can be seen that when I type 'Arm' it brings the entire list of managers, when it should display 'Deshawn Armstrong'. I understand that this is most probably to do with my project controller below
Project controller
def manager_list
list=Project.all.map{|i|i.manager_user_id}
arr= [].concat(list.sort{|a,b| a[0]<=>b[0]}).to_json
render :json =>arr
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查 Firebug 控制台中的响应,并确保字符串采用正确的 json 格式以进行自动完成。
看来您只是返回一个数组。
.ajax
中的dataType
设置不会转换为json
;它只是期待该格式回来。如果您没有从url : "/managerlist"
发回 json,这可能就是问题所在。实际上,只要您的ajax功能正常工作,您就可以:
JSON.stringify
的jsfiddle示例:http://jsfiddle.net/xKaqL/根据您的新信息,您需要在您的自动完成。
Check the response in the Firebug console and make sure the string is in proper json format for the autocomplete.
It appears that you are just returning an array. The
dataType
setting in.ajax
doesn't convert tojson
; it's just expecting that format back. If you are not sending json back from yoururl : "/managerlist"
, this may be the problem.Actually, provided your
ajax
function is working correctly, you could just:jsfiddle example of
JSON.stringify
: http://jsfiddle.net/xKaqL/Based on your new information, you need to add a
minLength
option to your autocomplete.