I am facing a challenge where I want to display both doctors and their connections in an autocomplete search using jQuery. Although I have successfully displayed the doctors, I'm struggling to categorize and display data from both arrays separately under doctor and connection headings. The JSON returned from the server is structured like this:
{"connections":
[{"ConnectionID":"8","ConnectionName":"Saima Fayyaz","ImageName":"201304061217391.5x1.5.jpg"}],
"doctors":
[{"DoctorID":"4","DoctorName":"Imran Saeed Ali","ImageName":"20130516210722ImranSAli.jpg","Type":"Doctor"},
{"DoctorID":"8","DoctorName":"Saima Fayyaz","ImageName":"201304061217391.5x1.5.jpg","Type":"Doctor"},
{"DoctorID":"10","DoctorName":"Sabira Javaid","ImageName":"20130406123414123.jpg","Type":"Doctor"}]}
My JavaScript code snippet for the autocomplete functionality is as follows:
<script type= "text/javascript">
$(document).ready(function () {
$("#people").autocomplete({
minLength: 0,
source: function (request, response) {
$.ajax({
url: '@Url.Action("Search","SearchPathologist")',
dataType: "json",
data: {
term: request.term,
flag: "all"
},
success: function (data) {
//alert(data.doctors);
response($.map(data.doctors, function (item) {
// alert(item.Type);
return {
//data: item.doctors
id: item.DoctorID,
name: item.DoctorName,
photo: item.ImageName,
}
}));
}
});
},
focus: function (event, ui) {
}
})
.data("autocomplete")._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<div style='width:100%; margin:auto; background:white; min-height:20px;'><div style='border-bottom:1px dashed gray;'><div style='float:left; width:16%; min-height:35px; margin:auto;'><img src='http://olivecliq.com:84/carepoint/doctor_images/"+item.photo+"' width='31px' style='margin-top:4px; margin-left:1px;' /></div><div class='mid_card'><h2> " + item.name + "</h2><h3>Use StrongVPN & have a USA or UK IP address anywhere.</h3></div><div style='float:left;width:10%; min-height:25px; margin:auto;'> <img src='@Url.Content("~/images/arrow.png")' width='25' style='margin-top: 10px;' /></div><div style='clear:both;'></div></div>")
.appendTo(ul);
});
Any assistance on how to handle this situation would be highly appreciated.