jquery autocomplete renderItem

时间:2013-03-27 17:06:07

标签: jquery-ui jquery-ui-autocomplete

我有以下代码。它不会生成js错误。无法获得自动填充功能以显示任何结果:

$(function() {
    $.ajax({
    url: "data.xml",
    dataType: "xml",
    cache: false,
    success: function (xmlResponse) {
        var data_results = $("Entry", xmlResponse).map(function () {
            return {
                var1: $.trim($("Partno", this).text()),
                var2: $.trim($("Description", this).text()),
                var3: $.trim($("SapCode", this).text()),
                var4: $("Title", this).text(),
                var5: $.trim($("File", this).text()),
                var6: $.trim($("ItemID", this).text())
            };
        }).get();

        $("#searchresults").autocomplete({
            source: data_results,
            minLength: 3,
            select: function (event, ui) {
                ...
            }
        }).data( "autocomplete" )._renderItem = function( ul, item ) {
                return $( "<li></li>" ).data("item.autocomplete", item)
                    .append( "<a>" + item.var1 + "<br>" + item.var2 + "</a>")
                    .appendTo( ul );
        };

    }
});

我可能缺少什么想法?提前谢谢。

2 个答案:

答案 0 :(得分:29)

似乎.data('autocomplete')现在是.data('ui-autocomplete')

来源:http://jqueryui.com/upgrade-guide/1.10/#removed-data-fallbacks-for-widget-names

答案 1 :(得分:7)

默认情况下,自动填充功能要求您的源数组包含具有 label 属性, value 属性或两者的对象。< / p>

考虑到这一点,你有两个选择:

  1. 从AJAX调用处理数组时,向源对象添加标签或值属性:

    var data_results = $("Entry", xmlResponse).map(function () {
        return {
            var1: $.trim($("Partno", this).text()),
            var2: $.trim($("Description", this).text()),
            var3: $.trim($("SapCode", this).text()),
            var4: $("Title", this).text(),
            var5: $.trim($("File", this).text()),
            var6: $.trim($("ItemID", this).text()),
            value: $.trim($("Description", this).text())
        };
    }).get();
    

    您指定的value将在focusselect上使用,并进行搜索。

  2. 更改source功能以执行自定义过滤逻辑:

    $("#searchresults").autocomplete({
        source: function (request, response) {
            var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
    
            response($.grep(data, function (value) {
                return matcher.test(value.var1) || 
                       matcher.test(value.var2);
                /* etc., continue with whatever parts of the object you want */
            }));
        },
        minLength: 3,
        select: function (event, ui) {
            event.preventDefault();
            this.value = ui.var1 + ui.var2;
        },
        focus: function (event, ui) {
            event.preventDefault();
            this.value = ui.var1 + ui.var2;
        }
    }).data( "autocomplete" )._renderItem = function( ul, item ) {
            return $( "<li></li>" ).data("item.autocomplete", item)
                .append( "<a>" + item.var1 + "<br>" + item.var2 + "</a>")
                .appendTo( ul );
    };
    

    请注意,使用此策略,您必须实施自定义selectfocus逻辑。