Я использую автозаполнение интерфейса Jquery для существующего проекта.Что ж, производительность очень низкая, особенно при выполнении
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, каждый на отдельной строке
Мне нужно, чтобы он был rabble foobar
Было бы очень хорошо, если бы это работало и для пустой строки, поскольку это мой самый сложный вызов.
Спасибо за вашу помощь