4

I am having issues with jQuery Autocomplete. I am customizing it to show two kinds of items:

  • actual matches to the search string and
  • suggestions (spelling corrections, related keywords, etc)

The backend does all the work on that; the Autocomplete is just fed two lists.

A picture can explain better:

http://shot.ninjaloot.se/20120412.141112.png (The data is just dummy data use while developing, and the styling is not complete yet)

While the rendering works, selection (click or otherwise) of the items throws an exception that is hard to debug:

TypeError: 'undefined' is not an object (evaluating 'ui.item.data')

As I understand it, the menu.selected function is given a ui argument that has an undefined item key.

Why does this happen, and what can I do about it? If I comment out my custom renderer, selection works.

This is my custom renderer function. It's been lifted verbatim from the one in the UI source files and then extended to do my custom haxing.

$input.data("autocomplete")._renderItem = function(ul, item) {
    var cls = 'ui-menu-item';

    if(item.label === null) {
        cls += ' center disabled';
        item.label = '-- perhaps thou meaneth --';
    }

    return $("<li></li>")
     .data("item.autocomplete", item)
     .append("<a class='ui-corner-all'>" + item.label + "</a>")
     .addClass(cls)
     .appendTo(ul);
};

If any more code is needed, I'd be happy to supply it!

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Lowe Thiderman
  • 498
  • 4
  • 7

1 Answers1

1

SELF-ANSWER! I managed to fix this myself. Appearantly, If you add the class "ui-menu-item" manually, the menu items are considered done and won't get any postprocessing, and the postprocessing is vital for the functionality. If I don't add the class manually, it works.

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265