In jQuery UI Autocomplete, you can customize the way each item in the dropdown is displayed by overwriting the _renderItem
method. Here’s how you can modify your code to achieve the desired output:
$("#txtDirectManagerId").autocomplete({
source: function (request, response) {
// ... your existing AJAX call ...
},
// ... other options ...
}).data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li>")
.append("<div>Id: " + item.value + "</div><div>Name: " + item.employeeName + "</div><div>Designation: " + item.Designation + "</div>")
.appendTo(ul);
};
In the _renderItem
function, ul
is the unordered list jQuery UI uses to build the dropdown, and item
is the object for each individual item in the list. Here, we are appending a new list item (<li>
) with the formatted HTML to the ul
.
Make sure to add Designation
to the item object in the response
callback:
response($.map(data, function (item) {
return {
label: "Id: " + item.EmployeeID + "\nName: " + item.EmployeeName + "\nDesignation: " + item.Designation,
value: item.EmployeeID,
employeeName: item.EmployeeName,
Designation: item.Designation
};
}))
This way, each property (Id, Name, Designation) will be displayed on a new line in the dropdown, as you want.