Certificazione impianto gas: come trovare idraulici
Come Trovare un Idraulico per la Certificazione dell'Impianto a Gas.
Trovare un Idraulico per la Certificazione dell'Impianto a Gas.
Scopri come utilizzare il widget Autocomplete di jQuery UI all'interno di un'applicazione ASP.NET MVC. In questo articolo, ti guiderò passo dopo passo nella trasformazione di un elemento Select in un box Autocomplete, ideale per presentare grandi set di dati senza far scorrere l'intera lista. Approfitta di questa funzionalità per migliorare l'esperienza utente!
Trova professionisti vicino a te
Trova professionisti
Recently, I wrote an article about using the jQuery UI Autocomplete widget in an ASP.NET MVC application. The Autocomplete widget provides suggestions while you type into the field. In that article, I described a few tricks to get the most out of the Autocomplete widget, including using it with an input element.
In this article, I will show you how to use the Autocomplete widget with a Select element. In other words, I will show you how to turn a DropDownList into an Autocomplete box. This can be useful when you have a large set of data that you want to present as a list of options, but you don't want the user to have to scroll through all the options.
1. Create the HTML
The first step is to create the HTML for your page. We'll need a Select element and a div to contain the Autocomplete widget:
2. Create the JavaScript
The next step is to create the JavaScript for the Autocomplete widget. We'll use the jQuery UI Autocomplete widget to create the Autocomplete box. The Autocomplete widget takes an array of objects, so we'll need to create an array of objects from the Select element. We can use the jQuery .map() function to do this:
var data = $("#ddlAutocomplete option").map(function () { return { label: $(this).text(), value: $(this).val() }; });
Once we have the array of objects, we can create the Autocomplete widget. We'll specify the array of objects as the source of the Autocomplete widget, and we'll also specify a callback function to be called when an item is selected:
$("#divAutocomplete").autocomplete({ source: data, select: function (event, ui) { $("#ddlAutocomplete option[value='" + ui.item.value + "']").attr("selected", "selected"); } });
In the select callback function, we get the value of the item that was selected, and then we set the corresponding option in the Select element to be selected.
3. The Result
Finally, we have a DropDownList that has been transformed into an Autocomplete box:
Conclusion
In this article, I showed you how to use the jQuery UI Autocomplete widget with a Select element. This can be a useful way to provide a large list of options to the user without having them scroll through the entire list.