【发布时间】: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