Ruby Rails-jQuery自动完成-为列表项组合多个值 [英] Ruby rails - jquery auto complete - combine multiple values for a list item
问题描述
如何解析Rails实例变量的值以创建jQuery的自动完成列表? 因此,我的"/user/search"返回一个实例变量,每个项目具有多个值.我想将它们结合起来并作为列表项显示.
How do I parse values of an rails instance variable to create an auto complete list of jQuery? So my '/user/search' return an instance variable with multiple values per item. I wanna combine them and present as a list item.
def search
if (params[:term] =~ /[a-zA-Z]/)
@result = User.FindLdap("sAMAccountName", params[:term])
else
@result = User.FindLdap("idnumber", params[:term])
end
respond_to do |format|
format.html { render :partial => 'text_for_autocomplete'}
format.js
end
end
我正在使用jQuery的示例代码,但是它可以按原样显示每个项目列表.在我的情况下,我检索了多个ldap属性,并且希望显示每个列表项的那些属性值.
因此,每个列表项都应像用逗号分隔的""#{sAMAccountName}", "#{idnumber}""
一样,而不只是在文本字段中键入的idnumber的值.
I am using jQuery's example code, but it has a way to display each item list as it is. In my case I retrieve multiple ldap attributes and I want to show those attributes values for each list item.
So each list item shall be like ""#{sAMAccountName}", "#{idnumber}""
seperated by a comma, instead of just value of idnumber that was typed in the text field.
$(function() {
$("#term").autocomplete({
source: function (request, response) {
$.post("/users/search", request, response);
},
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
} #No idea how it can be done here.
});
});
我在列表中得到了它,部分渲染的html-
I get this in the list, html of the partial render -
推荐答案
首先,您需要查看 JBuilder . JBuilder是用于json输出的强大,定义明确且可靠的解决方案.
First of all you need to look at JBuilder. JBuilder is strong, well-defined and reliable solution for json-output.
这篇关于Ruby Rails-jQuery自动完成-为列表项组合多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!