如何将正确的 JSON 数据从 Rails 控制器返回到 jQuery 自动完成选择框?

发布于 2024-12-09 00:46:40 字数 705 浏览 0 评论 0原文

我正在努力实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情换悲伤 2024-12-16 00:46:40

您需要返回一个哈希数组,每个哈希数组都有两个属性 - id 和 label。

我已经成功地使用了这个:(

collect{|p| {:label=>p.name, :id=>p.id}}.to_json

我认为你想在对象上调用part_number而不是name,但这就是格式)

另外,我认为你的代码对sql注入开放,你应该使用“like?”句法。

Product.where( "part_number like ?", "#{params[:term]}%" )

还有这个 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:

collect{|p| {:label=>p.name, :id=>p.id}}.to_json

(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.

Product.where( "part_number like ?", "#{params[:term]}%" )

There's also this gem, which I haven't used but should work:

https://github.com/crowdint/rails3-jquery-autocomplete

薄暮涼年 2024-12-16 00:46:40

我不能代表您正在处理的 Rails 部分,但我确实知道自动完成小部件期望的格式如下:

本地数据可以是一个简单的字符串数组,或者它包含
数组中每个项目的对象,带有标签或值
财产或两者兼而有之。

上面的意思是,如果您不返回一个简单的字符串数组,则必须有一个对象数组,其中至少包含一个标签或值属性(数组中的对象可以包含其他东西,小部件不关心)。例如,以下内容就可以正常工作(并且您可以访问事件处理程序中的 id 属性(例如 ui.item.id )

[{ label: 'Google', id: '1234' }, { label: 'Yahoo', id: '2345' }, ... ];

关于第二个问题:

我已经浏览了 jQuery UI 文档,但我无法弄清楚这段代码实际上是做什么的。

有问题的代码片段应该修改组合框的底层 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:

The local data can be a simple Array of Strings, or it contains
Objects for each item in the array, with either a label or value
property or both
.

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 the id property in event handlers (e.g. ui.item.id)

[{ label: 'Google', id: '1234' }, { label: 'Yahoo', id: '2345' }, ... ];

Concerning your second question:

I have been through the jQuery UI docs, and I can't figure out what this code actually does.

The piece of code in question is supposed to modify the underlying select element of the combobox and set the appropriate option as selected. If you're using a remote data source, you probably can do without this entirely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文