views:

102

answers:

1

Im using JQuerys Autocomplete plugin, but it doesn't autocomplete upon entering anything.

Any ideas why it doesnt work? The basic example works, but not mine.

var ppl = {"ppl":[{"name":"peterpeter", "work":"student"},
     {"name":"piotr","work":"student"}]};

var options = {
    matchContains: true,  // So we can search inside string too
    minChars: 2,      // this sets autocomplete to begin from X characters
    dataType: 'json', 
    parse: function(data) {
        var parsed = [];
        data = data.ppl;
        for (var i = 0; i < data.length; i++) {
            parsed[parsed.length] = {
                data: data[i],  // the entire JSON entry
                value: data[i].name,  // the default display value
                result: data[i].name // to populate the input element 
            };
        }
        return parsed;
    },
    // To format the data returned by the autocompleter for display
    formatItem: function(item) { 
        return item.name; 
    }
};

$('#inputplace').autocomplete(ppl, options);

Ok. Updated:

<input type="text" id="inputplace" />

So, when entering for example "peter" in the input field. No autocomplete suggestions appear. It should give "peterpeter" but nothing happens.

And one more thing. Using this example works perfectly.

var data = "Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax Utilities".split(" ");
$("#inputplace").autocomplete(data);
+3  A: 

Well, looking at the code in that plugin, the "parse" option looks like it's only called when the data is retrieved by an AJAX call. As an experiment, you might try passing in the data such that you don't need it:

var ppl = [{"name":"peterpeter", "work":"student"},
 {"name":"piotr","work":"student"}];
Pointy
Ooooh, thats why. Yes, tried with your version of ppl and took away the parse function and now it works! :DD Yes! Thank you so much!
heffaklump
OK, well that's great! Of course if you eventually *do* use Ajax callbacks, you can restore your "parse" handler. (I don't think it hurts anything to have it part of the options when you're not doing Ajax, if that would help with testing.)
Pointy