options.html 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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 linenums">
  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 linenums">
  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. // You can use a custom AJAX transport function if you do not want to use the
  489. // default one provided by jQuery.
  490. //
  491. // @param params The object containing the parameters used to generate the
  492. // request.
  493. // @param success A callback function that takes `data`, the results from the
  494. // request.
  495. // @param failure A callback function that indicates that the request could
  496. // not be completed.
  497. // @returns An object that has an `abort` function that can be called to abort
  498. // the request if needed.
  499. transport: function (params, success, failure) {
  500. var $request = $.ajax(params);
  501. $request.then(success);
  502. $request.fail(failure);
  503. return $request;
  504. }
  505. }
  506. </pre>
  507. </p>
  508. <h3 id="tags">
  509. Tags
  510. </h3>
  511. <p>
  512. Users can create their own options based on the text that they have
  513. entered.
  514. </p>
  515. <div class="row">
  516. <div class="col-sm-6">
  517. <dl class="dl-horizontal">
  518. <dt>Key</dt>
  519. <dd><code>tags</code></dd>
  520. <dt>Value</dt>
  521. <dd>boolean / array of objects</dd>
  522. </dl>
  523. </div>
  524. <div class="col-sm-6">
  525. <dl class="dl-horizontal">
  526. <dt>Adapter</dt>
  527. <dd>
  528. <code title="select2/data/base">DataAdapter</code>
  529. </dd>
  530. <dt>Decorator</dt>
  531. <dd>
  532. <code title="select2/data/tags">Tags</code>
  533. </dd>
  534. </dl>
  535. </div>
  536. </div>
  537. <p>
  538. If the <code>tags</code> option is passed into Select2, if a user types
  539. anything into the search box which doesn't already exist, it will be
  540. displayed at the top and the user will be able to select it.
  541. </p>
  542. <p>
  543. <strong>For backwards compatibility</strong>, if an array of objects is
  544. passed in with the <code>tags</code> option, the options will be
  545. automatically created and the user will be able to select from them.
  546. This is the <strong>same as how <a href="#data">array data</a>
  547. works</strong>, and has similar limitations.
  548. </p>
  549. <h3 id="matcher">
  550. Change how options are matched when searching
  551. </h3>
  552. <p>
  553. When users filter down the results by entering search terms into the
  554. search box, Select2 uses an internal "matcher" to match search terms to
  555. results. <strong>When a remote data set is used, Select2 expects that the
  556. returned results have already been filtered.</strong>
  557. </p>
  558. <dl class="dl-horizontal">
  559. <dt>Key</dt>
  560. <dd>
  561. <code>matcher</code>
  562. </dd>
  563. <dt>Value</dt>
  564. <dd>
  565. A function taking search <code>params</code> and the
  566. <code>data</code> object.
  567. </dd>
  568. </dl>
  569. <p>
  570. Select2 will pass the individual data objects that have been passed back
  571. from the data adapter into the <code>matcher</code> individually to
  572. determine if they should be displayed. Only the first-level objects will
  573. be passed in, so <strong>if you are working with nested data, you need to
  574. match those individually</strong>.
  575. </p>
  576. <pre class="prettyprint linenums">
  577. matcher: function (params, data) {
  578. // If there are no search terms, return all of the data
  579. if ($.trim(params.term) === '') {
  580. return data;
  581. }
  582. // `params.term` should be the term that is used for searching
  583. // `data.text` is the text that is displayed for the data object
  584. if (data.text.indexOf(params.term) > -1) {
  585. var modifiedData = $.extend({}, data, true);
  586. modifiedData.text += ' (matched)';
  587. // You can return modified objects from here
  588. // This includes matching the `children` how you want in nested data sets
  589. return modifiedData;
  590. }
  591. // Return `null` if the term should not be displayed
  592. return null;
  593. }
  594. </pre>
  595. <p>
  596. This allows for more advanced matching when working with nested objects,
  597. allowing you to handle them however you want. For those who are not
  598. looking to implement highly customized matching, but instead are just
  599. looking to change the matching algorithm for the text, a
  600. <a href="#compat-matcher">compatibility modules</a> has been created to
  601. make it easier.
  602. </p>
  603. </section>
  604. <section id="dropdown">
  605. <div class="page-header">
  606. <h1>Dropdown</h1>
  607. </div>
  608. <p>
  609. Select2 allows you to change the way that the dropdown works, allowing you
  610. to do anything from attach it to a different location in the document or
  611. add a search box.
  612. </p>
  613. <h2 id="dropdownParent">
  614. Attached to body
  615. </h2>
  616. <p>
  617. By default, Select2 will attach the dropdown to the end of the body and
  618. will absolutely position it to appear below the selection container.
  619. </p>
  620. <div class="row">
  621. <div class="col-sm-4">
  622. <dl class="dl-horizontal">
  623. <dt>Key</dt>
  624. <dd><code>dropdownParent</code></dd>
  625. <dt>Value</dt>
  626. <dd>jQuery element or DOM node</dd>
  627. <hr />
  628. <dt>Adapter</dt>
  629. <dd>
  630. <code title="select2/dropdown">DropdownAdapter</code>
  631. </dd>
  632. <dt>Decorator</dt>
  633. <dd>
  634. <code title="select2/dropdown/attachBody">AttachBody</code>
  635. </dd>
  636. </dl>
  637. </div>
  638. <div class="col-sm-8">
  639. <div class="alert alert-warning">
  640. <strong>Heads up!</strong>
  641. This will cause DOM events to be raised outside of the standard
  642. Select2 DOM container. This can cause issues with
  643. third-party components such as modals.
  644. </div>
  645. </div>
  646. </div>
  647. <p>
  648. When the dropdown is attached to the body, you are not limited to just
  649. displaying the dropdown below the container. Select2 will display above
  650. the container if there is not enough space below the container, but there
  651. is enough space above it. You are also not limited to displaying the
  652. drodown within the parent container, which means Select2 will render
  653. correctly inside of modals and other small containers.
  654. </p>
  655. <h2 id="dropdown-attachContainer">
  656. Attached below the container
  657. </h2>
  658. <p>
  659. Select2 can place the dropdown directly after the selection container, so
  660. it will appear in the same location within the DOM as the rest of Select2.
  661. </p>
  662. <div class="row">
  663. <div class="col-sm-4">
  664. <dl class="dl-horizontal">
  665. <dt>Adapter</dt>
  666. <dd>
  667. <code title="select2/dropdown">DropdownAdapter</code>
  668. </dd>
  669. <dt>Decorator</dt>
  670. <dd>
  671. <code title="select2/dropdown/attachContainer">AttachContainer</code>
  672. </dd>
  673. </dl>
  674. </div>
  675. <div class="col-sm-8">
  676. <div class="alert alert-warning">
  677. <strong>Check your build.</strong> This module is only included in the
  678. <a href="index.html#builds-full" class="alert-link">full builds</a> of
  679. Select2.
  680. </div>
  681. </div>
  682. </div>
  683. <div class="alert alert-info">
  684. <strong>
  685. <a href="https://harvesthq.github.io/chosen/">Harvest Chosen</a>
  686. migrators!
  687. </strong>
  688. If you are migrating to Select2 from Chosen, this option will cause
  689. Select2 to position the dropdown in a similar way.
  690. </div>
  691. <h2 id="dropdown-search">
  692. Search
  693. </h2>
  694. <p>
  695. Users can filter down the results by typing a search term into a box that
  696. is displayed at the top of the dropdown.
  697. </p>
  698. <dl class="dl-horizontal">
  699. <dt>Adapter</dt>
  700. <dd>
  701. <code title="select2/dropdown">DropdownAdapter</code>
  702. </dd>
  703. <dt>Decorator</dt>
  704. <dd>
  705. <code title="select2/dropdown/search">DropdownSearch</code>
  706. </dd>
  707. </dl>
  708. <p>
  709. A search box is added to the top of the dropdown automatically for select
  710. boxes where only a single option can be selected.
  711. </p>
  712. <h2 id="dropdown-select-on-close">
  713. Select the highlighted option on close
  714. </h2>
  715. <p>
  716. When users close the dropdown, the last highlighted option can be
  717. automatically selected. This is commonly used in combination with
  718. <a href="#tags">tagging</a> and <a href="#placeholder">placeholders</a>
  719. and other situations where the user is required to select an option, or
  720. they need to be able to quickly select multiple options.
  721. </p>
  722. <dl class="dl-horizontal">
  723. <dt>Adapter</dt>
  724. <dd>
  725. <code title="select2/results">ResultsAdapter</code>
  726. </dd>
  727. <dt>Decorator</dt>
  728. <dd>
  729. <code title="select2/dropdown/selectOnClose">SelectOnClose</code>
  730. </dd>
  731. </dl>
  732. </section>
  733. <section id="events">
  734. <div class="page-header">
  735. <h1>Events</h1>
  736. </div>
  737. <p>
  738. Select2 has an internal event system that is used to notify parts of the
  739. component that state has changed, as well as an adapter that allows some
  740. of these events to be relayed to the outside word.
  741. </p>
  742. <dl class="dl-horizontal">
  743. <dt>Adapter</dt>
  744. <dd>
  745. <code title="select2/selection">SelectionAdapter</code>
  746. </dd>
  747. <dt>Decorator</dt>
  748. <dd>
  749. <code title="select2/selection/eventRelay">EventRelay</code>
  750. </dd>
  751. </dl>
  752. <h2>
  753. Public events
  754. </h2>
  755. <p>
  756. All public events are relayed using the jQuery event system, and they are
  757. triggered on the <code>&lt;select&gt;</code> element that Select2 is
  758. attached to. You can attach to them using the
  759. <a href="https://api.jquery.com/on/"><code>.on</code> method</a> provided
  760. by jQuery.
  761. </p>
  762. <h2>
  763. Internal events
  764. </h2>
  765. <p>
  766. Select2 triggers internal events using its own internal event system,
  767. which allows adapters to communicate with each other. These events are not
  768. accessible through the jQuery event system.
  769. </p>
  770. <p>
  771. You can find more information on the public events triggered by individual
  772. adapters in <a href="#adapters">the individual adapter documentation</a>.
  773. </p>
  774. </section>
  775. <section id="adapters">
  776. <div class="page-header">
  777. <h1>Adapters</h1>
  778. </div>
  779. <p>
  780. Select2 allows plugins to add additional functionality through the core
  781. adapters. You can change almost anything involving the way Select2 works
  782. to the way Select2 interacts with the page by modifying the core adapters.
  783. Most third-party plugins should provide decorators (used to wrap adapters)
  784. and custom adapters that you can use.
  785. </p>
  786. <p>
  787. Each adapter contains a set of methods which will must always be defined.
  788. Along with the global methods that all adapters must implement, these
  789. methods must be implemented.
  790. </p>
  791. <h2>
  792. All adapters
  793. </h2>
  794. <p>
  795. All adapters must implement a set of methods that Select2 will use to
  796. display them and bind any internal events.
  797. </p>
  798. <pre class="prettyprint linenums">
  799. // The basic HTML that should be rendered by Select2. A jQuery or DOM element
  800. // should be returned, which will automatically be placed by Select2 within the
  801. // DOM.
  802. //
  803. // @returns A jQuery or DOM element that contains any elements that must be
  804. // rendered by Select2.
  805. Adapter.render = function () {
  806. return $jq;
  807. };
  808. // Bind to any Select2 or DOM events.
  809. //
  810. // @param container The Select2 object that is bound to the jQuery element. You
  811. // can listen to Select2 events with `on` and trigger Select2 events using the
  812. // `trigger` method.
  813. // @param $container The jQuery DOM node that all default adapters will be
  814. // rendered within.
  815. Adapter.bind = function (container, $container) { };
  816. // Position the DOM element within the Select2 DOM container, or in another
  817. // place. This allows adapters to be located outside of the Select2 DOM,
  818. // such as at the end of the document or in a specific place within the Select2
  819. // DOM node.
  820. //
  821. // Note: This method is not called on data adapters.
  822. //
  823. // @param $rendered The rendered DOM element that was returned from the call to
  824. // `render`. This may have been modified by Select2, but the root element
  825. // will always be the same.
  826. // @param $defaultContainer The default container that Select2 will typically
  827. // place the rendered DOM element within. For most adapters, this is the
  828. // Select2 DOM element.
  829. Adapter.position = function ($rendered, $defaultContainer) { };
  830. // Destroy any events or DOM elements that have been created.
  831. // This is called when `select2("destroy")` is called on an element.
  832. Adapter.destroy = function () { };
  833. </pre>
  834. <h2 id="selectionAdapter">
  835. Container (selection)
  836. </h2>
  837. <p>
  838. The selection is what is shown to the user as a replacement of the
  839. standard <code>&lt;select&gt;</code> box. It controls the display of the
  840. selection option(s), as well anything else that needs to be embedded
  841. within the container, such as a search box.
  842. </p>
  843. <dl class="dl-horizontal">
  844. <dt>Key</dt>
  845. <dd>
  846. <code>selectionAdapter</code>
  847. </dd>
  848. <dt>Default</dt>
  849. <dd>
  850. <code title="select2/selection/single">SingleSelection</code> or
  851. <code title="select2/selection/multiple">MultipleSelection</code>
  852. </dd>
  853. <dt>Base</dt>
  854. <dd>
  855. <code title="select2/selection/base">BaseSelection</code>
  856. </dd>
  857. </dl>
  858. <pre class="prettyprint linenums">
  859. // Update the selected data.
  860. //
  861. // @param data An array of data objects that have been generated by the data
  862. // adapter. If no objects should be selected, an empty array will be passed.
  863. //
  864. // Note: An array will always be passed into this method, even if Select2 is
  865. // attached to a source which only accepts a single selection.
  866. SelectionAdapter.update = function (data) { };
  867. </pre>
  868. <h2 id="dataAdapter">
  869. Data set
  870. </h2>
  871. <p>
  872. The data set is what Select2 uses to generate the possible results that
  873. can be selected, as well as the currently selected results.
  874. </p>
  875. <dl class="dl-horizontal">
  876. <dt>Key</dt>
  877. <dd>
  878. <code>dataAdapter</code>
  879. </dd>
  880. <dt>Default</dt>
  881. <dd>
  882. <code title="select2/data/select">SelectAdapter</code>
  883. </dd>
  884. <dt>Base</dt>
  885. <dd>
  886. <code title="select2/data/base">BaseAdapter</code>
  887. </dd>
  888. </dl>
  889. <pre class="prettyprint linenums">
  890. // Get the currently selected options. This is called when trying to get the
  891. // initial selection for Select2, as well as when Select2 needs to determine
  892. // what options within the results are selected.
  893. //
  894. // @param callback A function that should be called when the current selection
  895. // has been retrieved. The first parameter to the function should be an array
  896. // of data objects.
  897. DataAdapter.current = function (callback) {
  898. callback(currentData);
  899. }
  900. // Get a set of options that are filtered based on the parameters that have
  901. // been passed on in.
  902. //
  903. // @param params An object containing any number of parameters that the query
  904. // could be affected by. Only the core parameters will be documented.
  905. // @param params.term A user-supplied term. This is typically the value of the
  906. // search box, if one exists, but can also be an empty string or null value.
  907. // @param params.page The specific page that should be loaded. This is typically
  908. // provided when working with remote data sets, which rely on pagination to
  909. // determine what objects should be displayed.
  910. // @param callback The function that should be called with the queried results.
  911. DataAdapter.query = function (params, callback) {
  912. callback(queryiedData);
  913. }
  914. </pre>
  915. <h2 id="dropdownAdapter">
  916. Dropdown
  917. </h2>
  918. <p>
  919. The dropdown adapter defines the main container that the dropdown should
  920. be held in. <strong>It does not define any extra methods that can be used
  921. for decorators</strong>, but it is common for decorators to attach to the
  922. <code>render</code> and <code>position</code> methods to alter how the
  923. dropdown is altered and positioned.
  924. </p>
  925. <dl class="dl-horizontal">
  926. <dt>Key</dt>
  927. <dd>
  928. <code>dropdownAdapter</code>
  929. </dd>
  930. <dt>Default</dt>
  931. <dd>
  932. <code title="select2/dropdown">DropdownAdapter</code>
  933. </dd>
  934. </dl>
  935. <h2 id="resultsAdapter">
  936. Results
  937. </h2>
  938. <p>
  939. The results adapter controls the list of results that the user can select
  940. from. While the results adapter does not define any additional methods
  941. that must be implemented, it makes extensive use of the Select2 event
  942. system for controlling the display of results and messages.
  943. </p>
  944. <dl class="dl-horizontal">
  945. <dt>Key</dt>
  946. <dd>
  947. <code>resultsAdapter</code>
  948. </dd>
  949. <dt>Default</dt>
  950. <dd>
  951. <code title="select2/results">ResultsAdapter</code>
  952. </dd>
  953. </dl>
  954. </section>
  955. <section id="defaults">
  956. <div class="page-header">
  957. <h1>Setting default options</h1>
  958. </div>
  959. <p>
  960. In some cases, you need to set the default options for all instances of
  961. Select2 in your web application. This is especially useful when you are
  962. migrating from past versions of Select2, or you are using non-standard
  963. options <a href="#amd">like custom AMD builds</a>. Select2 exposes the
  964. default options through <code>$.fn.select2.defaults</code>, which allows
  965. you to set them globally.
  966. </p>
  967. <p>
  968. When setting options globally, any past defaults that have been set will
  969. be overriden. Default options are only used when an option is requested
  970. that has not been set during initialization.
  971. </p>
  972. <p>
  973. <strong>You can set default options</strong> by calling
  974. <code>$.fn.select2.defaults.set("key", "value")</code>. The key that is
  975. set should take the same format as keys set using
  976. <a href="#data-attributes">HTML <code>data-*</code> attributes</a> which
  977. means that two dashes (<code>--</code>) will be replaced by a level of
  978. nesting, and a single dash (<code>-</code>) will convert it to a camelCase
  979. string.
  980. </p>
  981. <pre class="prettyprint linenums">
  982. $.fn.select2.default2.set("theme", "classic");
  983. </pre>
  984. <p>
  985. <strong>You can reset the default options</strong> by calling
  986. <code>$.fn.select2.defaults.reset()</code>.
  987. </p>
  988. </section>
  989. <section id="compatibility">
  990. <div class="page-header">
  991. <h1>Backwards compatibility</h1>
  992. </div>
  993. <p>
  994. Select2 offers limited backwards compatibility with the previously 3.5.x
  995. release line, allowing people more efficiently transfer across releases
  996. and get the latest features. For many of the larger changes, such as the
  997. change in how custom data adapters work, compatibility modules were
  998. created that will be used to assist in the upgrade process. It is not
  999. recommended to rely on these compatibility modules, as they will not
  1000. always exist, but they make upgrading easier for major changes.
  1001. </p>
  1002. <p>
  1003. <strong>The compatibility modules are only included in the
  1004. <a href="index.html#builds-full" class="alert-link">full builds</a> of
  1005. Select2</strong>. These files end in <code>.full.js</code>, and the
  1006. compatibility modules are prefixed with <code>select2/compat</code>.
  1007. </p>
  1008. <h2 id="compat-matcher">
  1009. Simplified function for matching data objects
  1010. </h2>
  1011. <p class="alert alert-info">
  1012. <a href="announcements-4.0.html#new-matcher" class="alert-link">Added in Select2 4.0.0.</a>
  1013. This method was added to make upgrading easier from earlier versions of
  1014. Select2.
  1015. </p>
  1016. <p>
  1017. During the <a href="announcements-4.0.html">Select2 4.0.0 release</a>, the
  1018. <code>matcher</code> function was changed to allow for more complex
  1019. matching of nested objects.
  1020. </p>
  1021. <div class="row">
  1022. <div class="col-sm-6">
  1023. <dl class="dl-horizontal">
  1024. <dt>Key</dt>
  1025. <dd>
  1026. <code>matcher</code>
  1027. </dd>
  1028. <dt>Value</dt>
  1029. <dd>
  1030. A function taking a search <code>term</code> and the data object
  1031. <code>text</code>.
  1032. </dd>
  1033. </dl>
  1034. </div>
  1035. <div class="col-sm-6">
  1036. <dl class="dl-horizontal">
  1037. <dt>Adapter</dt>
  1038. <dd>
  1039. <code title="select2/compat/matcher">oldMatcher</code>
  1040. </dd>
  1041. </dl>
  1042. </div>
  1043. </div>
  1044. <p>
  1045. The <a href="examples.html#matcher">custom matcher example</a> provides a
  1046. guide for how to use this in your own application. For those upgrading
  1047. from older versions of Select2, you just need to wrap your old
  1048. <code>matcher</code> with this function to maintain compatibility.
  1049. </p>
  1050. <h2 id="initSelection">
  1051. Old initial selections with <code>initSelection</code>
  1052. </h2>
  1053. <p class="alert alert-warning">
  1054. <a href="announcements-4.0.html#removed-initselection" class="alert-link">Deprecated in Select2 4.0.</a>
  1055. This has been replaced by another option and is only available in the
  1056. <a href="index.html#builds-full" class="alert-link">full builds</a> of
  1057. Select2.
  1058. </p>
  1059. <p>
  1060. In the past, Select2 required an option called <code>initSelection</code>
  1061. that was defined whenever a custom data source was being used, allowing
  1062. for the initial selection for the component to be determined. This has
  1063. been replaced by the <code>current</code> method on the
  1064. <a href="#dataAdapter">data adapter</a>.
  1065. </p>
  1066. <div class="row">
  1067. <div class="col-sm-6">
  1068. <dl class="dl-horizontal">
  1069. <dt>Key</dt>
  1070. <dd>
  1071. <code>initSelection</code>
  1072. </dd>
  1073. <dt>Value</dt>
  1074. <dd>
  1075. A function taking a <code>callback</code>
  1076. </dd>
  1077. </dl>
  1078. </div>
  1079. <div class="col-sm-6">
  1080. <dl class="dl-horizontal">
  1081. <dt>Adapter</dt>
  1082. <dd>
  1083. <code title="select2/data/base">DataAdapter</code>
  1084. </dd>
  1085. <dt>Decorator</dt>
  1086. <dd>
  1087. <code title="select2/compat/initSelection">InitSelection</code>
  1088. </dd>
  1089. </dl>
  1090. </div>
  1091. </div>
  1092. <h2 id="query">
  1093. Querying old data with <code>query</code>
  1094. </h2>
  1095. <p class="alert alert-warning">
  1096. <a href="announcements-4.0.html#query-to-data-adapter" class="alert-link">Deprecated in Select2 4.0.</a>
  1097. This has been replaced by another option and is only available in the
  1098. <a href="index.html#builds-full" class="alert-link">full builds</a> of
  1099. Select2.
  1100. </p>
  1101. <p>
  1102. In the past, Select2 supported an option called <code>query</code> that
  1103. allowed for a custom data source to be used. This option has been replaced
  1104. by the <code>query</code> method on the
  1105. <a href="#dataAdapter">data adapter</a> and takes a very similar set of
  1106. parameters.
  1107. </p>
  1108. <div class="row">
  1109. <div class="col-sm-6">
  1110. <dl class="dl-horizontal">
  1111. <dt>Key</dt>
  1112. <dd>
  1113. <code>query</code>
  1114. </dd>
  1115. <dt>Value</dt>
  1116. <dd>
  1117. A function taking <code>params</code> (including a <code>callback</code>)
  1118. </dd>
  1119. </dl>
  1120. </div>
  1121. <div class="col-sm-6">
  1122. <dl class="dl-horizontal">
  1123. <dt>Adapter</dt>
  1124. <dd>
  1125. <code title="select2/data/base">DataAdapter</code>
  1126. </dd>
  1127. <dt>Decorator</dt>
  1128. <dd>
  1129. <code title="select2/compat/query">Query</code>
  1130. </dd>
  1131. </dl>
  1132. </div>
  1133. </div>
  1134. </section>
  1135. </div>
  1136. <script type="text/javascript">
  1137. prettyPrint();
  1138. </script>