Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI - Formatting the autocomplete results. Add image possible?

We're moving from the bassistance.de autocomplete to jQuery UI autocomplete. I can't find as many examples for the jQuery UI version, the documentation seems a little sparse. That could just be me.

I'd like to know if anyone has an example/tutorial which explains how to alter the look and feel of the autocomplete drop down. My code is as follows:

$( "#SearchInput" ).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: "http://servername/index.pl",
            dataType: "json",
            data: {
                term: request.term
            },
            success: function( data ) {
                response( $.map( data.items, function( item ) {
                    return {
                        label: item.id + " - " + item.label,
                        value: item.id
                    }
                }));
            }
        });
    },
});

This works, I get the ID and Label displayed seperated by a hyphen. Ideally I'd like to know how to format how the results are displayed. I'd like the ID then below the ID the label. If possible I'd like to know how to display an image to the right of the text.

If anyone has any pointers on how to achieve this I'd be greatful.

like image 307
martok Avatar asked May 20 '11 09:05

martok


People also ask

How does jQuery autocomplete work?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

What is the default value of Append to option of autocomplete () method?

By default its value is null. This option is used append an element to the menu. By default its value is null. When the value is null, the parents of the input field will be checked for a class of ui-front.

What is UI autocomplete?

Autocomplete mechanism is frequently used in modern websites to provide the users a list of suggestion while typing the beginning word in the text box. It facilitates the user to select an item from the list, which will be displayed in the input field.


1 Answers

There is some documentation on JqueryUI website on how to customize the result layout: http://jqueryui.com/demos/autocomplete/#custom-data.

Some example:

$( "#SearchInput" ).autocomplete({ .... }).data( "autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + "<img src='" + item.imgsrc + "' />" + item.id+ " - " + item.label+ "</a>" )
            .appendTo( ul );
    };
like image 174
Yman Avatar answered Oct 06 '22 05:10

Yman