options.html 39 KB

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