How can I properly format a complex nested JSON for use with jQuery autocomplete? I have been attempting to map my custom JSON data to fit the required jQuery autocomplete format of label and value, but unfortunately, my list is returning as 'undefined'. Here is the setup I am working with:
JSON:
{"data":[{"DT_RowId":"row_A6J851","accounts":{"code":"A6J851","name":"Peter Jackson"}},{"DT_RowId":"row_1D01Q14","accounts":{"code":"1D01Q14","name":"Earl Bundy"}}]}
Javascript:
$('#input-search').autocomplete({
source: function ( request, response ) {
$.ajax({
type: 'GET',
url: '/source.php',
dataType: "json",
success: function( data ) {
response( $.map( data.data.accounts, function( value, key ) {
return {
label: value.name,
value: value.name,
id: value.code
}
}));
}
});
},
create: function() {
$(this).data('ui-autocomplete')._renderItem = function (ul, item) {
return $( "<li></li>" )
.append( "<a>" + item.label + "</a>" )
.appendTo( ul );
};
}
});