如何将正确的 JSON 数据从 Rails 控制器返回到 jQuery 自动完成选择框?
我正在努力实现 jQuery UI 的“组合框”示例。我已经设置了一个远程数据源,它返回我输入的值。但是,只有当我返回一个字符串数组时它才有效,如下所示:
render :text => Product.find_by_sql("select id, part_number from products where part_number like '#{params[:term]}%'").collect{|p| p.part_number}.to_json
当然,我真正想做的是让它 还返回AR对象的id。不幸的是,当我尝试将它们返回到子数组中时,我的组合框中出现了一堆“未定义”值。
这是在我的组合框中触发的“select”事件:
select: function( event, ui ) {
ui.item.option.selected = true;
self._trigger( "selected", event, {
item: ui.item.option
});
},
我已经阅读了 jQuery UI 文档,但我无法弄清楚这段代码实际上是做什么的。另外,我无法弄清楚我需要返回的 JSON 的结构应该是什么。
I'm trying really hard to implement jQuery UI's "combo box" example. I have set up a remote data source, and it returns the values I type in. However, it only works if I return an array of strings, like so:
render :text => Product.find_by_sql("select id, part_number from products where part_number like '#{params[:term]}%'").collect{|p| p.part_number}.to_json
What I really want to do, of course, is to have it also return the id of the AR object. Unfortunately, when I try to return them both in a subarray, I get a bunch of "undefined" values in my combo box.
Here is the "select" event which fires on my combo box:
select: function( event, ui ) {
ui.item.option.selected = true;
self._trigger( "selected", event, {
item: ui.item.option
});
},
I have been through the jQuery UI docs, and I can't figure out what this code actually does. Also, I can't figure out what the structure of the JSON I need to return should be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要返回一个哈希数组,每个哈希数组都有两个属性 - id 和 label。
我已经成功地使用了这个:(
我认为你想在对象上调用part_number而不是name,但这就是格式)
另外,我认为你的代码对sql注入开放,你应该使用“like?”句法。
还有这个 gem,我没有使用过,但应该可以工作:
https://github.com/ crowint/rails3-jquery-autocomplete
You need to return an array of hashes, each of which has two attributes - id and label.
I've used this with success:
(I think you'd want to call part_number instead of name on your object, but that's the format)
Separately, I think your code is open to sql injection, you should use the "like ?" syntax.
There's also this gem, which I haven't used but should work:
https://github.com/crowdint/rails3-jquery-autocomplete
我不能代表您正在处理的 Rails 部分,但我确实知道自动完成小部件期望的格式如下:
上面的意思是,如果您不返回一个简单的字符串数组,则必须有一个对象数组,其中至少包含一个标签或值属性(数组中的对象可以包含其他东西,小部件不关心)。例如,以下内容就可以正常工作(并且您可以访问事件处理程序中的 id 属性(例如 ui.item.id )
关于第二个问题:
有问题的代码片段应该修改组合框的底层
select
元素,并将适当的option
设置为选定的。如果您使用的是远程数据源,您可能可以完全不需要它。I can't speak for the rails portion of what you're working on, but I do know that the format the autocomplete widget expects is as follows:
What the above means is that if you don't return a simple
Array
of strings, you must have an array of objects that includes at least a label or value property (the objects in the array can contain other things, the widget does not care). For example, the following will work just fine (and you can access theid
property in event handlers (e.g.ui.item.id
)Concerning your second question:
The piece of code in question is supposed to modify the underlying
select
element of the combobox and set the appropriateoption
as selected. If you're using a remote data source, you probably can do without this entirely.