Parameter | Type | Description |
minimumInputLength | int | Number of characters necessary to start a search |
placeholder | object/string |
Initial value that is selected if no other selection is made.
The value can be specified either as an object or as a string . If
specified as a string any custom formatSelection function will be
bypassed and the string value will be rendered directly.
The placeholder can also be specified as a data-placeholder attribute on the select
or input element that Select2 is attached to. In this case only string
values are supported
Note that because browsers assume the first option element
is selected in
non-multi-value select boxes an empty first option element must be provided (<option></option> )
for the placeholder to work.
|
allowClear |
boolean |
Whether or not a clear button is displayed when the select box has a selection. The
button,
when clicked, resets the value of the select box back to the placeholder, thus this option is
only
available when the placeholder is specified.
Also, note that this option only works with
non-multi-value based selects because multi-value selects always provide such a button for every
selected option.
|
multiple |
boolean |
Whether or not Select2 allows selection of multiple values.
When Select2 is attached to a select element this value will be ignored and select 's
multiple attribute will be used instead.
|
formatSelection | function |
Function used to render the current selection.
formatSelection(object)
Parameter | Type | Description |
object | object | The selected result object returned from the query function |
<returns> | string | Html that represents the selection |
The default implementation expects the object to have a text property that is returned.
|
formatResult | function |
Function used to render a result that the user can select.
formatResult(object)
Parameter | Type | Description |
object | object | One of the result objects returned from the query function |
<returns> | string | Html that represents the result |
The default implementation expects the object to have a text property that is returned.
|
formatNoMatches | function |
Function used to render the "No matches" message
formatNoMatches(term)
Parameter | Type | Description |
term | string | Search string entered by user |
<returns> | string | Message html |
|
formatInputTooShort | function |
Function used to render the "Search input too short" message
formatInputTooShort(term, minLength)
Parameter | Type | Description |
term | string | Search string entered by user |
minLength | int | Minimum required term length |
<returns> | string | Message html |
|
query |
function |
Function used to query results for the search term.
query(options)
Parameter | Type | Description |
options.term | string | Search string entered by user |
options.page | int | 1-based page number tracked by Select2 for use with infinite scrolling of results |
options.callback | function |
Callback function that should be called with the result object. The result object:
Parameter | Type | Description |
result.results | [object] | Array of result objects. The default renderers expect objects with id and text keys. The id attribute is required, even if custom renderers are used. |
result.more | boolean | true if more results are available for the current search term |
|
In order for this function to work Select2 should be attached to a input type='hidden' tag instead of a select .
|
ajax | object |
Options for the built in ajax query function. This object acts as a shortcut for having to manually write a function that performs ajax requests. The built-in function supports more advanced features such as throttling and dropping out-of-order responses.
Parameter | Type | Description |
url | string | Ajax url |
dataType | stringt | Data type for the request. ajax , jsonp , other formats supported by jquery |
quietMillis | int | Number of milliseconds to wait for the user to stop typing before issuing the ajax request |
data | function |
Function to generate query parameters for the ajax request.
data(term, page)
Parameter | Type | Description |
term | string | Search term |
page | int | 1-based page number tracked by Select2 for use with infinite scrolling of results |
<returns> | object | Object containing url paramters |
|
results | function |
Function used to build the query results object from the ajax response
results(term, page)
Parameter | Type | Description |
term | string | Search term |
page | int | Page number that was passed into the data function above |
<returns> | object | Results object. See "options.callback" in the "query" function for format. |
|
In order for this function to work Select2 should be attached to a input type='hidden' tag instead of a select .
|
data | array/object |
Options for the built in query function that works with arrays.
If this element contains an array, each element in the array must contain id and text keys
Alternatively, this element can be specified as an object in which results key must contain the data as an array and a text key can either be the name of the key in data items that contains text or a function that retrieves the text given a data element from the array
|
Gets or sets the selection. If the value
parameter is not specified, the id
attribute of the currently selected element is returned. If the value
parameter is specified it will become the current selection.