In addition to handling options from a standard <select>
, Select2 can also retrieve the results from other data sources.
Select2 provides a way to load the data from a local array. You can provide initial selections with array data by providing the option tag for the selected values, similar to how it would be done for a standard select.
Select2 comes with AJAX support built in, using jQuery's AJAX methods. In this example, we can search for repositories using GitHub's API.
When using Select2 with remote data, the HTML required for the
select
is the same as any other Select2. If you need to
provide default selections, you just need to include an
option
for each selection that contains the value and text
that should be displayed.
You can configure how Select2 searches for remote data using the
ajax
option. More information on the individual options
that Select2 handles can be found in the
options documentation for ajax
.
Select2 will pass any options in the ajax
object to
jQuery's $.ajax
function, or the transport
function you specify.