【问题标题】:Cache Jquery UI autocomplete Combobox缓存 Jquery UI 自动完成组合框
【发布时间】:2011-04-07 14:33:27
【问题描述】:

我在现有项目中使用 Jquery UI 自动完成功能。好吧,性能很慢,尤其是在执行

input.autocomplete("search", "");

我的解决方案是缓存信息,所以即使它的狗很慢,它也只会出现一次。我想我遗漏了一个非常简单的 Javascript 错误,如果能帮我解决这个问题,我将不胜感激。

这里是代码

input.autocomplete(
        {
            delay: 0,
            minLength: 0,
            source: function (request, response)
            {
                if (request.term in cache)
                {
                    response(cache[request.term]);
                    return;
                }
                // The source of the auto-complete is a function that returns all the select element's child option elements.
                var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                response(select.children("option").map(function ()
                {
                    var text = $(this).text();
                    if (this.value && (!request.term || matcher.test(text)))
                    {
                        cache[request.term] = text;
                        return { label: text, value: text, option: this };
                    }
                }));
            },

            select: function (event, ui)
            {
                // On the select event, trigger the "selected" event with the selected option. Also update the select element
                // so it's selected option is the same.
                ui.item.option.selected = true;
                self._trigger("selected", event,
                {
                    item: ui.item.option
                });
            },

            change: function (event, ui)
            {
                // On the change event, reset to the last selection since it didn't match anything.
                if (!ui.item)
                {
                    $(this).val(select.children("option[selected]").text());
                    return false;
                }
            }
        });

        // Add a combo-box button on the right side of the input box. It is the same height as the adjacent input element.
        var autocompleteButton = $("<button type='button' />");
        autocompleteButton.attr("tabIndex", -1)
                          .attr("title", "Show All Items")
                          .addClass("ComboboxButton")
                          .insertAfter(input)
                          .height(input.outerHeight())
                          .append($("<span />"))
        autocompleteButton.click(function ()
        {
            // If the menu is already open, close it.
            if (input.autocomplete("widget").is(":visible"))
            {
                input.autocomplete("close");
                return;
            }

            // Pass an empty string as value to search for -- this will display all results.
            input.autocomplete("search", "");
            input.focus();
        });

几乎所有这些都是默认的 jquery UI 组合框示例代码,除了我微弱的缓存尝试。它返回下拉列表中的每个字符。

例如,如果返回的解决方案集是 rabble,而下一个是 foobar,则“缓存数据”将如下所示 F ○ ○ b 一种 r 各占一行

我需要它 乌合之众 富吧

如果这也适用于空字符串,那就太好了,因为那是我最费力的电话。

感谢您的帮助

【问题讨论】:

    标签: javascript jquery jquery-ui caching autocomplete


    【解决方案1】:

    如果您通过设置适当的 HTTP 标头来缓存 Web 服务结果会容易得多。这样浏览器就会为你处理所有令人讨厌的缓存失效问题。

    在 C# 中,您可以像这样设置一年后的 Expires 标头:

    Response.Cache.SetExpires(DateTime.Now.AddYears(1));
    

    关于 HTTP 缓存的最佳参考是 Mark Nottingham 的 Caching Tutorial for Web Authors and Webmasters。我在这里没有回答的任何内容都在该文档中。

    更具体的 C# 资源是 dotnetperls.com 的 ASP.Net Cache Examples and Overview

    【讨论】:

      【解决方案2】:

      jQuery AutoComplete 默认缓存。您可能想要做的是在服务器端进行一些分页或缓存以限制性能压力。您使用的是什么服务器端技术?

      【讨论】:

      • 我们这里是.net商店,所以服务器端是C#
      • 有几种方法可以解决这个问题。我发现的最好方法是简单地缓存整个结果集并在一段时间后使其过期。对它的每个后续查询都会违反缓存。
      猜你喜欢
      • 2011-11-16
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多