announcements-4.0.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. ---
  2. layout: default
  3. title: Select2 4.0.0 Released
  4. slug: announcements-4.0
  5. ---
  6. <div class="container">
  7. <section id="pre-release">
  8. <h1>Pre-release notes</h1>
  9. <hr />
  10. <p class="lead">
  11. The 4.0 release is ready for early adopters interested in testing it out.
  12. You can use the development version, available on GitHub, by getting the
  13. source code available in the <code>select2-ng</code> branch. The source
  14. code can be
  15. <a href="https://github.com/select2/select2/archive/select2-ng.zip">
  16. downloaded as a <code>zip</code> archive
  17. </a> as well.
  18. </p>
  19. </section>
  20. <hr />
  21. <section id="release">
  22. <h1>Select2 4.0.0</h1>
  23. <p>
  24. The 4.0 release of Select2 is the result of three years of working on the
  25. code base and watching where it needs to go. At the core, it is a full
  26. rewrite that addresses many of the extensibility and usability problems
  27. that could not be addressed in previous versions.
  28. </p>
  29. <p>
  30. This release contains many breaking changes, but easy-upgrade pathes have
  31. been created as well as helper modules that will allow for backwards
  32. compatibility to be maintained with past versions of Select2. Upgrading
  33. <em>will</em> require you to read the release notes carefully, but the
  34. migration path should be relatively straightforward. You can view a list
  35. of the most common changes that you will need to make
  36. <a href="https://github.com/select2/select2/releases">in the release notes</a>.
  37. </p>
  38. <p>
  39. Below is an in-depth review of what is new in Select2, as well as some of
  40. the major changes that have been made.
  41. </p>
  42. </section>
  43. <section id="new">
  44. <h2>New features</h2>
  45. <p>
  46. The notable features of this new release include:
  47. </p>
  48. <ul>
  49. <li>
  50. A more flexible plugin framework that allows you to override Select2 to
  51. behave exactly how you want it to.
  52. </li>
  53. <li>
  54. Consistency with standard <code>&lt;select&gt;</code> elements for all
  55. data adapters, removing the need for hidden <code>&lt;input&gt;</code>
  56. elements.
  57. </li>
  58. <li>
  59. A new build system that uses AMD to keep everything organized.
  60. </li>
  61. <li>
  62. Less specific selectors allowing for Select2 to be styled to fit the
  63. rest of your application.
  64. </li>
  65. </ul>
  66. </section>
  67. <section id="plugins">
  68. <h2>Plugin system</h2>
  69. <p>
  70. Select2 now provides interfaces that allow for it to be easily extended,
  71. allowing for anyone to create a plugin that changes the way Select2 works.
  72. This is the result of Select2 being broken into four distinct sections,
  73. each of which can be extended and used together to create your unique
  74. Select2.
  75. </p>
  76. <p>
  77. The adapters implement a consistent interface that is documented in the
  78. <a href="options.html#adapters">options section for adapters</a>, allowing
  79. you to customize Select2 to do exactly what you are looking for. Select2
  80. is designed such that you can mix and match plugins, with most of the core
  81. options being built as decorators that wrap the standard adapters.
  82. </p>
  83. </section>
  84. <section id="amd-builds">
  85. <h2>AMD-based build system</h2>
  86. <p>
  87. Select2 now uses an
  88. <a href="https://en.wikipedia.org/wiki/Asynchronous_module_definition">AMD-based build system</a>,
  89. allowing for builds that only require the parts of Select2 that you need.
  90. While a custom build system has not yet been created, Select2 is open
  91. source and will gladly accept a pull request for one.
  92. </p>
  93. <p>
  94. Select2 includes the minimal <a href="https://github.com/jrburke/almond">almond</a>
  95. AMD loader, but a custom <code>select2.amd.js</code> build is available
  96. if you already use an AMD loader. The code base (available in the
  97. <code>src</code> directory) also uses AMD, allowing you to include Select2
  98. in your own build system and generate your own builds alongside your
  99. existing infrastructure.
  100. </p>
  101. <p>
  102. The AMD methods used by Select2 are available as
  103. <code>jQuery.fn.select2.amd.define()/require()</code>, allowing you to use the
  104. included almond loader. These methods are primarily used by the
  105. translations, but they are the recommended way to access custom modules
  106. that Select2 provides.
  107. </p>
  108. </section>
  109. <section id="migrating">
  110. <h1>Migrating from Select2 3.5</h1>
  111. <p>
  112. There are a few breaking changes that migrators should be aware of when
  113. they are coming from older versions of Select2.
  114. </p>
  115. <h2 id="hidden-input">No more hidden input tags</h2>
  116. <p>
  117. In past versions of Select2, an <code>&lt;input type="hidden" /&gt;</code>
  118. tag was recommended if you wanted to do anything advanced with Select2,
  119. such as work with remote data sources or allow users to add their own
  120. tags. This had the unfortunate side-effect of servers not receiving the
  121. data from Select2 as an array, like a standard <code>&lt;select&gt;</code>
  122. element does, but instead sending a string containing the comma-separated
  123. strings. The code base ended up being littered with special cases for the
  124. hidden input, and libraries using Select2 had to work around the
  125. differences it caused.
  126. </p>
  127. <p>
  128. In Select2 4.0, the <code>&lt;select&gt;</code> element supports all core
  129. options, and support for the old
  130. <code>&lt;input type="hidden" /&gt;</code> has been removed. This means
  131. that if you previously declared an AJAX field with some pre-selected
  132. options that looked like...
  133. </p>
  134. <pre class="prettyprint linenums">
  135. &lt;input type="hidden" name="select-boxes" value="1,2,4,6" /&gt;
  136. </pre>
  137. <p>
  138. Will need to be recreated as a <code>&lt;select&gt;</code> element with
  139. some <code>&lt;option&gt;</code> tags that have <code>value</code>
  140. attributes that match the old value.
  141. </p>
  142. <pre class="prettyprint linenums">
  143. &lt;select name="select-boxes" multiple="multiple"&gt;
  144. &lt;option value="1" selected="selected"&gt;Select2&lt;/option&gt;
  145. &lt;option value="2" selected="selected"&gt;Chosen&lt;/option&gt;
  146. &lt;option value="4" selected="selected"&gt;selectize.js&lt;/option&gt;
  147. &lt;option value="6" selected="selected"&gt;typeahead.js&lt;/option&gt;
  148. &lt;/select&gt;
  149. </pre>
  150. <p>
  151. The options that you create should have <code>selected="selected"</code>
  152. set so Select2 and the browser knows that they should be selected. The
  153. <code>value</code> attribute of the option should also be set to the value
  154. that will be returned from the server for the result, so Select2 can
  155. highlight it as selected in the dropdown. The text within the option
  156. should also reflect the value that should be displayed by default for the
  157. option.
  158. </p>
  159. <h2 id="new-matcher">Advanced matching of searches</h2>
  160. <p>
  161. In past versions of Select2, when matching search terms to individual
  162. options, which limited the control that you had when displaying results,
  163. especially in cases where there was nested data. The <code>matcher</code>
  164. function was only given the individual option, even if it was a nested
  165. options, without any context.
  166. </p>
  167. <p>
  168. With the new matcher function, only the root-level options are matched and
  169. matchers are expected to limit the results of any children options that
  170. they contain. This allows developers to customize how options within
  171. groups can be displayed, and modify how the results are returned.
  172. </p>
  173. <p>
  174. A function has been created that allows old-style matcher functions to be
  175. converted to the new style. You can retrieve the function from the
  176. <code>select2/compat/matcher</code> module, which should just wrap the old
  177. matcher function.
  178. </p>
  179. <h2 id="flexible-placeholders">More flexible placeholders</h2>
  180. <p>
  181. In the most recent versions of Select2, placeholders could only be
  182. applied to the first (typically the default) option in a
  183. <code>&lt;select&gt;</code> if it was blank. The
  184. <code>placeholderOption</code> option was added to Select2 to allow users
  185. using the <code>select</code> tag to select a different option, typically
  186. an automatically generated option with a different value.
  187. </p>
  188. <p>
  189. The <code>placeholder</code> option can now take an object as well as just
  190. a string. This replaces the need for the old
  191. <code>placeholderOption</code>, as now the <code>id</code> of the object
  192. can be set to the <code>value</code> attribute of the
  193. <code>&lt;option&gt;</code> tag.
  194. </p>
  195. <p>
  196. For a select that looks like the following, where the first option (with a
  197. value of <code>-1</code>) is the placeholder option...
  198. </p>
  199. <pre class="prettyprint linenums">
  200. &lt;select&gt;
  201. &lt;option value="-1" selected="selected"&gt;Select an option&lt;/option&gt;
  202. &lt;option value="1"&gt;Something else&lt;/option&gt;
  203. &lt;/select&gt;
  204. </pre>
  205. <p>
  206. You would have previously had to get the placeholder option through the
  207. <code>placeholderOption</code>, but now you can do it through the
  208. <code>placeholder</code> option by setting an <code>id</code>.
  209. </p>
  210. <pre class="prettyprint linenums">
  211. $("select").select2({
  212. placeholder: {
  213. id: "-1",
  214. placeholder: "Select an option"
  215. }
  216. })
  217. </pre>
  218. <p>
  219. And Select2 will automatically display the placeholder when the value of
  220. the select is <code>-1</code>, which it is by default. This does not break
  221. the old functionality of Select2 where the placeholder option was blank by
  222. default.
  223. </p>
  224. <h2 id="value-ordering">Display reflects the actual order of the values</h2>
  225. <p>
  226. In past versions of Select2, choices were displayed in the order that
  227. they were selected. In cases where Select2 was used on a
  228. <code>&lt;select&gt;</code> element, the order that the server recieved
  229. the selections did not always match the order that the choices were
  230. displayed, resulting in confusion in situations where the order is
  231. important.
  232. </p>
  233. <p>
  234. Select2 will now order selected choices in the same order that will be
  235. sent to the server.
  236. </p>
  237. <h2 id="changed-options">Changed method and option names</h2>
  238. <p>
  239. When designing the future option set for Select2 4.0, special care was
  240. taken to ensure that the most commonly used options were brought over.
  241. For the most part, the commonly used options of Select2 can still be
  242. referenced under their previous names, but there were some changes which
  243. have been noted.
  244. </p>
  245. <h3 id="changed-templating">Renamed templating options</h3>
  246. <p>
  247. Select2 previously provided multiple options for formatting the results
  248. list and selected options, commonly referred to as "formatters", using the
  249. <code>formatSelection</code> and <code>formatResult</code> options. As the
  250. "formatters" were also used for things such as localization,
  251. <a href="#changed-translations">which has also changed</a>, they have been
  252. renamed to <code>templateSelection</code> and <code>templateResult</code>
  253. and their signatures have changed as well.
  254. </p>
  255. <p>
  256. You should refer to the updated
  257. <a href="options.html#templates">documentation on templates</a> when
  258. migrating from previous versions of Select2.
  259. </p>
  260. <h3 id="changed-id">
  261. The <code>id</code> and <code>text</code> properties are strictly enforced
  262. </h3>
  263. <p>
  264. When working with array and AJAX data in the past, Select2 allowed a
  265. custom <code>id</code> function or attribute to be set in various places,
  266. ranging from the initializion of Select2 to when the remote data was being
  267. returned. This allowed Select2 to better integrate with existing data
  268. sources that did not necessarily use the <code>id</code> attribute to
  269. indicate the unique identifier for an object.
  270. </p>
  271. <p>
  272. Select2 no longer supports a custom <code>id</code> or <code>text</code>
  273. to be used, but provides integration points for converting incorrect data
  274. to the expected format.
  275. </p>
  276. <h4>
  277. When working with array data
  278. </h4>
  279. <p>
  280. Select2 previously supported defining array data as an object that matched
  281. the signature of an AJAX response. A <code>text</code> property could be
  282. specified that would map the given property to the <code>text</code>
  283. property on the individual objects. You can now do this when initializing
  284. Select2 by using the following jQuery code to map the old
  285. <code>text</code> and <code>id</code> properties to the new ones.
  286. </p>
  287. <pre class="prettyprint linenums">
  288. var data = $.map([
  289. {
  290. pk: 1,
  291. word: 'one'
  292. },
  293. {
  294. pk: 2,
  295. word: 'two'
  296. }
  297. ], function (obj) {
  298. obj.id = obj.id || obj.pk;
  299. obj.text = obj.text || obj.word;
  300. return obj;
  301. });
  302. </pre>
  303. <p>
  304. This will result in an array of data objects that have the <code>id</code>
  305. properties that match the existing <code>pk</code> properties and
  306. <code>text</code> properties that match the existing <code>word</code>
  307. properties.
  308. </p>
  309. <h4>
  310. When working with remote data
  311. </h4>
  312. <p>
  313. The same code that was given above can be used in the
  314. <code>processResults</code> method of an AJAX call to map properties there
  315. as well.
  316. </p>
  317. <h3 id="changed-translations">Renamed translation options</h3>
  318. <p>
  319. In previous versions of Select2, the default messages provided to users
  320. could be localized to fit the language of the website that it was being
  321. used on. Select2 only comes with the English language by default, but
  322. provides
  323. <a href="options.html#language">community-contributed translations</a> for
  324. many common languages. Many of the formatters have been moved to the
  325. <code>language</code> option and the signatures of the formatters have
  326. been changed to handle future additions.
  327. </p>
  328. <h3 id="changed-data">
  329. Declaring options using <code>data-*</code> attributes
  330. </h3>
  331. <p>
  332. In the past, Select2 has only supported declaring a subset of options
  333. using <code>data-*</code> attributes. Select2 now supports declaring all
  334. options using the attributes, using
  335. <a href="options.html#data-attributes">the format specified in the documentation</a>.
  336. </p>
  337. <p>
  338. You could previously declare the URL that was used for AJAX requests using
  339. the <code>data-ajax-url</code> attribute. While Select2 still allows for
  340. this, the new attribute that should be used is the
  341. <code>data-ajax--url</code> attribute. Support for the old attribute will
  342. be removed in Select2 4.1.
  343. </p>
  344. <p>
  345. Although it was not documented, a list of possible tags could also be
  346. provided using the <code>data-select2-tags</code> attribute and passing in
  347. a JSON-formatted array of objects for tags. As the method for specifying
  348. tags has changed in 4.0, you should now provide the array of objects using
  349. the <code>data-data</code> attribute, which maps to
  350. <a href="options.html#data">the array data</a> option. You should also
  351. enable tags by setting <code>data-tags="true"</code> on the object, to
  352. maintain the ability for users to create their own options as well.
  353. </p>
  354. <p>
  355. If you previously declared the list of tags as...
  356. </p>
  357. <pre class="prettyprint linenums">
  358. &lt;select data-select2-tags="[{id: '1', text: 'One', id: '2', text: 'Two'}]"&gt;&lt;/select&gt;
  359. </pre>
  360. <p>
  361. ...then you should now delare it as...
  362. </p>
  363. <pre class="prettyprint linenums">
  364. &lt;select data-data="[{id: '1', text: 'One', id: '2', text: 'Two'}]" data-tags="true"&gt;&lt;/select&gt;
  365. </pre>
  366. <h2 id="removed-methods">Deprecated and removed methods</h2>
  367. <p>
  368. As Select2 now uses a <code>&lt;select&gt;</code> element for all data
  369. sources, a few methods that were available by calling
  370. <code>.select2()</code> are no longer required.
  371. </p>
  372. <h3>.select2("val")</h3>
  373. <p>
  374. The <code>"val"</code> method has been deprecated and will be removed in
  375. Select2 4.1. The deprecated method no longer includes the
  376. <code>triggerChange</code> parameter.
  377. </p>
  378. <p>
  379. You should directly call <code>.val</code> on the underlying
  380. <code>&lt;select&gt;</code> element instead. If you needed the second
  381. parameter (<code>triggerChange</code>), you should also call
  382. <code>.trigger("change")</code> on the element.
  383. </p>
  384. <pre class="prettyprint linenums">
  385. $("select").val("1"); // instead of $("select").select2("val", "1");
  386. </pre>
  387. <h3>.select2("enable")</h3>
  388. <p>
  389. Select2 will respect the <code>disabled</code> property of the underlying
  390. select element. In order to enable or disable Select2, you should call
  391. <code>.prop('disabled', true/false)</code> on the
  392. <code>&lt;select&gt;</code> element. Support for the old methods will be
  393. completely removed in Select2 4.1.
  394. </p>
  395. <pre class="prettyprint linenums">
  396. $("select").prop("disabled", true); // instead of $("select").enable(false);
  397. </pre>
  398. </section>
  399. </div>
  400. <script type="text/javascript">
  401. prettyPrint();
  402. </script>