announcements-4.0.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. ---
  2. layout: default
  3. title: Select2 4.0.0 Released
  4. slug: announcements-4.0
  5. ---
  6. <div class="container">
  7. <section id="release">
  8. <h1>Select2 4.0.0</h1>
  9. <p>
  10. The 4.0 release of Select2 is the result of three years of working on the
  11. code base and watching where it needs to go. At the core, it is a full
  12. rewrite that addresses many of the extensibility and usability problems
  13. that could not be addressed in previous versions.
  14. </p>
  15. <p>
  16. This release contains many breaking changes, but easy-upgrade paths have
  17. been created as well as helper modules that will allow for backwards
  18. compatibility to be maintained with past versions of Select2. Upgrading
  19. <em>will</em> require you to read the release notes carefully, but the
  20. migration path should be relatively straightforward. You can view a list
  21. of the most common changes that you will need to make
  22. <a href="https://github.com/select2/select2/releases">in the release notes</a>.
  23. </p>
  24. <p>
  25. Below is an in-depth review of what is new in Select2, as well as some of
  26. the major changes that have been made.
  27. </p>
  28. </section>
  29. <section id="new">
  30. <h2>New features</h2>
  31. <p>
  32. The notable features of this new release include:
  33. </p>
  34. <ul>
  35. <li>
  36. A more flexible plugin framework that allows you to override Select2 to
  37. behave exactly how you want it to.
  38. </li>
  39. <li>
  40. Consistency with standard <code>&lt;select&gt;</code> elements for all
  41. data adapters, removing the need for hidden <code>&lt;input&gt;</code>
  42. elements.
  43. </li>
  44. <li>
  45. A new build system that uses AMD to keep everything organized.
  46. </li>
  47. <li>
  48. Less specific selectors allowing for Select2 to be styled to fit the
  49. rest of your application.
  50. </li>
  51. </ul>
  52. </section>
  53. <section id="plugins">
  54. <h2>Plugin system</h2>
  55. <p>
  56. Select2 now provides interfaces that allow for it to be easily extended,
  57. allowing for anyone to create a plugin that changes the way Select2 works.
  58. This is the result of Select2 being broken into four distinct sections,
  59. each of which can be extended and used together to create your unique
  60. Select2.
  61. </p>
  62. <p>
  63. The adapters implement a consistent interface that is documented in the
  64. <a href="options.html#adapters">options section for adapters</a>, allowing
  65. you to customize Select2 to do exactly what you are looking for. Select2
  66. is designed such that you can mix and match plugins, with most of the core
  67. options being built as decorators that wrap the standard adapters.
  68. </p>
  69. </section>
  70. <section id="amd-builds">
  71. <h2>AMD-based build system</h2>
  72. <p>
  73. Select2 now uses an
  74. <a href="https://en.wikipedia.org/wiki/Asynchronous_module_definition">AMD-based build system</a>,
  75. allowing for builds that only require the parts of Select2 that you need.
  76. While a custom build system has not yet been created, Select2 is open
  77. source and will gladly accept a pull request for one.
  78. </p>
  79. <p>
  80. Select2 includes the minimal <a href="https://github.com/jrburke/almond">almond</a>
  81. AMD loader, but a custom <code>select2.amd.js</code> build is available
  82. if you already use an AMD loader. The code base (available in the
  83. <code>src</code> directory) also uses AMD, allowing you to include Select2
  84. in your own build system and generate your own builds alongside your
  85. existing infrastructure.
  86. </p>
  87. <p>
  88. The AMD methods used by Select2 are available as
  89. <code>jQuery.fn.select2.amd.define()/require()</code>, allowing you to use the
  90. included almond loader. These methods are primarily used by the
  91. translations, but they are the recommended way to access custom modules
  92. that Select2 provides.
  93. </p>
  94. </section>
  95. <section id="migrating">
  96. <h1>Migrating from Select2 3.5</h1>
  97. <p>
  98. There are a few breaking changes that migrators should be aware of when
  99. they are coming from older versions of Select2.
  100. </p>
  101. <p>
  102. If you use the full build of Select2 (<code>select2.full.js</code>), you
  103. will be automatically notified of the major breaking changes, and
  104. compatibility modules will be used in some cases to ensure that your code
  105. still behaves how you were expecting.
  106. </p>
  107. <h2 id="hidden-input">No more hidden input tags</h2>
  108. <p>
  109. In past versions of Select2, an <code>&lt;input type="hidden" /&gt;</code>
  110. tag was recommended if you wanted to do anything advanced with Select2,
  111. such as work with remote data sources or allow users to add their own
  112. tags. This had the unfortunate side-effect of servers not receiving the
  113. data from Select2 as an array, like a standard <code>&lt;select&gt;</code>
  114. element does, but instead sending a string containing the comma-separated
  115. strings. The code base ended up being littered with special cases for the
  116. hidden input, and libraries using Select2 had to work around the
  117. differences it caused.
  118. </p>
  119. <p>
  120. In Select2 4.0, the <code>&lt;select&gt;</code> element supports all core
  121. options, and support for the old
  122. <code>&lt;input type="hidden" /&gt;</code> has been deprecated. This means
  123. that if you previously declared an AJAX field with some pre-selected
  124. options that looked like...
  125. </p>
  126. <pre class="prettyprint linenums">
  127. &lt;input type="hidden" name="select-boxes" value="1,2,4,6" /&gt;
  128. </pre>
  129. <p>
  130. It will need to be recreated as a <code>&lt;select&gt;</code> element with
  131. some <code>&lt;option&gt;</code> tags that have <code>value</code>
  132. attributes that match the old value.
  133. </p>
  134. <pre class="prettyprint linenums">
  135. &lt;select name="select-boxes" multiple="multiple"&gt;
  136. &lt;option value="1" selected="selected"&gt;Select2&lt;/option&gt;
  137. &lt;option value="2" selected="selected"&gt;Chosen&lt;/option&gt;
  138. &lt;option value="4" selected="selected"&gt;selectize.js&lt;/option&gt;
  139. &lt;option value="6" selected="selected"&gt;typeahead.js&lt;/option&gt;
  140. &lt;/select&gt;
  141. </pre>
  142. <p>
  143. The options that you create should have <code>selected="selected"</code>
  144. set so Select2 and the browser knows that they should be selected. The
  145. <code>value</code> attribute of the option should also be set to the value
  146. that will be returned from the server for the result, so Select2 can
  147. highlight it as selected in the dropdown. The text within the option
  148. should also reflect the value that should be displayed by default for the
  149. option.
  150. </p>
  151. <h2 id="new-matcher">Advanced matching of searches</h2>
  152. <p>
  153. In past versions of Select2, when matching search terms to individual
  154. options, which limited the control that you had when displaying results,
  155. especially in cases where there was nested data. The <code>matcher</code>
  156. function was only given the individual option, even if it was a nested
  157. options, without any context.
  158. </p>
  159. <p>
  160. With the new matcher function, only the root-level options are matched and
  161. matchers are expected to limit the results of any children options that
  162. they contain. This allows developers to customize how options within
  163. groups can be displayed, and modify how the results are returned.
  164. </p>
  165. <p>
  166. A function has been created that allows old-style matcher functions to be
  167. converted to the new style. You can retrieve the function from the
  168. <code>select2/compat/matcher</code> module, which should just wrap the old
  169. matcher function.
  170. </p>
  171. <p>
  172. So if your old code used a matcher that only displayed options if they
  173. started with the term that was entered, it would look something like...
  174. </p>
  175. <pre class="prettyprint linenums">
  176. function matchStart (term, text) {
  177. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  178. return true;
  179. }
  180. return false;
  181. }
  182. $("select").select2({
  183. matcher: matchStart
  184. })
  185. </pre>
  186. <p>
  187. Then in Select2 4.0, you would need to wrap the <code>matchStart</code>
  188. method (or the name of the matcher you created) with a
  189. <code>oldMatcher</code> method that we have created.
  190. </p>
  191. <pre class="prettyprint linenums">
  192. function matchStart (term, text) {
  193. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  194. return true;
  195. }
  196. return false;
  197. }
  198. $.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  199. $("select").select2({
  200. matcher: oldMatcher(matchStart)
  201. })
  202. });
  203. </pre>
  204. <p>
  205. This will work for any matchers that only took in the search term and the
  206. text of the option as parameters. If your matcher relied on the third
  207. parameter containing the jQuery element representing the original
  208. <code>&lt;option&gt;</code> tag, then you may need to slightly change
  209. your matcher to expect the full JavaScript data object being passed in
  210. instead. You can still retrieve the jQuery element from the data object
  211. using the <code>data.element</code> property.
  212. </p>
  213. <h2 id="flexible-placeholders">More flexible placeholders</h2>
  214. <p>
  215. In the most recent versions of Select2, placeholders could only be
  216. applied to the first (typically the default) option in a
  217. <code>&lt;select&gt;</code> if it was blank. The
  218. <code>placeholderOption</code> option was added to Select2 to allow users
  219. using the <code>select</code> tag to select a different option, typically
  220. an automatically generated option with a different value.
  221. </p>
  222. <p>
  223. The <code>placeholder</code> option can now take an object as well as just
  224. a string. This replaces the need for the old
  225. <code>placeholderOption</code>, as now the <code>id</code> of the object
  226. can be set to the <code>value</code> attribute of the
  227. <code>&lt;option&gt;</code> tag.
  228. </p>
  229. <p>
  230. For a select that looks like the following, where the first option (with a
  231. value of <code>-1</code>) is the placeholder option...
  232. </p>
  233. <pre class="prettyprint linenums">
  234. &lt;select&gt;
  235. &lt;option value="-1" selected="selected"&gt;Select an option&lt;/option&gt;
  236. &lt;option value="1"&gt;Something else&lt;/option&gt;
  237. &lt;/select&gt;
  238. </pre>
  239. <p>
  240. You would have previously had to get the placeholder option through the
  241. <code>placeholderOption</code>, but now you can do it through the
  242. <code>placeholder</code> option by setting an <code>id</code>.
  243. </p>
  244. <pre class="prettyprint linenums">
  245. $("select").select2({
  246. placeholder: {
  247. id: "-1",
  248. placeholder: "Select an option"
  249. }
  250. })
  251. </pre>
  252. <p>
  253. And Select2 will automatically display the placeholder when the value of
  254. the select is <code>-1</code>, which it will be by default. This does not
  255. break the old functionality of Select2 where the placeholder option was
  256. blank by default.
  257. </p>
  258. <h2 id="value-ordering">Display reflects the actual order of the values</h2>
  259. <p>
  260. In past versions of Select2, choices were displayed in the order that
  261. they were selected. In cases where Select2 was used on a
  262. <code>&lt;select&gt;</code> element, the order that the server received
  263. the selections did not always match the order that the choices were
  264. displayed, resulting in confusion in situations where the order is
  265. important.
  266. </p>
  267. <p>
  268. Select2 will now order selected choices in the same order that will be
  269. sent to the server.
  270. </p>
  271. <h2 id="changed-options">Changed method and option names</h2>
  272. <p>
  273. When designing the future option set for Select2 4.0, special care was
  274. taken to ensure that the most commonly used options were brought over.
  275. For the most part, the commonly used options of Select2 can still be
  276. referenced under their previous names, but there were some changes which
  277. have been noted.
  278. </p>
  279. <h3 id="removed-initselection">
  280. Removed the requirement of <code>initSelection</code>
  281. </h3>
  282. <p>
  283. In the past, whenever you wanted to use a custom data adapter, such as
  284. AJAX or tagging, you needed to help Select2 out in determining the initial
  285. values that were selected. This was typically done through the
  286. <code>initSelection</code> option, which took the underlying data of the
  287. input and converted it into data objects that Select2 could use.
  288. </p>
  289. <p>
  290. This is now handled by
  291. <a href="options.html#dataAdapter">the data adapter</a> in the
  292. <code>current</code> method, which allows Select2 to convert the currently
  293. selected values into data objects that can be displayed. The default
  294. implementation converts the text and value of <code>option</code> elements
  295. into data objects, and is probably suitable for most cases. An example of
  296. the old <code>initSelection</code> option is included below, which
  297. converts the value of the selected options into a data object with both
  298. the <code>id</code> and <code>text</code> matching the selected value.
  299. </p>
  300. <pre class="prettyprint linenums">
  301. initSelection : function (element, callback) {
  302. var data = [];
  303. $(element.val()).each(function () {
  304. data.push({id: this, text: this});
  305. });
  306. callback(data);
  307. }
  308. </pre>
  309. <p>
  310. When using the new <code>current</code> method of the custom data adapter,
  311. <strong>this method is called any time Select2 needs a list</strong> of
  312. the currently selected options. This is different from the old
  313. <code>initSelection</code> in that it was only called once, so it could
  314. suffer from being relatively slow to process the data (such as from a
  315. remote data source).
  316. </p>
  317. <pre class="prettyprint linenums">
  318. $.fn.select2.amd.require(
  319. ['select2/data/array', 'select2/utils'],
  320. function (ArrayData, Utils) {
  321. function CustomData ($element, options) {
  322. CustomData.__super__.constructor.call(this, $element, options);
  323. }
  324. Utils.Extend(CustomData, ArrayData);
  325. CustomData.prototype.current = function (callback) {
  326. var data = [];
  327. var currentVal = this.$element.val();
  328. if (!this.$element.prop('multiple')) {
  329. currentVal = [currentVal];
  330. }
  331. for (var v = 0; v < currentVal.length; v++) {
  332. data.push({
  333. id: currentVal[v],
  334. text: currentVal[v]
  335. });
  336. }
  337. callback(data);
  338. };
  339. $("#select").select2({
  340. dataAdapter: CustomData
  341. });
  342. }
  343. </pre>
  344. <p>
  345. The new <code>current</code> method of the data adapter works in a similar
  346. way to the old <code>initSelection</code> method, with three notable
  347. differences. The first, and most important, is that <strong>it is called
  348. whenever the current selections are needed</strong> to ensure that Select2
  349. is always displaying the most accurate and up to date data. No matter
  350. what type of element Select2 is attached to, whether it supports a
  351. single or multiple selections, the data passed to the callback
  352. <strong>must be an array, even if it contains one selection</strong>.
  353. The last is that there is only one parameter, the callback to be
  354. executed with the latest data, and the current element that Select2 is
  355. attached to is available on the class itself as
  356. <code>this.$element</code>.
  357. </p>
  358. <p>
  359. If you only need to load in the initial options once, and otherwise will
  360. be letting Select2 handle the state of the selections, you don't need to
  361. use a custom data adapter. You can just create the
  362. <code>&lt;option&gt;</code> tags on your own, and Select2 will pick up
  363. the changes.
  364. </p>
  365. <pre class="prettyprint linenums">
  366. var $element = $('select').select2(); // the select element you are working with
  367. var $request = $.ajax({
  368. url: '/my/remote/source' // wherever your data is actually coming from
  369. });
  370. $request.then(function (data) {
  371. // This assumes that the data comes back as an array of data objects
  372. // The idea is that you are using the same callback as the old `initSelection`
  373. for (var d = 0; d < data.length; d++) {
  374. var item = data[d];
  375. // Create the DOM option that is pre-selected by default
  376. var option = new Option(data.text, data.id, true, true);
  377. // Append it to the select
  378. $element.append(option);
  379. }
  380. // Update the selected options that are displayed
  381. $element.trigger('change');
  382. });
  383. </pre>
  384. <h3 id="query-to-data-adapter">
  385. Custom data adapters instead of <code>query</code>
  386. </h3>
  387. <p>
  388. <a href="http://select2.github.io/select2/#data">In the past</a>, any time
  389. you wanted to hook Select2 up to a different data source you would be
  390. required to implement custom <code>query</code> and
  391. <code>initSelection</code> methods. This allowed Select2 to determine the
  392. initial selection and the list of results to display, and it would handle
  393. everything else internally, which was fine more most people.
  394. </p>
  395. <p>
  396. The custom <code>query</code> and <code>initSelection</code> methods have
  397. been replaced by
  398. <a href="options.html#dataAdapter">custom data adapters</a> that handle
  399. how Select2 stores and retrieves the data that will be displayed to the
  400. user. An example of the old <code>query</code> option is provided below,
  401. which is
  402. <a href="http://select2.github.io/select2/#data">the same as the old example</a>,
  403. and it generates results that contain the search term repeated a certain
  404. number of times.
  405. </p>
  406. <pre class="prettyprint linenums">
  407. query: function (query) {
  408. var data = {results: []}, i, j, s;
  409. for (i = 1; i < 5; i++) {
  410. s = "";
  411. for (j = 0; j < i; j++) {s = s + query.term;}
  412. data.results.push({id: query.term + i, text: s});
  413. }
  414. query.callback(data);
  415. }
  416. </pre>
  417. <p>
  418. This has been replaced by custom data adapters which define a similarly
  419. named <code>query</code> method. The comparable data adapter is provided
  420. below as an example.
  421. </p>
  422. <pre class="prettyprint linenums">
  423. $.fn.select2.amd.require(
  424. ['select2/data/array', 'select2/utils'],
  425. function (ArrayData, Utils) {
  426. function CustomData ($element, options) {
  427. CustomData.__super__.constructor.call(this, $element, options);
  428. }
  429. Utils.Extend(CustomData, ArrayData);
  430. CustomData.prototype.query = function (params, callback) {
  431. var data = {
  432. results: []
  433. };
  434. for (var i = 1; i < 5; i++) {
  435. var s = "";
  436. for (var j = 0; j < i; j++) {
  437. s = s + params.term;
  438. }
  439. data.results.push({
  440. id: params.term + i,
  441. text: s
  442. });
  443. }
  444. callback(data);
  445. };
  446. $("#select").select2({
  447. dataAdapter: CustomData
  448. });
  449. }
  450. </pre>
  451. <p>
  452. The new <code>query</code> method of the data adapter is very similar to
  453. the old <code>query</code> option that was passed into Select2 when
  454. initializing it. The old <code>query</code> argument is mostly the same as
  455. the new <code>params</code> that are passed in to query on, and the
  456. callback that should be used to return the results is now passed in as the
  457. second parameter.
  458. </p>
  459. <h3 id="changed-templating">Renamed templating options</h3>
  460. <p>
  461. Select2 previously provided multiple options for formatting the results
  462. list and selected options, commonly referred to as "formatters", using the
  463. <code>formatSelection</code> and <code>formatResult</code> options. As the
  464. "formatters" were also used for things such as localization,
  465. <a href="#changed-translations">which has also changed</a>, they have been
  466. renamed to <code>templateSelection</code> and <code>templateResult</code>
  467. and their signatures have changed as well.
  468. </p>
  469. <p>
  470. You should refer to the updated
  471. <a href="options.html#templating">documentation on templates</a> when
  472. migrating from previous versions of Select2.
  473. </p>
  474. <h3 id="changed-id">
  475. The <code>id</code> and <code>text</code> properties are strictly enforced
  476. </h3>
  477. <p>
  478. When working with array and AJAX data in the past, Select2 allowed a
  479. custom <code>id</code> function or attribute to be set in various places,
  480. ranging from the initialization of Select2 to when the remote data was
  481. being returned. This allowed Select2 to better integrate with existing
  482. data sources that did not necessarily use the <code>id</code> attribute to
  483. indicate the unique identifier for an object.
  484. </p>
  485. <p>
  486. Select2 no longer supports a custom <code>id</code> or <code>text</code>
  487. to be used, but provides integration points for converting incorrect data
  488. to the expected format.
  489. </p>
  490. <h4>
  491. When working with array data
  492. </h4>
  493. <p>
  494. Select2 previously supported defining array data as an object that matched
  495. the signature of an AJAX response. A <code>text</code> property could be
  496. specified that would map the given property to the <code>text</code>
  497. property on the individual objects. You can now do this when initializing
  498. Select2 by using the following jQuery code to map the old
  499. <code>text</code> and <code>id</code> properties to the new ones.
  500. </p>
  501. <pre class="prettyprint linenums">
  502. var data = $.map([
  503. {
  504. pk: 1,
  505. word: 'one'
  506. },
  507. {
  508. pk: 2,
  509. word: 'two'
  510. }
  511. ], function (obj) {
  512. obj.id = obj.id || obj.pk;
  513. obj.text = obj.text || obj.word;
  514. return obj;
  515. });
  516. </pre>
  517. <p>
  518. This will result in an array of data objects that have the <code>id</code>
  519. properties that match the existing <code>pk</code> properties and
  520. <code>text</code> properties that match the existing <code>word</code>
  521. properties.
  522. </p>
  523. <h4>
  524. When working with remote data
  525. </h4>
  526. <p>
  527. The same code that was given above can be used in the
  528. <code>processResults</code> method of an AJAX call to map properties there
  529. as well.
  530. </p>
  531. <h3 id="changed-translations">Renamed translation options</h3>
  532. <p>
  533. In previous versions of Select2, the default messages provided to users
  534. could be localized to fit the language of the website that it was being
  535. used on. Select2 only comes with the English language by default, but
  536. provides
  537. <a href="options.html#language">community-contributed translations</a> for
  538. many common languages. Many of the formatters have been moved to the
  539. <code>language</code> option and the signatures of the formatters have
  540. been changed to handle future additions.
  541. </p>
  542. <h3 id="changed-data">
  543. Declaring options using <code>data-*</code> attributes
  544. </h3>
  545. <p>
  546. In the past, Select2 has only supported declaring a subset of options
  547. using <code>data-*</code> attributes. Select2 now supports declaring all
  548. options using the attributes, using
  549. <a href="options.html#data-attributes">the format specified in the documentation</a>.
  550. </p>
  551. <p>
  552. You could previously declare the URL that was used for AJAX requests using
  553. the <code>data-ajax-url</code> attribute. While Select2 still allows for
  554. this, the new attribute that should be used is the
  555. <code>data-ajax--url</code> attribute. Support for the old attribute will
  556. be removed in Select2 4.1.
  557. </p>
  558. <p>
  559. Although it was not documented, a list of possible tags could also be
  560. provided using the <code>data-select2-tags</code> attribute and passing in
  561. a JSON-formatted array of objects for tags. As the method for specifying
  562. tags has changed in 4.0, you should now provide the array of objects using
  563. the <code>data-data</code> attribute, which maps to
  564. <a href="options.html#data">the array data</a> option. You should also
  565. enable tags by setting <code>data-tags="true"</code> on the object, to
  566. maintain the ability for users to create their own options as well.
  567. </p>
  568. <p>
  569. If you previously declared the list of tags as...
  570. </p>
  571. <pre class="prettyprint linenums">
  572. &lt;select data-select2-tags="[{id: '1', text: 'One', id: '2', text: 'Two'}]"&gt;&lt;/select&gt;
  573. </pre>
  574. <p>
  575. ...then you should now delare it as...
  576. </p>
  577. <pre class="prettyprint linenums">
  578. &lt;select data-data="[{id: '1', text: 'One', id: '2', text: 'Two'}]" data-tags="true"&gt;&lt;/select&gt;
  579. </pre>
  580. <h2 id="removed-methods">Deprecated and removed methods</h2>
  581. <p>
  582. As Select2 now uses a <code>&lt;select&gt;</code> element for all data
  583. sources, a few methods that were available by calling
  584. <code>.select2()</code> are no longer required.
  585. </p>
  586. <h3>.select2("val")</h3>
  587. <p>
  588. The <code>"val"</code> method has been deprecated and will be removed in
  589. Select2 4.1. The deprecated method no longer includes the
  590. <code>triggerChange</code> parameter.
  591. </p>
  592. <p>
  593. You should directly call <code>.val</code> on the underlying
  594. <code>&lt;select&gt;</code> element instead. If you needed the second
  595. parameter (<code>triggerChange</code>), you should also call
  596. <code>.trigger("change")</code> on the element.
  597. </p>
  598. <pre class="prettyprint linenums">
  599. $("select").val("1").trigger("change"); // instead of $("select").select2("val", "1");
  600. </pre>
  601. <h3>.select2("enable")</h3>
  602. <p>
  603. Select2 will respect the <code>disabled</code> property of the underlying
  604. select element. In order to enable or disable Select2, you should call
  605. <code>.prop('disabled', true/false)</code> on the
  606. <code>&lt;select&gt;</code> element. Support for the old methods will be
  607. completely removed in Select2 4.1.
  608. </p>
  609. <pre class="prettyprint linenums">
  610. $("select").prop("disabled", true); // instead of $("select").enable(false);
  611. </pre>
  612. </section>
  613. </div>
  614. <script type="text/javascript">
  615. prettyPrint();
  616. </script>