我正在使用jQuery UI的自動完成從遠程源爲搜索輸入框提供建議。我有「遠程數據源」示例工作。例如,這個工程:jQuery UI與類別的自動完成
$("#search").autocomplete({
source: "search_basic.php",
minLength: 2
});
不過,我想用「Categories」例如,按類別的suggesions排序。從jQuery UI的網站,有一個內嵌的數據集的例子正常工作:
<script>
$.widget("custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function(ul, items) {
var self = this,
currentCategory = "";
$.each(items, function(index, item) {
if (item.category != currentCategory) {
ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
currentCategory = item.category;
}
self._renderItem(ul, item);
});
}
});
$(function() {
var data = [
{ label: "anders", category: "" },
{ label: "andreas", category: "" },
{ label: "antal", category: "" },
{ label: "annhhx10", category: "Products" },
{ label: "annk K12", category: "Products" },
{ label: "annttop C13", category: "Products" },
{ label: "anders andersson", category: "People" },
{ label: "andreas andersson", category: "People" },
{ label: "andreas johnson", category: "People" }
];
$("#search").catcomplete({
delay: 0,
source: data
});
});
</script>
然而,當我嘗試從我的遠程文件
source: 'search.php'
不建議任何獲取數據。下面是用的search.php代碼:
<script>
$.widget("custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function(ul, items) {
var self = this,
currentCategory = "";
$.each(items, function(index, item) {
if (item.category != currentCategory) {
ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
currentCategory = item.category;
}
self._renderItem(ul, item);
});
}
});
$(function() {
$("#search").catcomplete({
source: 'search.php'
});
});
</script>
的數據search.php中被返回的格式正確無誤
[
{ label: "annhhx10", category: "Products" },
{ label: "annttop", category: "Products" },
{ label: "anders", category: "People" },
{ label: "andreas", category: "People" }
]
任何幫助將不勝感激!
感謝, 格雷格
我有同樣的問題。你是如何解決它的? – 2015-05-07 18:52:46