6

我正在使用 JQuery UI 自动完成来快速搜索小部件。我有几个分组项目,如酒店、城市、地区等。我可以呈现类别,但我无法链接它们。当我尝试时,ui 自动完成插件会检测项目等类别。这不是问题,但是当我使用向上/向下箭头或鼠标聚焦它们时,它会返回如下错误:

"TypeError: item is undefined. this.liveRegion.text( item.value );"

我怎样才能解决这个问题?我尝试通过几种方法(如 '或')使用 JQuery UI 自动完成的“焦点”事件return false,但它没有用e.stopPropagatione.preventDefault

这是我的代码:

$.widget("custom.catcomplete", $.ui.autocomplete, {
        _renderMenu: function (ul, items) {
            var searchkey = "";
            var itemtype = "";
            var searchtype = "";

            var self = this, currentCategory = "";
            $.each(items, function (index, item) {
                if (typeof item.kelime != 'undefined') { searchkey = item.kelime; }
                if (item.category != currentCategory) {
                    if (item.category == "Bölge" || item.category == "Şehir") {
                        itemtype = "cat-bolgeler";
                    } else if (item.category == "Otel") {
                        itemtype = "cat-oteller";
                        searchtype = "otel";
                    } else if (item.category == "Yurt Dışı Tur") {
                        itemtype = "cat-ydtur";
                        searchtype = "yurtdisitur";
                    } else if (item.category == "Yurt İçi Tur") {
                        itemtype = "cat-yitur";
                        searchtype = "yurticitur";
                    } else if (item.category == "Cruise") {
                        itemtype = "cat-cruise";
                        searchtype = "cruise";
                    }
                    if (searchtype != "") {
                        ul.append("<li class='ui-autocomplete-category " + itemtype + "' id='" + item.searchid + "'><a href='/arama/" + searchkey + "?k=" + searchtype + "&q=" + searchkey + "'>" + item.category + "</a></li>");
                    } else {
                        ul.append("<li class='ui-autocomplete-category " + itemtype + "' id='" + item.searchid + "'>" + item.category + "</li>");
                    }
                    currentCategory = item.category;
                }
                self._renderItem(ul, item);
            });
        }
    });

    $(".hizliaratext").catcomplete({
        source: function (request, response) {
            $.ajax({
                url: '/filename.aspx',
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                type: "get",
                data: { kelime: request.term },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.label,
                            searchid: item.searchid,
                            category: item.category,
                            link: item.link,
                            kelime: item.kelime
                        }
                    }));
                }
            });
        },
        minLength: 3,
        appendTo: "#hizliara",
        select: function (event, ui) {
            window.location = ui.item.link;
        },
        focus: function (event, ui) {
        }
    }).data("catcomplete")._renderItem = function (ul, item) {
        return $("<li>").data("item.autocomplete", item).append("<a>" + item.label + "</a>").appendTo(ul);
    };

这是“ ?kelime=anka ”的示例 JSON 响应:

[{"searchid":2001,"label":"Alba Ankara Hotel","category":"Otel","link":"/otel-detay/alba-ankara-hotel","kelime":"anka"},{"searchid":2238,"label":"Ankara Madi İnci Hotel","category":"Otel","link":"/otel-detay/ankara-madi-inci-hotel"},{"searchid":2244,"label":"Madi Hotel Ankara","category":"Otel","link":"/otel-detay/madi-hotel-ankara"},{"searchid":2403,"label":"City Hotel Ankara","category":"Otel","link":"/otel-detay/city-hotel-ankara"},{"searchid":2404,"label":"Doğa Residence Ankara","category":"Otel","link":"/otel-detay/doga-residence-ankara"},{"searchid":6779,"label":"Ankara","category":"Şehir","link":"/ustaramaliste.aspx?y=6779"},{"searchid":6785,"label":"Ankara / Çankaya","category":"Bölge","link":"/ustaramaliste.aspx?y=6785"},{"searchid":14601,"label":"İzmir / Çankaya","category":"Bölge","link":"/ustaramaliste.aspx?y=14601"}]
4

2 回答 2

3

听起来(和看起来)你想要类别项目的select/focus事件(如果不是这种情况,我会更新我的答案)。

自动完成小部件在内部期望列表项具有item.autocomplete与之关联的数据。为了避免错误,您可以使用适当的数据创建“类别”项目。这将使您能够对select事件做出反应并摆脱事件上发生的错误focus

更新的小部件代码:

$.widget("custom.catcomplete", $.ui.autocomplete, {
    _renderMenu: function(ul, items) {
        var searchkey = "";
        var itemtype = "";
        var searchtype = "";

        var self = this,
            currentCategory = "";
        $.each(items, function(index, item) {
            if (typeof item.kelime != 'undefined') {
                searchkey = item.kelime;
            }
            if (item.category != currentCategory) {
                if (item.category == "Bölge" || item.category == "Şehir") {
                    itemtype = "cat-bolgeler";
                } else if (item.category == "Otel") {
                    itemtype = "cat-oteller";
                    searchtype = "otel";
                } else if (item.category == "Yurt Dışı Tur") {
                    itemtype = "cat-ydtur";
                    searchtype = "yurtdisitur";
                } else if (item.category == "Yurt İçi Tur") {
                    itemtype = "cat-yitur";
                    searchtype = "yurticitur";
                } else if (item.category == "Cruise") {
                    itemtype = "cat-cruise";
                    searchtype = "cruise";
                }
                if (searchtype != "") {
                    ul.append($("<li class='ui-autocomplete-category " + itemtype + "' id='" + item.searchid + "'><a href='/arama/" + searchkey + "?k=" + searchtype + "&q=" + searchkey + "'>" + item.category + "</a></li>").data("item.autocomplete", {}));
                } else {
                    ul.append($("<li class='ui-autocomplete-category " + itemtype + "' id='" + item.searchid + "'>" + item.category + "</li>").data("item.autocomplete", {}));
                }
                currentCategory = item.category;
            }
            self._renderItem(ul, item);
        });
    }
});

示例:http: //jsfiddle.net/J5rVP/20/

于 2012-12-07T17:18:57.777 回答
1

对于 jquery-ui-1.10+,数据标签名称已更改为“ui-autocomplete-item”,如下所示:

ul.append($("<li class='ui-autocomplete-category'>" + item.Type + "</li>").data("ui-autocomplete-item", {}));
于 2015-08-19T22:56:59.400 回答