Я использую автозаполнение JQuery UI для существующего проекта. Ну, производительность собака медленно, особенно при выполненииCache JQuery UI autocomplete Combobox
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 по умолчанию выпадающий пример кода, за исключением моей слабой попытки кэширования. Он возвращает каждый символ в раскрывающемся списке.
Например, если возвращенный набор решение чернь, а следующий был Foobar «Сохраненная копия данных» будет выглядеть следующим образом е о о б г каждый по своей линии
Мне нужно, чтобы это было rabble foobar
Было бы очень приятно, если бы это также работало на пустую строку, поскольку это мой самый налоговый вызов.
Спасибо за вашу помощь
Мы здесь .net-магазин, поэтому на стороне сервера есть C# – Aardvark
Есть несколько способов справиться с этим. Лучший способ, который я нашел, - просто кэшировать весь набор результатов и истекать через некоторое время. Каждый последующий запрос идет против кеша. –