Select2 supports connecting to a remote data source using the ajax
option.
You can refer to the following Stack Overflow answer if you want to set the initial value for AJAX requests: Select2 4.0.0 initial value with AJAX
You can use the ajax.processResults
option to modify the data returned from the server before passing it to Select2.
By default, Select2 will trigger a new AJAX request whenever the user changes their search term. You can set a time limit for debouncing requests using the ajax.delay
option.
This will tell Select2 to wait 250 milliseconds before sending the request out to your API.
When connecting Select2 to a remote data source, you have the option of using either a single endpoint (a single page which handles all requests) or a dynamic endpoint (one of many pages). You can point Select2 to a single endpoint during initialization by specifying a string for the ajax.url
option.
If there isn't a single url for your search results, or you need to call a function to determine the url to use, you can specify a function for the ajax.url
option, and this will be used instead. The query parameters will be passed in through the params
option.
By default, Select2 will send the query term as well as the pagination data as query parameters in requests. You can override the data that is sent to your API, or change any of the query paramters, by overriding the ajax.data
option.
Select2 expects that the results that are returned from the remote endpoint are already filtered ahead of time based on the search term. If your remote endpoint just returns the list of all possible options, you may be interested in using Select2's support for data arrays.
jQuery.ajax
be used?
Select2 uses the transport method defined in ajax.transport
to send requests to your API. By default, this transport method is jQuery.ajax
but this can be changed.