Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Autocomplete Formatting for Multiple Elements

I have the jQuery UI Autocomplete setup to my liking and working perfectly, but there is one fatal flaw. In my autocomplete I use a custom display like this example. I have something very similar built but with on exception...

The only difference is that I have multiple autocomplete elements of the same class on that one page. Only the first element shows the extra data line, the rest only show the basic autocomplete.

I can get the desired result by just iterating all of those class elements and calling the autocomplete on them, but I was hoping there was a better way of calling it once and having it "just work".

Here's how I'm adding the extra line:

.data( 'autocomplete' )._renderItem = function( ul, item ) {
  return $( '<li></li>' )
  .data( 'item.autocomplete', item )
  .append( '<a>' + item.label + '<br/><small>' + item.desc + '<small></a>' )
  .appendTo( ul );
};

I should note that I'm not getting any console exceptions at all.

like image 926
sshefer Avatar asked Feb 08 '11 18:02

sshefer


People also ask

How do you select multiple values in autocomplete?

AutoComplete control allows you to select multiple values from the suggestions list by using the MultiSelectMode property. Multiple values can be selected when MultiSelectMode value is set to VisualMode or Delimiter. Delimiter mode separates multiple items by using a separator character defined.

How does autocomplete work in jQuery?

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.

How can create autocomplete search box in jQuery?

Syntax: $("TagId"). autocomplete({ source : itemList // List of Words. })

What is jQuery 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.


2 Answers

The only way I can make this work is by changing my code from:

addautocomplete($('.tagEntry'));

To:

$('.tagEntry').each(function() {
     addautocomplete(this);
});
like image 72
Ben Finkel Avatar answered Sep 25 '22 00:09

Ben Finkel


You simply need to override the function via the object prototype, instead of on a single instance.

$.ui.autocomplete.prototype._renderItem = function( ul, item ) {
  return $( '<li></li>' )
  .data( 'item.autocomplete', item )
  .append( '<a>' + item.label + '<br/><small>' + item.desc + '<small></a>' )
  .appendTo( ul );
};

Overwrite the function before activating any autocompletes, but after the script has loaded.

like image 41
Josiah Ruddell Avatar answered Sep 24 '22 00:09

Josiah Ruddell