views:

6037

answers:

2

im getting response in json, but this wont parse the json response. what m i doing wrong? i could'nt find anything on doc http://docs.jquery.com/Plugins/Autocomplete

$("#users-allowed").autocomplete("/people/following.json", {
         width: 320,
    //   max: 4,
         highlight: false,
         scroll: true,
         scrollHeight: 300,
         formatItem: function(response, i, max) {
          console.log(response);
          console.log(response['items']);
          console.log(response.items);
          return i + "/" + max + ": \"" + response.status_code + "\" [" + response.status_description + "]";

          //return "<img src='images/" + value + "'/> " + value.split(".")[0];
         },
         formatResult: function(response) {
          //return value.split(".")[0];
          return response.status_description;
         }
        });
+1  A: 

I think you just need to throw in a dataType option, I remember readying that you can use any of $.ajax's options in the autocompleter:

$("#users-allowed").autocomplete("/people/following.json", {
    dataType: "json",
    ...
karim79
with that im getting data.split is not a functionjquery/jquery.autocomplete.jsLine 11 error
Basit
btw im not using that function anywhere.. you can see above my code
Basit
@basit - because now you're working with an object, `split` is a function of type `Array`.
karim79
+5  A: 
$("#users-allowed").autocomplete("/people/following.json", {
     width: 320,
     dataType: 'json',
     highlight: false,
     scroll: true,
     scrollHeight: 300,
     parse: function(data) {
      var array = new Array();
      for(var i=0;i<data.items.length;i++)
      {
       array[array.length] = { data: data.items[i], value: data.items[i], result: data.items[i].username };
      }
      return array;
     },

     formatItem: function(row) {      
      var name = '';
      if (row.first_name && row.last_name)
       name = '('+row.first_name+', '+row.last_name+')';
      else if (row.first_name)
       name = '('+row.first_name+')';
      else if (row.last_name)
       name = '('+row.last_name+')';

      return row.username+' '+name;
     }
    });

check dataType and parse option.

Basit
btw this is the correct answer with the help of karim79
Basit
Hi basit, I have the same problem, what is it that you did because I am getting a data.split is not a function
Angela
I tried to follow this answer and it doesn't work. the formatItem function argument is undefined.
James Lin