jquery autocomplete renderItem
Asked Answered
B

2

14

I have the following code. It generates no js errors. Can't get the autocomplete to display any results:

$(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 );
        };

    }
});

Any ideas what I might be missing? Thanks in advance.

Brewton answered 27/3, 2013 at 17:6 Comment(0)
M
32

It seems that .data('autocomplete') is now .data('ui-autocomplete').

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

Marguerite answered 18/7, 2013 at 10:37 Comment(0)
F
7

By default, autocomplete expects your source array to contain objects with either a label property, a value property, or both.

With that in mind you have two options:

  1. Add a label or value property to your source objects when you process the array from your AJAX call:

    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();
    

    The value you assign will be used on focus, select, and to search on.

  2. Change the source function to perform custom filtering logic:

    $("#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 );
    };
    

    Note that with this strategy you have to implement custom select and focus logic.

Felipafelipe answered 30/3, 2013 at 0:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.