JQuery UI 自动完成插件中的分组结果?
我正在尝试创建一些跨多种类型数据的搜索功能,并具有自动完成功能。我希望为每个自动完成建议以及根据类型分组的建议提供自定义视图。各组也应该分开。
如果我的解释很糟糕,您可以查看hotels.com 上的搜索功能作为示例:建议根据城市、地标、机场等进行分组。
我一直在查看 JQuery UI 自动完成插件,它似乎是能够做我需要的大部分事情,但我还没有看到任何分组的例子。
由于我的 javascript/JQuery 技能有点缺乏,我希望这里有人能告诉我是否可以实现我想要的自动完成插件,或者是否有其他插件可以实现这一点?如何完成它的示例/概述也将不胜感激。
I'm trying to create some search functionality across several types of data, with autocomplete. I'd prefer to have custom views for each autocomplete suggestion, as well as for the suggestions to be grouped according to type. The groups should also be separated.
If my explanation is poor, you can see the search functionality on hotels.com for an example: The suggestions are grouped according to city, landmarks, airports etc.
I've been looking at the JQuery UI Autocomplete plugin, and it seems to be able to do most of what I need, but I have not seen any example of the grouping.
Since my javascript/JQuery skills are a bit lacking, I'm hoping someone here could tell me whether it is possible to achieve what I want Autocomplete plugin, or if there's some other plugin that might do the trick? An example/outline of how it can be done would also be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过更改默认的 _renderMenu 函数来覆盖自动完成呈现的方式。我做了一些类似于你所说的事情,(1)返回按类别排序的 json 结果,(2)覆盖这个函数。没有代码可以具体帮助您,但这是我自己的代码的示例
You can overwrite the way that autocomplete renders by changing the default _renderMenu function. I did something similar to what you are talking about by (1) returning the json results sorted by category and (2) overwriting this function. No code to help you specifically but here is an example from my own code
我尝试了上面的答案。然而,一个问题是,如果类别没有排序,例如,
它将创建重复的“最爱”和“其他”类别。
这是我为 jquery ui 自动完成分组创建的工作演示。即使项目的类别未按排序顺序,这也可以对项目进行分类。
http://jsfiddle.net/jooooice/qua87frd/
I tried the above answers. However, one problem is that if the category is not ordered, e.g.
it will create duplicates "favourite" and "other" category.
Here is a working demo I created for jquery ui autocomplete grouping. This can categorize items even when their categories are not in sorted order.
http://jsfiddle.net/jooooice/qua87frd/
这是 @natedavisolds 接受的答案,已更新以与 Jquery UI 1.10 一起使用。
This is the accepted answer by @natedavisolds updated for use with Jquery UI 1.10.
另外,你可以将:替换
为:,
否则你会在控制台中看到很多错误,例如:n is undefined,或者item is undefined...
And in addition, you may replace:
with:
otherwise you will see many errors in the console, like: n is undefined, or item is undefined...