[英]Cache Jquery UI autocomplete Combobox
我在现有项目上使用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,则“缓存的数据”将看起来像此foobar,每行分别
我需要它成为愚蠢的foobar
如果这也适用于空字符串,那就太好了,这就是我最费力的电话。
谢谢你的帮助
jQuery AutoComplete默认情况下缓存。 您可能想做的是在服务器端进行一些分页或缓存,以限制性能负担。 您正在使用哪种服务器端技术?
如果通过设置适当的HTTP标头来缓存Web服务结果,将会容易得多。 这样,浏览器将为您解决所有烦人的缓存失效问题。
在C#中,您可以设置未来一年的Expires标头,如下所示:
Response.Cache.SetExpires(DateTime.Now.AddYears(1));
HTTP缓存的最佳参考是Mark Nottingham的Web作者和网站管理员的缓存教程 。 该文档中没有我要回答的任何内容。
更多C#特定资源是dotnetperls.com的ASP.Net缓存示例和概述 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.