javascript - JQuery Autocomplete using a global JSON object -


i have json array of objects looks like:

jsonobject=       [{"description":"mac , cheese", "servingsize":"1 cup", "calories":"500"},  {"description":"slice of pizza", "servingsize":"1 slice", "calories":"400"},  {"description":"oreo cookie", "servingsize":"1 cookie", "calories":"100"},  {"description":"salad", "servingsize":"1 cup", "calories":"50"},  {"description":"apple", "servingsize":"1 apple", "calories":"70"}] 

i trying create autocomplete text box using description field of jsonobject. have tried following code (on textbox called "food") not display anything:

$('#food').autocomplete({source:jsonobject,                         select: function(event, ui) {                                        selectedobj = ui.item;                                        alert("selected object="+selectedobj.value);},                         minlength:1                     }); 

i tried creating separate array of label/value pairs descriptions jsonobject, still nothing displaying. can see missing?

i hoping keep jsonobject in tact, trying access serving size , calories associated chosen food "description".

edit: here complete code. perhaps find problem...

var jsonversion;  $(document).ready(function($){ $.ajax({     type: "get",     url: "test.js",     datatype: "text",     success: function(data) {                                 data = $.csv.toarrays(data);                                 var arrayofobjects = new array();                                  for(var i=1; i<data.length; i++)//skipping on header                                 {                                     var foodobject = new object();                                     foodobject.number = data[i][0];                                     foodobject.description = data[i][1];                                     foodobject.weight = data[i][2];                                     foodobject.servingsize = data[i][3];                                     foodobject.calories = data[i][4];                                     arrayofobjects.push(foodobject);                                 }                                 jsonversion = json.stringify(arrayofobjects);                                 }  });  });  $('#food').autocomplete({source:jsonversion,                           minlength:1                           }); 


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -