jquery ui 自动完成复杂搜索以及前后结果

发布于 2024-11-27 23:47:29 字数 367 浏览 1 评论 0原文

我使用 jquery ui 自动完成进行搜索,效果很好。目前,我预加载了城市和国家/地区的 json 列表,以便加载速度快且无需轮询服务器。然而现在,我想在我的自动完成搜索中添加特定的地点名称,即在我的例子中酒店、餐馆等。

我的问题是我是否应该放弃预加载的城市和国家列表并进行简单的普通ajax数据库查询? q= 正如他们所输入的那样,或者是否有可能并且在两者之间进行某种混合是否有用。 IE 预加载列表直到达到一定数量的字符或结果少于 5 个?

它会节省加载时间还是只是令人头痛?如果是这样,我该怎么办?

我最初将其作为 ajax 查询,但切换到预加载的 json 以加快速度。城市和国家的完整列表很小,但地点列表很大。 99.9% 的时间人们会搜索位置而不是特定地点。

I have a search using jquery ui autocomplete which works great. Currently I preload a json list of cities and countries so that it loads quickly and has no need to poll the server. However now, I'd like to add to my autocomplete search specific names of places ie in my case hotels, restaurants, etc..

My question is should I just ditch the preloaded city and country list and do a plain vanilla ajax database query ?q= as they type, or is it possible and would it be useful to have some sort of hybrid between the two. IE a preloaded list until they reach a certain number of characters or there are fewer than 5 results?

Would it save load time or just be a headache? And if so, how should I go about it?

I originally had it as an ajax query but switched to the preloaded json to speed it up. The complete list of cities and countries is tiny, however the list of places is large. 99.9% of the time people will search for locations and not specific places.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

别念他 2024-12-04 23:47:29

如何实现此目的:

您可以通过将函数传递给自动完成的 source 选项来完成类似的操作。在该函数中,您将执行一些逻辑来确定是否需要使用 AJAX 调用或只是在客户端过滤数据:

$("#tags").autocomplete({
    source: function(request, response) {
        // This is how autocomplete finds matching results by default:
        var results = $.ui.autocomplete.filter(common, request.term);

        if (results.length < 5) {
            // Perform an ajax call.
            // In the success method, call response(results)
        } else {
            response(results);
        }
    }
});

您关于使用两个源的“混合”自动完成的两个想法都很有趣。然而,在测试该解决方案时,我遇到了潜在的可用性问题。通常,使用这种类型的控件,您会期望在键入时返回更少结果。

例如:假设您的本地源数组如下所示:

['jquery', 'jquery-ui', 'jquery-ajax', 'jquery-plugins']

并且您使用在键入 5 个字符后显示更多结果的策略。用户输入 jquery 后,输入 y 将触发 AJAX 调用,用更多结果填充列表,这对于用户来说可能并不简单,具体取决于您的用例。

示例: http://jsfiddle.net/andrewwhitaker/wSBa3/

我认为当您对可以从服务器返回的结果数量设置一些限制时,情况会更好。如果您在 AJAX 调用发生之前将此值与最小结果数同步,则交互看起来并不那么时髦。事实上,这个例子让这个概念对我来说更加合理:

示例: http:// jsfiddle.net/andrewwhitaker/NxELC/


值得吗?

它会节省加载时间还是只是令人头疼?

也许两者都有!

我认为这实际上取决于查询的资源密集程度、我们正在讨论的数据量以及这样的解决方案的可维护性。我无法真正推测前两个选项,但我想说,维护两个列表可能会遇到维护问题。这个问题在某种程度上取决于您使用的服务器端技术。我想如果你做得正确的话,就不会有任何麻烦。

提前为冗长的答案表示歉意,但我希望它有所帮助。

How you would achieve this:

You could accomplish something like this by passing a function to the source option of autocomplete. Within that function, you would perform some logic to determine if you needed to use an AJAX call or simply filter the data on the client-side:

$("#tags").autocomplete({
    source: function(request, response) {
        // This is how autocomplete finds matching results by default:
        var results = $.ui.autocomplete.filter(common, request.term);

        if (results.length < 5) {
            // Perform an ajax call.
            // In the success method, call response(results)
        } else {
            response(results);
        }
    }
});

Both of your ideas for a "hybrid" autocomplete using two sources were intriguing. However, while testing out the solution, I encountered a potential usability problem. Normally with this type of control you would expect to get fewer results back as you type.

For example: lets say your local source array looks like this:

['jquery', 'jquery-ui', 'jquery-ajax', 'jquery-plugins']

And you use the strategy of showing more results after, say, 5 characters are typed. After the user types j-q-u-e-r-y, typing the y would trigger an AJAX call, populating the list with more results, which may not be straightforward for the user depending on your use case.

Example: http://jsfiddle.net/andrewwhitaker/wSBa3/

I think this gets better when you set some limit on the number of results that can come back from the server. If you sync this value up with the minimum number of results before an AJAX call occurs, the interaction doesn't seem as funky. In fact, this example makes the concept more plausible to me:

Example: http://jsfiddle.net/andrewwhitaker/NxELC/


Is it worth it?

Would it save load time or just be a headache?

Maybe both!

I think this really depends on how resource-intensive the query is, how much data we're talking about, and also how maintainable a solution like this would be. I can't really speculate on the first two options, but I would say that you could run into a maintenance problem with maintaining two lists. That problem sort of depends on what server-side technology you're using. I think if you did it correctly, you wouldn't have any trouble though.

Apologies in advance for the lengthy answer, but I hope it helps.

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