jQuery-Autocomplete icon indicating copy to clipboard operation
jQuery-Autocomplete copied to clipboard

Format suggestion inside suggestions container

Open juancrrn opened this issue 10 years ago • 3 comments

Hello! I'm using the following code in an app related to borrowing books, so the users can find the books with the search input:

$('#input').autocomplete({
    lookup: books,
    onSelect: function (suggestion) {
        $('#container table tbody').append('<tr class="book" data-id="' + suggestion.data.id + '"><td>' + suggestion.data.name + '</td><td><img src="' + suggestion.data.img + '"></td><td>' + suggestion.data.isbn + '</td></tr>');
        }
    }
});

As you can see, when the user finds the book and clicks it, it appends a TR to a TABLE. That TR contains an IMG, is there any way to get that IMG inside the suggestions container when the user searches for it? Thank you!

juancrrn avatar Mar 24 '16 11:03 juancrrn

Yes, use formatResult to format suggestion in any way you like.

tkirda avatar Mar 24 '16 14:03 tkirda

Sorry, the problem is that I'm not used to work with jQuery, and I'm Spanish so my English is not excellent. An example (like how to add <div>' + suggestion.data.name + '</div><div><img src="' + suggestion.data.img + '"></div>) would help me a lot :)

juancrrn avatar Mar 24 '16 16:03 juancrrn

$('#input').autocomplete({
    lookup: books,
    formatResult: function (suggestion) {
        return '<div><img src="' + suggestion.data.img + '" width="20" /> ' + suggestion.value + '</div>';
    },
    onSelect: function (suggestion) {
        $('#container table tbody').append('<tr class="book" data-id="' + suggestion.data.id + '"><td>' + suggestion.data.name + '</td><td><img src="' + suggestion.data.img + '"></td><td>' + suggestion.data.isbn + '</td></tr>');
        }
    }
});

tkirda avatar Mar 24 '16 18:03 tkirda