Make Select2 Dropbox to work like an autocomplete

0 comments
If you need a Select2Dropdown box to not include all the records at once and only load records based on what was typed in. Note that the entire set of records is too long and made the dropdown box slower.
1. Add an usual text field:
<%=new Forms.TextField("ProductCode", Model.SearchCriteria.ProductCode,true){placeholder = "-- Please enter --"}%>
2. The Javascript code (must include the jquery-ui file):
image.png
3. Example of the function to set the url dynamically
image.png
4. On the server side we need to create a JSON list based on the following Model object:
image.png
5. Send back a JSON object data containing the results (the list of the object) in response to the AJAX request.
image.png
Result:
image.png
image.png
Example JSON returned from the controller is like this:

{"results": [{"id": "123","text": "XP3 OIL (123)"}, {"id": "456","text": "SYNTHETIC OIL (456)"}, {"id": "789","text": "XP4 OIL (789)"}]}

Comments


Leave a Comment