JQuery UI自动填充带链接的渲染类别

时间:2012-12-07 13:20:30

标签: jquery jquery-ui autocomplete jquery-ui-autocomplete

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

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

我该如何解决这个问题?我尝试使用JQuery UI Autocomplete的“焦点”事件和几种方法(例如“return falsee.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"}]

2 个答案:

答案 0 :(得分:3)

声音(并且看起来)就像你想要类别项目的select / focus事件一样(如果不是这样的情况我会&#39; ll更新我的回答)。

自动完成小部件在内部期望列表项具有与其关联的item.autocomplete数据。为了解决错误,您可以创建&#34;类别&#34;具有适当数据的项目。这将使您能够对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/

答案 1 :(得分:1)

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

(('a -> 'b) * 'a list) -> 'b list