我在現有項目上使用Jquery UI自動完成。好了,表現狗緩慢,執行緩存Jquery UI自動完成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「緩存數據」將如下所示 ˚F ö ö b 一個 ř 每個在其自己的行
我需要它是 烏茲別克人 foobar
這將是非常好的,如果這也適用於一個空字符串,因爲這是我最困難的調用。
感謝您的幫助
我們在這裏是一個.net商店,所以服務器端是C# – Aardvark 2010-09-02 22:56:49
有幾種方法可以解決這個問題。我發現的最好方法是簡單地緩存整個結果集並在一段時間後過期。它的每一個後續查詢都會違背緩存。 – 2010-09-03 16:55:25