Hi @Jose Daniel Navarro Brito ,
To address the issue where the jQuery UI Autocomplete dropdown is not displaying the label in the input box, please consider the following recommendations:
1. Ensure Correct Data Format:
The API should return an array of objects with label
and value
properties, which matches the expected format for jQuery UI Autocomplete.
2. Script and CSS Loading Order:
Confirm that the following resources are included in the correct order:
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>
3. Modal and z-index Considerations:
When using Autocomplete within a Bootstrap modal, the dropdown may appear behind the modal. To resolve this, add the ui-front
class to the modal body or the container of the search input:
<div class="modal-body ui-front">
<!-- Search input here -->
</div>
Additionally, apply the following CSS to ensure the dropdown appears above the modal:
.ui-autocomplete {
z-index: 2000;
max-height: 200px;
overflow-y: auto;
overflow-x: hidden;
}
4. Initialize Autocomplete on Modal Show:
It is advisable to initialize the Autocomplete widget when the modal is displayed:
$('#searchModal').on('shown.bs.modal', function () {
$("#search").autocomplete({
source: function(request, response) {
$.ajax({
url: "/Erf/AutoComplete",
dataType: "json",
type: "GET",
data: { param: request.term },
success: function(data) {
response(data);
},
error: function(xhr, status, error) {
toastr.error("Error: " + error);
}
});
},
minLength: 2,
select: function(event, ui) {
if (ui.item) {
$("#search").val(ui.item.label);
// Optionally handle ui.item.value as needed
return false;
}
}
});
});
5. Avoid Framework Conflicts:
When using both Bootstrap and jQuery UI, conflicts may arise. If issues persist, consider using a Bootstrap-compatible autocomplete plugin such as typeahead.js or bootstrap-autocomplete.
6. Controller Action Format:
Ensure the controller returns data in the correct format:
public class AutoSearchViewModel
{
public string label { get; set; }
public string value { get; set; }
}
public async Task<JsonResult> AutoComplete(string param)
{
var httpClient = _httpClientFactory.CreateClient("HousingWebAPI");
using (var response = await httpClient.GetAsync("AsyncAutocompleteErf/" + param))
{
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var apiResponseBody = JsonConvert.DeserializeObject<IEnumerable<AutoSearchViewModel>>(content);
return Json(apiResponseBody);
}
}
return null;
}
In summary, verify the data format, ensure proper script and CSS loading, address modal z-index issues, initialize Autocomplete at the appropriate time, and consider framework compatibility. These steps should resolve the issue with the Autocomplete dropdown not displaying as expected.
Hope this resolves your issue effectively. If you agree with my suggestion, feel free to interact with the system accordingly!