announcements-4.0.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/ivaynberg/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 find more
  35. information on the modules that have been created to make upgrading easier
  36. by looking through <a href="release-notes.html">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. </section>
  87. <section id="migrating">
  88. <h1>Migrating from Select2 3.5</h1>
  89. <p>
  90. There are a few breaking changes that migrators should be aware of when
  91. they are coming from older versions of Select2.
  92. </p>
  93. <h2 id="hidden-input">No more hidden input tags</h2>
  94. <p>
  95. In past versions of Select2, an <code>&lt;input type="hidden" /&gt;</code>
  96. tag was recommended if you wanted to do anything advanced with Select2,
  97. such as work with remote data sources or allow users to add their own
  98. tags. This had the unfortunate side-effect of servers not receiving the
  99. data from Select2 as an array, like a standard <code>&lt;select&gt;</code>
  100. element does, but instead sending a string containing the comma-separated
  101. strings. The code base ended up being littered with special cases for the
  102. hidden input, and libraries using Select2 had to work around the
  103. differences it caused.
  104. </p>
  105. <p>
  106. In Select2 4.0, the <code>&lt;select&gt;</code> element supports all core
  107. options, and support for the old
  108. <code>&lt;input type="hidden" /&gt;</code> has been removed. This means
  109. that if you previously declared an AJAX field with some pre-selected
  110. options that looked like...
  111. </p>
  112. <pre class="prettyprint linenums">
  113. &lt;input type="hidden" name="select-boxes" value="1,2,4,6" /&gt;
  114. </pre>
  115. <p>
  116. Will need to be recreated as a <code>&lt;select&gt;</code> element with
  117. some <code>&lt;option&gt;</code> tags that have <code>value</code>
  118. attributes that match the old value.
  119. </p>
  120. <pre class="prettyprint linenums">
  121. &lt;select name="select-boxes" multiple="multiple"&gt;
  122. &lt;option value="1" selected="selected"&gt;Select2&lt;/option&gt;
  123. &lt;option value="2" selected="selected"&gt;Chosen&lt;/option&gt;
  124. &lt;option value="4" selected="selected"&gt;selectize.js&lt;/option&gt;
  125. &lt;option value="6" selected="selected"&gt;typeahead.js&lt;/option&gt;
  126. &lt;/select&gt;
  127. </pre>
  128. <p>
  129. The options that you create should have <code>selected="selected"</code>
  130. set, so Select2 and the browser knows that they should be selected. The
  131. <code>value</code> attribute of the option should also be set to the value
  132. that will be returned from the server for the result, so Select2 can
  133. highlight it as selected in the dropdown. The text within the option
  134. should also reflect the value that should be displayed by default for the
  135. option.
  136. </p>
  137. <h2 id="new-matcher">Advanced matching of searches</h2>
  138. <p>
  139. In past versions of Select2, when matching search terms to individual
  140. options, which limited the control that you had when displaying results,
  141. especially in cases where there was nested data. The <code>matcher</code>
  142. function was only given the individual option, even if it was a nested
  143. options, without any context.
  144. </p>
  145. <p>
  146. With the new matcher function, only the root-level options are matched and
  147. matchers are expected to limit the results of any children options that
  148. they contain. This allows developers to customize how options within
  149. groups can be displayed, and modify how the results are returned.
  150. </p>
  151. <p>
  152. A function has been created that allows old-style matcher functions to be
  153. converted to the new style. You can retrieve the function from the
  154. <code>select2/compat/matcher</code> module.
  155. </p>
  156. <h2 id="flexible-placeholders">More flexible placeholders</h2>
  157. <p>
  158. In the most recent versions of Select2, placeholders could only be
  159. applied to the first (typically the default) option in a
  160. <code>&lt;select&gt;</code> if it was blank. The
  161. <code>placeholderOption</code> option was added to Select2 to allow users
  162. using the <code>select</code> tag to select a different option, typically
  163. an automatically generated option with a different value.
  164. </p>
  165. <p>
  166. The <code>placeholder</code> option can now take an object as well as just
  167. a string. This replaces the need for the old
  168. <code>placeholderOption</code>, as now the <code>id</code> of the object
  169. can be set to the <code>value</code> attribute of the
  170. <code>&lt;option&gt;</code> tag.
  171. </p>
  172. <p>
  173. For a select that looks like the following, where the first option (with a
  174. value of <code>-1</code>) is the placeholder option...
  175. </p>
  176. <pre class="prettyprint linenums">
  177. &lt;select&gt;
  178. &lt;option value="-1" selected="selected"&gt;Select an option&lt;/option&gt;
  179. &lt;option value="1"&gt;Something else&lt;/option&gt;
  180. &lt;/select&gt;
  181. </pre>
  182. <p>
  183. You would have previously had to get the placeholder option through the
  184. <code>placeholderOption</code>, but now you can do it through the
  185. <code>placeholder</code> option by setting an <code>id</code>.
  186. </p>
  187. <pre class="prettyprint linenums">
  188. $("select").select2({
  189. placeholder: {
  190. id: "-1",
  191. placeholder: "Select an option"
  192. }
  193. })
  194. </pre>
  195. <p>
  196. And Select2 will automatically display the placeholder when the value of
  197. the select is <code>-1</code>, which it is by default. This does not break
  198. the old functionality of Select2 where the placeholder option was blank by
  199. default.
  200. </p>
  201. </section>
  202. </div>