options.html 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. ---
  2. layout: default
  3. title: Options - Select2
  4. slug: options
  5. ---
  6. <div class="container">
  7. <section id="core">
  8. <div class="page-header">
  9. <h1>Core options</h1>
  10. </div>
  11. <p>
  12. Select2 supports a small subset of options in every build that is
  13. generated. Each option typically has a decorator that is required that
  14. wraps an adapter, adding support for the option. This is only required
  15. when a custom adapter is being used, as Select2 will build the required
  16. adapters by default.
  17. </p>
  18. <p>
  19. Select2 will automatically apply decorators to any adapters which have not
  20. been manually overridden. The only time you need to decorate adapters is
  21. when you are using third-party adapters not provided by Select2, or you
  22. are using features not provided in the Select2 core. You can apply a
  23. decorator to an adapter using the
  24. <code title="select2/utils">Utils.Decorate</code> method provided with
  25. Select2.
  26. </p>
  27. <pre class="prettyprint linenums">
  28. $.fn.select2.amd.require(
  29. ["select2/utils", "select2/selection/single", "select2/selection/placeholder"],
  30. function (Utils, SingleSelection, Placeholder) {
  31. var CustomSelectionAdapter = Utils.Decorate(SingleSelection, Placeholder);
  32. });
  33. </pre>
  34. <p>
  35. All core options that use decorators or adapters will clearly state it
  36. in the "Decorator" or "Adapter" part of the documentation. Decorators are
  37. typically only compatible with a specific type of adapter, so make sure to
  38. note what adapter is given.
  39. </p>
  40. <h2 id="data-attributes">
  41. Declaring configuration in the <code>data-*</code> attributes
  42. </h2>
  43. <p>
  44. It is recommended that you declare your configuration options for Select2
  45. when initializing Select2. You can also define your configuration options
  46. by using the HTML5 <code>data-*</code> attributes, which will override
  47. any options set when initializing Select2 and any defaults.
  48. </p>
  49. <p>
  50. This means that if you declare your <code>&lt;select&gt;</code> tag as...
  51. </p>
  52. <pre class="prettyprint linenums">
  53. &lt;select data-tags="true" data-placeholder="Select an option"&gt;&lt;/select&gt;
  54. </pre>
  55. <p>
  56. Will be interpreted the same as initializing Select2 as...
  57. </p>
  58. <pre class="prettyprint linenums">
  59. $("select").select2({
  60. tags: "true",
  61. placeholder: "Select an option"
  62. });
  63. </pre>
  64. <p>
  65. You can also define nested configurations, which are typically needed for
  66. options such as AJAX. Each level of nesting should be separated by two
  67. dashes (<code>--</code>) instead of one.
  68. </p>
  69. <pre class="prettyprint linenums">
  70. &lt;select data-ajax--url="http://example.org/api/test" data-ajax--cache="true"&gt;&lt;/select&gt;
  71. </pre>
  72. <p>
  73. Which will be interpreted the same as initializing Select2 with...
  74. </p>
  75. <pre class="prettyprint linenums">
  76. $("select").select2({
  77. ajax: {
  78. url: "http://example.org/api/test",
  79. cache: "true"
  80. }
  81. });
  82. </pre>
  83. <p>
  84. The value of the option is subject to jQuery's
  85. <a href="https://api.jquery.com/data/#data-html5">parsing rules</a> for
  86. HTML5 data attributes.
  87. </p>
  88. <h2 id="amd">
  89. AMD compatibility
  90. </h2>
  91. <p>
  92. You can find more information on how to integrate Select2 with your
  93. existing AMD-based project by
  94. <a href="announcements-4.0.html#amd-builds">viewing the 4.0 release notes</a>.
  95. Select2 automatically loads some modules when the adapters are being
  96. automatically constructed, so those who are using Select2 with a custom
  97. AMD build using their own system will need to specify the pathes that are
  98. generated to the Select2 modules.
  99. </p>
  100. <div class="row">
  101. <div class="col-sm-6">
  102. <dl class="dl-horizontal">
  103. <dt>Key</dt>
  104. <dd>
  105. <code>amdBase</code>
  106. </dd>
  107. <dt>Default</dt>
  108. <dd>
  109. <code>select2/</code>
  110. </dd>
  111. </dl>
  112. </div>
  113. <div class="col-sm-6">
  114. <dl class="dl-horizontal">
  115. <dt>Key</dt>
  116. <dd>
  117. <code>amdLanguageBase</code>
  118. </dd>
  119. <dt>Default</dt>
  120. <dd>
  121. <code>select2/i18n/</code>
  122. </dd>
  123. </dl>
  124. </div>
  125. </div>
  126. <p>
  127. If you are using Select2 in your own code base with your own AMD build
  128. system, it is recommended to use the full source of Select2 instead of the
  129. custom AMD build (<code>select2.amd.js</code>). This will allow you to
  130. apply your own build steps, such as minification and concatenation,
  131. independent of the Select2 defaults. You will need to set the path to the
  132. source directories in the <code>amdBase</code> and
  133. <code>amdLanguage</code> base options, which can be set globally through
  134. <a href="#defaults">the default options</a>.
  135. </p>
  136. <h2>
  137. Display
  138. </h2>
  139. <p>
  140. Select2 provides options that allow you to directly affect how the
  141. container that holds the current selection is displayed.
  142. </p>
  143. <h3 id="placeholder">
  144. Placeholders
  145. </h3>
  146. <p>
  147. Select2 can display a placeholder for a single-value select that will
  148. replace an option, or be shown when no options are selected for
  149. multiple-value selects. You can find an example on the
  150. <a href="examples.html#placeholders">example page</a>.
  151. </p>
  152. <div class="row">
  153. <div class="col-sm-4">
  154. <dl class="dl-horizontal">
  155. <dt>Key</dt>
  156. <dd>
  157. <code>placeholder</code>
  158. </dd>
  159. <dt>Value</dt>
  160. <dd>string or object</dd>
  161. </dl>
  162. <hr />
  163. <dl class="dl-horizontal">
  164. <dt>Adapter</dt>
  165. <dd>
  166. <code title="select2/selection/base">SelectionAdapter</code>
  167. </dd>
  168. <dt>Decorator</dt>
  169. <dd>
  170. <code title="select2/selection/placeholder">Placeholder</code>
  171. and
  172. <code title="select2/dropdown/hidePlaceholder">HidePlaceholder</code>
  173. </dd>
  174. </dl>
  175. </div>
  176. <div class="col-sm-8">
  177. <div class="alert alert-warning">
  178. <strong>Heads up!</strong>
  179. Because browsers assume that the first <code>option</code> in
  180. single-value select boxes is selected, you should add an empty
  181. <code>&lt;option&gt;&lt;/option&gt;</code> tag that the placeholder
  182. should use or it may not work.
  183. </div>
  184. </div>
  185. </div>
  186. <p>
  187. If the <strong>value is a string</strong>, the placeholder will be
  188. displayed when a <strong>blank option</strong> is used as the placeholder.
  189. The <strong>value</strong> will be the message to show to users as the
  190. placeholders.
  191. </p>
  192. <pre class="prettyprint linenums">
  193. placeholder: "Select a repository",
  194. </pre>
  195. <p>
  196. If the <strong>value is an object</strong>, the object should be
  197. compatible with Select2's internal objects. The <code>id</code> should
  198. be the id to look for when determining if the placeholder should be
  199. displayed. The <code>text</code> should be the placeholder to display
  200. when that option is selected.
  201. </p>
  202. <pre class="prettyprint linenums">
  203. placeholder: {
  204. id: "-1",
  205. text: "Select a repository"
  206. }
  207. </pre>
  208. <div class="alert alert-info">
  209. You should <strong>pass in an object</strong> when you are using a
  210. framework that <strong>creates its own placeholder option</strong>. The
  211. <strong>id</strong> should be the same as the <code>value</code>
  212. attribute on the <code>option</code>.
  213. </div>
  214. <p id="allowClear">
  215. You can allow a selected option to be cleared back to the placeholder by
  216. enabling the <code>allowClear</code> option.
  217. </p>
  218. <div class="row">
  219. <div class="col-sm-6">
  220. <dl class="dl-horizontal">
  221. <dt>Key</dt>
  222. <dd><code>allowClear</code></dd>
  223. <dt>Value</dt>
  224. <dd>boolean</dd>
  225. </dl>
  226. </div>
  227. <div class="col-sm-6">
  228. <dl class="dl-horizontal">
  229. <dt>Adapter</dt>
  230. <dd>
  231. <code title="select2/selection/base">SelectionAdapter</code>
  232. </dd>
  233. <dt>Decorator</dt>
  234. <dd>
  235. <code title="select2/selection/allowClear">AllowClear</code>
  236. </dd>
  237. </dl>
  238. </div>
  239. </div>
  240. <p>
  241. This will display an "x" that the user can click to clear the current
  242. selection. It is designed to be used for cases where a single selection
  243. can be made.
  244. </p>
  245. <h3 id="multiple">
  246. Multiple selections
  247. </h3>
  248. <p>
  249. Select2 can display either a single selection or multiple selections.
  250. </p>
  251. <dl class="dl-horizontal">
  252. <dt>Key</dt>
  253. <dd><code>multiple</code></dd>
  254. <dt>Value</dt>
  255. <dd>boolean (<code>true</code> or <code>false</code>)</dd>
  256. </dl>
  257. <p>
  258. This option will determine what the <code>SelectAdapter</code> (used by
  259. default) should use to set the value of the underlying <code>select</code>
  260. element. It will also determine if the <code>MultipleSelection</code>
  261. adapter should be used.
  262. </p>
  263. <h3 id="width">
  264. Container width
  265. </h3>
  266. <p>
  267. Select2 will try to match the width of the original element as closely as
  268. possible. Sometimes this isn't perfect, which is what you can tell Select2
  269. how to determine the width.
  270. </p>
  271. <div class="row">
  272. <div class="col-sm-8">
  273. <table class="table table-striped table-bordered">
  274. <thead>
  275. <tr>
  276. <th>Value</th>
  277. <th>Description</th>
  278. </tr>
  279. </thead>
  280. <tbody>
  281. <tr>
  282. <td><code>"element"</code></td>
  283. <td>
  284. Uses javascript to calculate the width of the source element.
  285. </td>
  286. </tr>
  287. <tr>
  288. <td><code>"style"</code></td>
  289. <td>
  290. Copies the value of the width <code>style</code> attribute set on the source element.
  291. </td>
  292. </tr>
  293. <tr>
  294. <td><code>"resolve"</code></td>
  295. <td>
  296. Tries to use <code>style</code> to determine the width, falling back to <code>element</code>.
  297. </td>
  298. </tr>
  299. <tr>
  300. <td>Anything else</td>
  301. <td>
  302. The value of the <code>width</code> option is diretly set as the width of the container.
  303. </td>
  304. </tr>
  305. </tbody>
  306. </table>
  307. </div>
  308. <div class="col-sm-4">
  309. <dl class="dl-horizontal">
  310. <dt>Key</dt>
  311. <dd><code>width</code></dd>
  312. <dt>Value</dt>
  313. <dd>string</dd>
  314. </dl>
  315. </div>
  316. </div>
  317. <h3 id="language">
  318. Internationalization (Language support)
  319. </h3>
  320. <p>
  321. Messages will be displayed to users when necessary, such as when no
  322. search results were found or more characters need to be entered in order
  323. for a search to be made. These messages have been
  324. <a href="community.html#translations">translated into many languages</a>
  325. by contributors to Select2, but you can also provide your own
  326. translations.
  327. </p>
  328. <div class="row">
  329. <div class="col-sm-4">
  330. <dl class="dl-horizontal">
  331. <dt>Key</dt>
  332. <dd><code>language</code></dd>
  333. <dt>Value</dt>
  334. <dd>object or string</dd>
  335. </dl>
  336. <hr />
  337. <dl class="dl-horizontal">
  338. <dt>Module</dt>
  339. <dd>
  340. <code title="select2/translation">Translation</code>
  341. </dd>
  342. </dl>
  343. </div>
  344. <div class="col-sm-8">
  345. <p class="alert alert-warning">
  346. <strong>Heads up!</strong> When using translations provided by Select2,
  347. you must make sure to include the translation file in your page after
  348. Select2.
  349. </p>
  350. </div>
  351. </div>
  352. <p>
  353. When a string is passed in as the language, Select2 will try to resolve
  354. it into a language file. This allows you to specify your own language
  355. files, which must be defined as an AMD module. If the language file
  356. cannot be found, Select2 will assume it is a language code controlled by
  357. Select2, and it will try to load the translations for that language
  358. instead.
  359. </p>
  360. <p>
  361. You can include your own translations by providing an object similar to
  362. the one below.
  363. </p>
  364. <pre class="prettyprint">
  365. language: {
  366. // You can find all of the options in the language files provided in the
  367. // build. They all must be functions that return the string that should be
  368. // displayed.
  369. inputTooShort: function () {
  370. return "You must enter more characters...";
  371. }
  372. }
  373. </pre>
  374. <h2>
  375. Results
  376. </h2>
  377. <p>
  378. Select2 can work on many different data sets ranging from local options,
  379. the same way that a <code>&lt;select&gt;</code> typically works, from
  380. remote options where a server generates the results that users can select
  381. from.
  382. </p>
  383. <h3 id="data">
  384. Array
  385. </h3>
  386. <p>
  387. Select2 allows creating the results based on an array of data objects that
  388. is included when initializing Select2.
  389. </p>
  390. <div class="row">
  391. <div class="col-sm-6">
  392. <dl class="dl-horizontal">
  393. <dt>Key</dt>
  394. <dd><code>data</code></dd>
  395. <dt>Value</dt>
  396. <dd>array of objects</dd>
  397. </dl>
  398. </div>
  399. <div class="col-sm-6">
  400. <dl class="dl-horizontal">
  401. <dt>Adapter</dt>
  402. <dd>
  403. <code title="select2/data/array">ArrayAdapter</code>
  404. </dd>
  405. </dl>
  406. </div>
  407. </div>
  408. <p>
  409. The objects that the users can select from should be passed as an array
  410. with each object containing <code>id</code> and <code>text</code>
  411. properties.
  412. </p>
  413. <h3 id="ajax">
  414. AJAX
  415. </h3>
  416. <p>
  417. Select2 allows searching for results from remote data sources using AJAX
  418. requests.
  419. </p>
  420. <div class="row">
  421. <div class="col-sm-6">
  422. <dl class="dl-horizontal">
  423. <dt>Key</dt>
  424. <dd><code>ajax</code></dd>
  425. <dt>Value</dt>
  426. <dd>object</dd>
  427. </dl>
  428. </div>
  429. <div class="col-sm-6">
  430. <dl class="dl-horizontal">
  431. <dt>Adapter</dt>
  432. <dd>
  433. <code title="select2/data/ajax">AjaxAdapter</code>
  434. </dd>
  435. </dl>
  436. </div>
  437. </div>
  438. <p>
  439. All options passed to this option will be directly passed to the
  440. <code>$.ajax</code> function that executes AJAX requests. There are a few
  441. custom options that Select2 will intercept, allowing you to customize the
  442. request as it is being made.
  443. <pre class="prettyprint">
  444. ajax: {
  445. // The number of milliseconds to wait for the user to stop typing before
  446. // issuing the ajax request.
  447. delay: 250,
  448. // You can craft a custom url based on the parameters that are passed into the
  449. // request. This is useful if you are using a framework which has
  450. // JavaScript-based functions for generating the urls to make requests to.
  451. //
  452. // @param params The object containing the parameters used to generate the
  453. // request.
  454. // @returns The url that the request should be made to.
  455. url: function (params) {
  456. return UrlGenerator.Random();
  457. },
  458. // You can pass custom data into the request based on the parameters used to
  459. // make the request. For `GET` requests, the default method, these are the
  460. // query parameters that are appended to the url. For `POST` requests, this
  461. // is the form data that will be passed into the request. For other requests,
  462. // the data returned from here should be customized based on what jQuery and
  463. // your server are expecting.
  464. //
  465. // @param params The object containing the parameters used to generate the
  466. // request.
  467. // @returns Data to be directly passed into the request.
  468. data: function (params) {
  469. var queryParameters = {
  470. q: params.term
  471. }
  472. return queryParameters;
  473. },
  474. // You can modify the results that are returned from the server, allowing you
  475. // to make last-minute changes to the data, or find the correct part of the
  476. // response to pass to Select2. Keep in mind that results should be passed as
  477. // an array of objects.
  478. //
  479. // @param data The data as it is returned directly by jQuery.
  480. // @returns An object containing the results data as well as any required
  481. // metadata that is used by plugins. The object should contain an array of
  482. // data objects as the `results` key.
  483. processResults: function (data) {
  484. return {
  485. results: data
  486. };
  487. }
  488. }
  489. </pre>
  490. </p>
  491. <h3 id="tags">
  492. Tags
  493. </h3>
  494. <p>
  495. Users can create their own options based on the text that they have
  496. entered.
  497. </p>
  498. <div class="row">
  499. <div class="col-sm-6">
  500. <dl class="dl-horizontal">
  501. <dt>Key</dt>
  502. <dd><code>tags</code></dd>
  503. <dt>Value</dt>
  504. <dd>boolean / array of objects</dd>
  505. </dl>
  506. </div>
  507. <div class="col-sm-6">
  508. <dl class="dl-horizontal">
  509. <dt>Adapter</dt>
  510. <dd>
  511. <code title="select2/data/base">DataAdapter</code>
  512. </dd>
  513. <dt>Decorator</dt>
  514. <dd>
  515. <code title="select2/data/tags">Tags</code>
  516. </dd>
  517. </dl>
  518. </div>
  519. </div>
  520. <p>
  521. If the <code>tags</code> option is passed into Select2, if a user types
  522. anything into the search box which doesn't already exist, it will be
  523. displayed at the top and the user will be able to select it.
  524. </p>
  525. <p>
  526. <strong>For backwards compatibility</strong>, if an array of objects is
  527. passed in with the <code>tags</code> option, the options will be
  528. automatically created and the user will be able to select from them.
  529. This is the <strong>same as how <a href="#data">array data</a>
  530. works</strong>, and has similar limitations.
  531. </p>
  532. </section>
  533. <section id="dropdown">
  534. <div class="page-header">
  535. <h1>Dropdown</h1>
  536. </div>
  537. <p>
  538. Select2 allows you to change the way that the dropdown works, allowing you
  539. to do anything from attach it to a different location in the document or
  540. add a search box.
  541. </p>
  542. <h2 id="dropdownParent">
  543. Attached to body
  544. </h2>
  545. <p>
  546. By default, Select2 will attach the dropdown to the end of the body and
  547. will absolutely position it to appear below the selection container.
  548. </p>
  549. <div class="row">
  550. <div class="col-sm-4">
  551. <dl class="dl-horizontal">
  552. <dt>Key</dt>
  553. <dd><code>dropdownParent</code></dd>
  554. <dt>Value</dt>
  555. <dd>jQuery element or DOM node</dd>
  556. <hr />
  557. <dt>Adapter</dt>
  558. <dd>
  559. <code title="select2/dropdown">DropdownAdapter</code>
  560. </dd>
  561. <dt>Decorator</dt>
  562. <dd>
  563. <code title="select2/dropdown/attachBody">AttachBody</code>
  564. </dd>
  565. </dl>
  566. </div>
  567. <div class="col-sm-8">
  568. <div class="alert alert-warning">
  569. <strong>Heads up!</strong>
  570. This will cause DOM events to be raised outside of the standard
  571. Select2 DOM container. This can cause issues with
  572. third-party components such as modals.
  573. </div>
  574. </div>
  575. </div>
  576. <p>
  577. When the dropdown is attached to the body, you are not limited to just
  578. displaying the dropdown below the container. Select2 will display above
  579. the container if there is not enough space below the container, but there
  580. is enough space above it. You are also not limited to displaying the
  581. drodown within the parent container, which means Select2 will render
  582. correctly inside of modals and other small containers.
  583. </p>
  584. <h2 id="dropdown-attachContainer">
  585. Attached below the container
  586. </h2>
  587. <p>
  588. Select2 can place the dropdown directly after the selection container, so
  589. it will appear in the same location within the DOM as the rest of Select2.
  590. </p>
  591. <dl class="dl-horizontal">
  592. <dt>Adapter</dt>
  593. <dd>
  594. <code title="select2/dropdown">DropdownAdapter</code>
  595. </dd>
  596. <dt>Decorator</dt>
  597. <dd>
  598. <code title="select2/dropdown/attachContainer">AttachContainer</code>
  599. </dd>
  600. </dl>
  601. <div class="alert alert-info">
  602. <strong>
  603. <a href="https://harvesthq.github.io/chosen/">Harvest Chosen</a>
  604. migrators!
  605. </strong>
  606. If you are migrating to Select2 from Chosen, this option will cause
  607. Select2 to position the dropdown in a similar way.
  608. </div>
  609. <h2 id="dropdown-search">
  610. Search
  611. </h2>
  612. <p>
  613. Users can filter down the results by typing a search term into a box that
  614. is displayed at the top of the dropdown.
  615. </p>
  616. <dl class="dl-horizontal">
  617. <dt>Adapter</dt>
  618. <dd>
  619. <code title="select2/dropdown">DropdownAdapter</code>
  620. </dd>
  621. <dt>Decorator</dt>
  622. <dd>
  623. <code title="select2/dropdown/search">DropdownSearch</code>
  624. </dd>
  625. </dl>
  626. <p>
  627. A search box is added to the top of the dropdown automatically for select
  628. boxes where only a single option can be selected.
  629. </p>
  630. </section>
  631. <section id="events">
  632. <div class="page-header">
  633. <h1>Events</h1>
  634. </div>
  635. <p>
  636. Select2 has an internal event system that is used to notify parts of the
  637. component that state has changed, as well as an adapter that allows some
  638. of these events to be relayed to the outside word.
  639. </p>
  640. <dl class="dl-horizontal">
  641. <dt>Adapter</dt>
  642. <dd>
  643. <code title="select2/selection">SelectionAdapter</code>
  644. </dd>
  645. <dt>Decorator</dt>
  646. <dd>
  647. <code title="select2/selection/eventRelay">EventRelay</code>
  648. </dd>
  649. </dl>
  650. <h2>
  651. Public events
  652. </h2>
  653. <p>
  654. All public events are relayed using the jQuery event system, and they are
  655. triggered on the <code>&lt;select&gt;</code> element that Select2 is
  656. attached to. You can attach to them using the
  657. <a href="https://api.jquery.com/on/"><code>.on</code> method</a> provided
  658. by jQuery.
  659. </p>
  660. <h2>
  661. Internal events
  662. </h2>
  663. <p>
  664. Select2 triggers internal events using its own internal event system,
  665. which allows adapters to communicate with each other. These events are not
  666. accessible through the jQuery event system.
  667. </p>
  668. <p>
  669. You can find more information on the public events triggered by individual
  670. adapters in <a href="#adapters">the individual adapter documentation</a>.
  671. </p>
  672. </section>
  673. <section id="adapters">
  674. <div class="page-header">
  675. <h1>Adapters</h1>
  676. </div>
  677. <p>
  678. Select2 allows plugins to add additional functionality through the core
  679. adapters. You can change almost anything involving the way Select2 works
  680. to the way Select2 interacts with the page by modifying the core adapters.
  681. Most third-party plugins should provide decorators (used to wrap adapters)
  682. and custom adapters that you can use.
  683. </p>
  684. <p>
  685. Each adapter contains a set of methods which will must always be defined.
  686. Along with the global methods that all adapters must implement, these
  687. methods must be implemented.
  688. </p>
  689. <h2>
  690. All adapters
  691. </h2>
  692. <p>
  693. All adapters must implement a set of methods that Select2 will use to
  694. display them and bind any internal events.
  695. </p>
  696. <pre class="prettyprint linenums">
  697. // The basic HTML that should be rendered by Select2. A jQuery or DOM element
  698. // should be returned, which will automatically be placed by Select2 within the
  699. // DOM.
  700. //
  701. // @returns A jQuery or DOM element that contains any elements that must be
  702. // rendered by Select2.
  703. Adapter.render = function () {
  704. return $jq;
  705. };
  706. // Bind to any Select2 or DOM events.
  707. //
  708. // @param container The Select2 object that is bound to the jQuery element. You
  709. // can listen to Select2 events with `on` and trigger Select2 events using the
  710. // `trigger` method.
  711. // @param $container The jQuery DOM node that all default adapters will be
  712. // rendered within.
  713. Adapter.bind = function (container, $container) { };
  714. // Position the DOM element within the Select2 DOM container, or in another
  715. // place. This allows adapters to be located outside of the Select2 DOM,
  716. // such as at the end of the document or in a specific place within the Select2
  717. // DOM node.
  718. //
  719. // Note: This method is not called on data adapters.
  720. //
  721. // @param $rendered The rendered DOM element that was returned from the call to
  722. // `render`. This may have been modified by Select2, but the root element
  723. // will always be the same.
  724. // @param $defaultContainer The default container that Select2 will typically
  725. // place the rendered DOM element within. For most adapters, this is the
  726. // Select2 DOM element.
  727. Adapter.position = function ($rendered, $defaultContainer) { };
  728. // Destroy any events or DOM elements that have been created.
  729. // This is called when `select2("destroy")` is called on an element.
  730. Adapter.destroy = function () { };
  731. </pre>
  732. <h2 id="selectionAdapter">
  733. Container (selection)
  734. </h2>
  735. <p>
  736. The selection is what is shown to the user as a replacement of the
  737. standard <code>&lt;select&gt;</code> box. It controls the display of the
  738. selection option(s), as well anything else that needs to be embedded
  739. within the container, such as a search box.
  740. </p>
  741. <dl class="dl-horizontal">
  742. <dt>Key</dt>
  743. <dd>
  744. <code>selectionAdapter</code>
  745. </dd>
  746. <dt>Default</dt>
  747. <dd>
  748. <code title="select2/selection/single">SingleSelection</code> or
  749. <code title="select2/selection/multiple">MultipleSelection</code>
  750. </dd>
  751. <dt>Base</dt>
  752. <dd>
  753. <code title="select2/selection/base">BaseSelection</code>
  754. </dd>
  755. </dl>
  756. <pre class="prettyprint linenums">
  757. // Update the selected data.
  758. //
  759. // @param data An array of data objects that have been generated by the data
  760. // adapter. If no objects should be selected, an empty array will be passed.
  761. //
  762. // Note: An array will always be passed into this method, even if Select2 is
  763. // attached to a source which only accepts a single selection.
  764. SelectionAdapter.update = function (data) { };
  765. </pre>
  766. <h2 id="dataAdapter">
  767. Data set
  768. </h2>
  769. <p>
  770. The data set is what Select2 uses to generate the possible results that
  771. can be selected, as well as the currently selected results.
  772. </p>
  773. <dl class="dl-horizontal">
  774. <dt>Key</dt>
  775. <dd>
  776. <code>dataAdapter</code>
  777. </dd>
  778. <dt>Default</dt>
  779. <dd>
  780. <code title="select2/data/select">SelectAdapter</code>
  781. </dd>
  782. <dt>Base</dt>
  783. <dd>
  784. <code title="select2/data/base">BaseAdapter</code>
  785. </dd>
  786. </dl>
  787. <pre class="prettyprint linenums">
  788. // Get the currently selected options. This is called when trying to get the
  789. // initial selection for Select2, as well as when Select2 needs to determine
  790. // what options within the results are selected.
  791. //
  792. // @param callback A function that should be called when the current selection
  793. // has been retrieved. The first parameter to the function should be an array
  794. // of data objects.
  795. DataAdapter.current = function (callback) {
  796. callback(currentData);
  797. }
  798. // Get a set of options that are filtered based on the parameters that have
  799. // been passed on in.
  800. //
  801. // @param params An object containing any number of parameters that the query
  802. // could be affected by. Only the core parameters will be documented.
  803. // @param params.term A user-supplied term. This is typically the value of the
  804. // search box, if one exists, but can also be an empty string or null value.
  805. // @param params.page The specific page that should be loaded. This is typically
  806. // provided when working with remote data sets, which rely on pagination to
  807. // determine what objects should be displayed.
  808. // @param callback The function that should be called with the queried results.
  809. DataAdapter.query = function (params, callback) {
  810. callback(queryiedData);
  811. }
  812. </pre>
  813. <h2 id="dropdownAdapter">
  814. Dropdown
  815. </h2>
  816. <p>
  817. The dropdown adapter defines the main container that the dropdown should
  818. be held in. <strong>It does not define any extra methods that can be used
  819. for decorators</strong>, but it is common for decorators to attach to the
  820. <code>render</code> and <code>position</code> methods to alter how the
  821. dropdown is altered and positioned.
  822. </p>
  823. <dl class="dl-horizontal">
  824. <dt>Key</dt>
  825. <dd>
  826. <code>dropdownAdapter</code>
  827. </dd>
  828. <dt>Default</dt>
  829. <dd>
  830. <code title="select2/dropdown">DropdownAdapter</code>
  831. </dd>
  832. </dl>
  833. <h2 id="resultsAdapter">
  834. Results
  835. </h2>
  836. <p>
  837. The results adapter controls the list of results that the user can select
  838. from. While the results adapter does not define any additional methods
  839. that must be implemented, it makes extensive use of the Select2 event
  840. system for controlling the display of results and messages.
  841. </p>
  842. <dl class="dl-horizontal">
  843. <dt>Key</dt>
  844. <dd>
  845. <code>resultsAdapter</code>
  846. </dd>
  847. <dt>Default</dt>
  848. <dd>
  849. <code title="select2/results">ResultsAdapter</code>
  850. </dd>
  851. </dl>
  852. </section>
  853. <section id="defaults">
  854. <div class="page-header">
  855. <h1>Setting default options</h1>
  856. </div>
  857. <p>
  858. In some cases, you need to set the default options for all instances of
  859. Select2 in your web application. This is especially useful when you are
  860. migrating from past versions of Select2, or you are using non-standard
  861. options <a href="#amd">like custom AMD builds</a>. Select2 exposes the
  862. default options through <code>$.fn.select2.defaults</code>, which allows
  863. you to set them globally.
  864. </p>
  865. <p>
  866. When setting options globally, any past defaults that have been set will
  867. be overriden. Default options are only used when an option is requested
  868. that has not been set during initialization.
  869. </p>
  870. <p>
  871. <strong>You can set default options</strong> by calling
  872. <code>$.fn.select2.defaults.set("key", "value")</code>. The key that is
  873. set should take the same format as keys set using
  874. <a href="#data-attributes">HTML <code>data-*</code> attributes</a> which
  875. means that two dashes (<code>--</code>) will be replaced by a level of
  876. nesting, and a single dash (<code>-</code>) will convert it to a camelCase
  877. string.
  878. </p>
  879. <pre class="prettyprint linenums">
  880. $.fn.select2.default2.set("theme", "classic");
  881. </pre>
  882. <p>
  883. <strong>You can reset the default options</strong> by calling
  884. <code>$.fn.select2.defaults.reset()</code>.
  885. </p>
  886. </section>
  887. <section id="compatibility">
  888. <div class="page-header">
  889. <h1>Backwards compatibility</h1>
  890. </div>
  891. <p>
  892. Select2 offers limited backwards compatibility with the previously 3.5.x
  893. release line, allowing people more efficiently transfer across releases
  894. and get the latest features. For many of the larger changes, such as the
  895. change in how custom data adapters work, compatibility modules were
  896. created that will be used to assist in the upgrade process. It is not
  897. recommended to rely on these compatibility modules, as they will not
  898. always exist, but they make upgrading easier for major changes.
  899. </p>
  900. <p>
  901. <strong>The compatibility modules are only included in the full builds of
  902. Select2</strong>. These files end in <code>.full.js</code>, and the
  903. compatibility modules are prefixed with <code>select2/compat</code>.
  904. </p>
  905. <h2 id="initSelection">
  906. Old initial selections with <code>initSelection</code>
  907. </h2>
  908. <p class="alert alert-warning">
  909. <a href="announcements-4.0.html#removed-initselection" class="alert-link">Deprecated in Select2 4.0.</a>
  910. This has been replaced by another option.
  911. </p>
  912. <p>
  913. In the past, Select2 required an option called <code>initSelection</code>
  914. that was defined whenever a custom data source was being used, allowing
  915. for the initial selection for the component to be determined. This has
  916. been replaced by the <code>current</code> method on the
  917. <a href="#dataAdapter">data adapter</a>.
  918. </p>
  919. <div class="row">
  920. <div class="col-sm-6">
  921. <dl class="dl-horizontal">
  922. <dt>Key</dt>
  923. <dd>
  924. <code>initSelection</code>
  925. </dd>
  926. <dt>Value</dt>
  927. <dd>
  928. A function taking a <code>callback</code>
  929. </dd>
  930. </dl>
  931. </div>
  932. <div class="col-sm-6">
  933. <dl class="dl-horizontal">
  934. <dt>Adapter</dt>
  935. <dd>
  936. <code title="select2/data/base">DataAdapter</code>
  937. </dd>
  938. <dt>Decorator</dt>
  939. <dd>
  940. <code title="select2/compat/initSelection">InitSelection</code>
  941. </dd>
  942. </dl>
  943. </div>
  944. </div>
  945. <h2 id="query">
  946. Querying old data with <code>query</code>
  947. </h2>
  948. <p class="alert alert-warning">
  949. <a href="announcements-4.0.html#query-to-data-adapter" class="alert-link">Deprecated in Select2 4.0.</a>
  950. This has been replaced by another option.
  951. </p>
  952. <p>
  953. In the past, Select2 supported an option called <code>query</code> that
  954. allowed for a custom data source to be used. This option has been replaced
  955. by the <code>query</code> method on the
  956. <a href="#dataAdapter">data adapter</a> and takes a very similar set of
  957. parameters.
  958. </p>
  959. <div class="row">
  960. <div class="col-sm-6">
  961. <dl class="dl-horizontal">
  962. <dt>Key</dt>
  963. <dd>
  964. <code>query</code>
  965. </dd>
  966. <dt>Value</dt>
  967. <dd>
  968. A function taking <code>params</code> (including a <code>callback</code>)
  969. </dd>
  970. </dl>
  971. </div>
  972. <div class="col-sm-6">
  973. <dl class="dl-horizontal">
  974. <dt>Adapter</dt>
  975. <dd>
  976. <code title="select2/data/base">DataAdapter</code>
  977. </dd>
  978. <dt>Decorator</dt>
  979. <dd>
  980. <code title="select2/compat/query">Query</code>
  981. </dd>
  982. </dl>
  983. </div>
  984. </div>
  985. </section>
  986. </div>
  987. <script type="text/javascript">
  988. prettyPrint();
  989. </script>