--- layout: default title: Select2 4.0.0 Released slug: announcements-4.0 ---
The 4.0 release is ready for early adopters interested in testing it out.
You can use the development version, available on GitHub, by getting the
source code available in the select2-ng
branch. The source
code can be
downloaded as a zip
archive
as well.
The 4.0 release of Select2 is the result of three years of working on the code base and watching where it needs to go. At the core, it is a full rewrite that addresses many of the extensibility and usability problems that could not be addressed in previous versions.
This release contains many breaking changes, but easy-upgrade pathes have been created as well as helper modules that will allow for backwards compatibility to be maintained with past versions of Select2. Upgrading will require you to read the release notes carefully, but the migration path should be relatively straightforward. You can find more information on the modules that have been created to make upgrading easier by looking through the release notes.
Below is an in-depth review of what is new in Select2, as well as some of the major changes that have been made.
The notable features of this new release include:
<select>
elements for all
data adapters, removing the need for hidden <input>
elements.
Select2 now provides interfaces that allow for it to be easily extended, allowing for anyone to create a plugin that changes the way Select2 works. This is the result of Select2 being broken into four distinct sections, each of which can be extended and used together to create your unique Select2.
The adapters implement a consistent interface that is documented in the options section for adapters, allowing you to customize Select2 to do exactly what you are looking for. Select2 is designed such that you can mix and match plugins, with most of the core options being built as decorators that wrap the standard adapters.
Select2 now uses an AMD-based build system, allowing for builds that only require the parts of Select2 that you need. While a custom build system has not yet been created, Select2 is open source and will gladly accept a pull request for one.
Select2 includes the minimal almond
AMD loader, but a custom select2.amd.js
build is available
if you already use an AMD loader. The code base (available in the
src
directory) also uses AMD, allowing you to include Select2
in your own build system and generate your own builds alongside your
existing infrastructure.
The AMD methods used by Select2 are available as
jQuery.fn.select2.amd.define()/require()
, allowing you to use the
included almond loader. These methods are primarily used by the
translations, but they are the recommended way to access custom modules
that Select2 provides.
There are a few breaking changes that migrators should be aware of when they are coming from older versions of Select2.
In past versions of Select2, an <input type="hidden" />
tag was recommended if you wanted to do anything advanced with Select2,
such as work with remote data sources or allow users to add their own
tags. This had the unfortunate side-effect of servers not receiving the
data from Select2 as an array, like a standard <select>
element does, but instead sending a string containing the comma-separated
strings. The code base ended up being littered with special cases for the
hidden input, and libraries using Select2 had to work around the
differences it caused.
In Select2 4.0, the <select>
element supports all core
options, and support for the old
<input type="hidden" />
has been removed. This means
that if you previously declared an AJAX field with some pre-selected
options that looked like...
<input type="hidden" name="select-boxes" value="1,2,4,6" />
Will need to be recreated as a <select>
element with
some <option>
tags that have value
attributes that match the old value.
<select name="select-boxes" multiple="multiple"> <option value="1" selected="selected">Select2</option> <option value="2" selected="selected">Chosen</option> <option value="4" selected="selected">selectize.js</option> <option value="6" selected="selected">typeahead.js</option> </select>
The options that you create should have selected="selected"
set, so Select2 and the browser knows that they should be selected. The
value
attribute of the option should also be set to the value
that will be returned from the server for the result, so Select2 can
highlight it as selected in the dropdown. The text within the option
should also reflect the value that should be displayed by default for the
option.
In past versions of Select2, when matching search terms to individual
options, which limited the control that you had when displaying results,
especially in cases where there was nested data. The matcher
function was only given the individual option, even if it was a nested
options, without any context.
With the new matcher function, only the root-level options are matched and matchers are expected to limit the results of any children options that they contain. This allows developers to customize how options within groups can be displayed, and modify how the results are returned.
A function has been created that allows old-style matcher functions to be
converted to the new style. You can retrieve the function from the
select2/compat/matcher
module, which should just wrap the old
matcher function.
In the most recent versions of Select2, placeholders could only be
applied to the first (typically the default) option in a
<select>
if it was blank. The
placeholderOption
option was added to Select2 to allow users
using the select
tag to select a different option, typically
an automatically generated option with a different value.
The placeholder
option can now take an object as well as just
a string. This replaces the need for the old
placeholderOption
, as now the id
of the object
can be set to the value
attribute of the
<option>
tag.
For a select that looks like the following, where the first option (with a
value of -1
) is the placeholder option...
<select> <option value="-1" selected="selected">Select an option</option> <option value="1">Something else</option> </select>
You would have previously had to get the placeholder option through the
placeholderOption
, but now you can do it through the
placeholder
option by setting an id
.
$("select").select2({ placeholder: { id: "-1", placeholder: "Select an option" } })
And Select2 will automatically display the placeholder when the value of
the select is -1
, which it is by default. This does not break
the old functionality of Select2 where the placeholder option was blank by
default.
In past versions of Select2, choices were displayed in the order that
they were selected. In cases where Select2 was used on a
<select>
element, the order that the server recieved
the selections did not always match the order that the choices were
displayed, resulting in confusion in situations where the order is
important.
Select2 will now order selected choices in the same order that will be sent to the server.
As Select2 now uses a <select>
element for all data
sources, a few methods that were available by calling
.select2()
are no longer required.
The val
method has been deprecated and will be removed in
Select2 4.1. The deprecated method no longer includes the
triggerChange
parameter.
You should directly call val
on the underlying
<select>
element instead. If you needed the second
parameter (triggerChange
), you should also call
.trigger("change")
on the element.
Select2 will respect the disabled
property of the underlying
select element. In order to enable or disable Select2, you should call
.prop('disabled', true/false)
on the
<select>
element. This method will be completely
removed in Select2 4.1.