[SELECT2] Full Options (ok)
https://jeesite.gitee.io/front/jquery-select2/4.0/options.htm
Last updated
Was this helpful?
https://jeesite.gitee.io/front/jquery-select2/4.0/options.htm
Last updated
Was this helpful?
Select2 will register itself as a jQuery function if you use any of the distribution builds, so you can call .select2()
on any jQuery element where you would like to initialize Select2.
You can optionally pass an object containing all of the options that you would like to initialize Select2 with.
In some cases, you need to set the default options for all instances of Select2 in your web application. This is especially useful when you are migrating from past versions of Select2, or you are using non-standard options . Select2 exposes the default options through $.fn.select2.defaults
, which allows you to set them globally.
When setting options globally, any past defaults that have been set will be overridden. Default options are only used when an option is requested that has not been set during initialization.
You can set default options by calling $.fn.select2.defaults.set("key", "value")
.
You can reset the default options to their initial values by calling
It is recommended that you declare your configuration options for Select2 when initializing Select2. You can also define your configuration options by using the HTML5 data-*
attributes, which will override any options set when initializing Select2 and any defaults.
camelCase
options be written?HTML data attributes are case-insensitive, so any options which contain capital letters will be parsed as if they were all lowercase. Because Select2 has many options which are camelCase, where words are separated by uppercase letters, you must write these options out with dashes instead. So an option that would normally be called allowClear
should instead be defined as allow-clear
.
This means that if you declare your <select>
tag as...
Will be interpreted the same as initializing Select2 as...
Which will be interpreted the same as initializing Select2 with...
For most AMD and CommonJS setups, the location of the data files used by Select2 will be automatically determined and handled without you needing to do anything.
If you are using Select2 in a build environment where preexisting module names are changed during a build step, Select2 may not be able to find optional dependencies or language files. You can manually set the prefixes to use for these files using the amdBase
and amdLanguageBase
options.
By upgrading to version 2.1.18 or higher of the r.js build tool, you will be able to fix the issue.
dist
or src
?Select2 internally uses AMD and the r.js build tool to build the files located in the dist
folder. These are built using the files in the src
folder, so you can just point your modules to the Select2 source and load in jquery.select2
or select2/core
when you want to use Select2. The files located in the dist
folder are also AMD-compatible, so you can point to that file if you want to load in all of the default Select2 modules.
<select>
tag?Select2 was designed to be a replacement for the standard <select>
boxes that are displayed by the browser, so by default it supports all options and operations that are available in a standard select box, but with added flexibility. There is no special configuration required to make Select2 work with a <select>
tag.
A standard <select>
box can display nested options by wrapping them with in an <optgroup>
tag.
Only a single level of nesting is allowed per the HTML specification. If you nest an <optgroup>
within another <optgroup>
, Select2 will not be able to detect the extra level of nesting and errors may be triggered as a result.
<optgroup>
tags be made selectable?<option>
and <optgroup>
tags serialized into data objects?Select2 will convert the <option>
tag into a data object based on the following rules.
And <optgroup>
tags will be converted into data objects using the following rules
While Select2 is designed to be used with a <select>
tag the data that is used to search through and display the results can be loaded from a JavaScript array using the data
option. This option should be passed in during the initialization of Select2.
The id
and text
properties are required on each object, and these are the properties that Select2 uses for the internal data objects. Any additional paramters passed in with data objects will be included on the data objects that Select2 exposes.
id
properties have to be strings?Because the value
attributes on a <select>
tag must be strings, the id
property on the data objects must also be strings. Select2 will attempt to convert anything that is not a string to a string, which will work for most situations, but it is recommended to force all of your ids to strings ahead of time.
0
!Nested results should be specified using the children
property on the data objects that are passed in. This children
property should be an array of data objects that are grouped under this option, and the label for the group should be specified as the text
property on the root data object.
<option>
tags being created?id
for their unique identifiers, what can I do?Select2 requires that the id
property is used to uniquely identify the options that are displayed in the results list. If you use a property other than id
(like pk
) to uniquely identify an option, you need to map your old property to id
before passing it to Select2.
If you cannot do this on your server or you are in a situation where the identifier cannot be changed, you can do this in JavaScript before passing it to Select2.
text
for the text that needs to be displayedJust like with the id
property, Select2 requires that the text that should be displayed for an option is stored in the text
property. You can map this property from any existing property using the following JavaScript.
Select2 supports connecting to a remote data source using the ajax
option.
The data returned by the data provider or by processResults
should be a JSON object containing an array of objects keyed by the results
key.
Each object should contain, at a minimum, an id
and a text
property. The text property will be displayed by default, unless you are using templateResult
and templateSelection
to customize the way options and selections are rendered.
Results for grouped options
When options are to be generated in <optgroup>
sections, options should be nested under the children
attribute of each group object:
You can use the ajax.processResults
option to modify the data returned from the server before passing it to Select2. The data structure returned by processResults
should match the format as specified above.
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 parameters, by overriding the ajax.data
option.
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.
As Select2 uses the HTML <SELECT>
element to store the selection result, the selection data are represented by <OPTION>
elements and can be accessed in the plain jQuery/DOM manner:
It is possible to extend the <OPTION>
elements representing selection with the HTML data-* attributes containing arbitrary data from the source data objects:
In addition, properties/values from source data objects can ba accessed from within an event handler:
Yes, Select2 supports making multiple selections through the use of the multiple
option that can be passed in when initializing Select2.
multiple
attribute be used on my <select>
element?Yes, Select2 will automatically map the value of the multiple
attribute to the multiple
option during initialization.
Select2 supports displaying a placeholder by default using the placeholder
option. This can be either a data object matching the placeholder option, or a string to display as the placeholder if you are using a blank placeholder option.
This usually means that you do not have a blank <option></option>
as the first option in your <select>
.
Note that this does not apply to multiple selects, as the browser does not select the first option by default when multiple selections can be made.
Yes, Select2 supports placeholders for all configurations. You will still need to add in the placeholder option if you are using a single select.
The placeholder
option allows you to pass in a data object instead of just a string if you need more flexibility. The id
of the data object should match the value
of the placeholder option.
When using Select2 when only a single selection can be made, the placeholder option will be passed through the standard templating methods, including the templateSelection
option, so you are able to change how it is displayed.
You can allow people to clear their current selections with the allowClear
option when initializing Select2. Setting this option to true
will enable an "x" icon that will reset the selection to the placeholder.
Yes, by setting the value of the control to null
:
When a selection is made by the user Select2 will display the text of the option by default, just like how it is displayed in a standard select box. You can override the display of the selection by setting the templateSelection
option to a JavaScript function.
If you want to use HTML in your selection template, you will need to return a jQuery object. Otherwise, Select2 will assume that your template only returns text and will escape it.
Select2 will trigger a few different events when different actions are taken using the component, allowing you to add custom hooks and perform actions.changeTriggered whenever an option is selected or removed.select2:closeTriggered whenever the dropdown is closed.select2:closingTriggered before the dropdown is closed. This event can be prevented.select2:openTriggered whenever the dropdown is opened.select2:openingTriggered before the dropdown is opened. This event can be prevented.select2:selectTriggered whenever a result is selected.select2:selectingTriggered before a result is selected. This event can be prevented.select2:unselectTriggered whenever a selection is removed.select2:unselectingTriggered before a selection is removed. This event can be prevented.
Select2 will listen for the change
event on the <select>
that it is attached to. If you make any external changes that need to be reflected in Select2 (such as changing the value), you should trigger this event.
change
to notify Select2 of changes?It's common for other components to be listening to the change
event, or for custom event handlers to be attached that may have side effects. Select2 does not have a custom event (like select2:update
) that can be triggered other than change
. You can rely on jQuery's event namespacing to limit the scope to Select2 though by triggering the change.select2
event.
Select2 has an internal event system that works independently of the DOM event system. This internal event system is only accessible from plugins and adapters that are connected to Select2.
initSelection
query
<input type="text" />
The key that is set should take the same format as keys set using which means that two dashes (--
) will be replaced by a level of nesting, and a single dash (-
) will convert it to a camelCase string.
You can also define nested configurations, which are typically needed for options such as AJAX. Each level of nesting should be separated by two dashes (--
) instead of one. Due to , nested options using data-*
attributes .
The value of the option is subject to jQuery's for HTML5 data attributes.
Select2 should work with most AMD- or CommonJS-compliant module loaders, including and . Select2 ships with a modified version of the that supports both CommonJS and AMD environments.
Due to of the r.js build tool, Select2 was sometimes placed before jQuery in then compiled build file. Because of this, Select2 will trigger an error because it won't be able to find or load jQuery.
No. This is a limitation of the HTML specification and is not a limitation that Select2 can overcome. You can emulate grouping by using an <option>
instead of an <optgroup>
and , but this is not recommended as it is not fully accessible.
See .
Because Select2 falls back to an <optgroup>
when creating nested options, only is supported. Any additional levels of nesting is not guaranteed to be displayed properly across all browsers and devices.
The data
option is a shortcut that Select2 provides which allows you to load options into your select
from a data array.This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
You can refer to the following Stack Overflow answer if you want to set the initial value for AJAX requests:
The response object may also contain , if you would like to use the "infinite scroll" feature. See for more information. A complete example is as follows:
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 .
There are few ways to programmatically access the selection data. Calling select2('data')
will return the JavaScript array of an objects representing the current selection. Each object will have properties/values which was in the source data objects passed through processResults
and templateResult
functions (as in and ).
When multiple selections are allowed, the placeholder will be displayed using the placeholder
attribute on the search box. You can customize the display of this placeholder using CSS, as explained in the following Stack Overflow answer:
Select2 uses the native placeholder
attribute on input boxes for the multiple select, and that attribute is not supported in older versions of Internet Explorer. You need to include on your page, or use the full build, in order to add placeholder
attribute support to input boxes.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
See for a working example.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
Yes.This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
Use the dropdownParent
option, setting it to the modal.This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This answer to this question has not yet been written. You can by creating a pull request with an answer to this question.
This method was added to make upgrading easier from earlier versions of Select2.
During the , the matcher
function was changed to allow for more complex matching of nested objects.Keymatcher
ValueA function taking a search term
and the data object text
.AdapteroldMatcher
The provides a guide for how to use this in your own application. For those upgrading from older versions of Select2, you just need to wrap your old matcher
with this function to maintain compatibility.
This has been replaced by another option and is only available in the of Select2.
In the past, Select2 required an option called initSelection
that was defined whenever a custom data source was being used, allowing for the initial selection for the component to be determined. This has been replaced by the current
method on the .KeyinitSelection
ValueA function taking a callback
AdapterDataAdapter
DecoratorInitSelection
This has been replaced by another option and is only available in the of Select2.
In the past, Select2 supported an option called query
that allowed for a custom data source to be used. This option has been replaced by the query
method on the and takes a very similar set of parameters.Keyquery
ValueA function taking params
(including a callback
)AdapterDataAdapter
DecoratorQuery