views:

3003

answers:

3

I have a string of values separated by a space that I return to the Jquery call, but it does not separate the words into rows. Should the data be returned as a string or a list or something else?

+4  A: 

If you're using the plugin located here, then I think you return results in JSON format. Here's how to do it with ASP.Net MVV.

nickohrn
+5  A: 

By default it wants the results separated by newlines. If you supply a list of values separated by spaces you'll want to provide a parse function that will take the returned data and turn it into an array. The same is true if you supply a list of values instead.

Here's a sample from a project I'm working on that returns a list of strings via JSON from an MVC action.

$('#eventName').autocomplete( '<%= Url.Action("SearchEvent", "Donor" ) %>', {
    dataType: "json",
    formatItem: function(data,i,max,value,term){
       return value;
    },
    parse: function(data){
        var array = new Array();
        for(var i=0;i<data.length;i++)
        {
            array[array.length] = { data: data[i], value: data[i], result: data[i] };
        }
        return array;
   }
});
tvanfosson
Yes, the "parse" override is the way to go. jQuery really needs better documentation for their Autocomplete (as of this comment, it is still "planned to be released w/jQuery 1.7"). But if you are doing to document existing code, they should document all.The only problem with use the parse override is now all functions of the autocompleter now will be passed the new Array(). So, you have to account for this. tvanfosson showed how you account for the formatting of display with the formatItem override. But also note, that you may need to override the result() method.
eduncan911
+1  A: 

There is another autocomplete plugin that wants to have a string separated by the '|' character like this:

item 1 | item 2 |

You might want to check the docs of the one you're using !

Morph