options.html 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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. Due to
  68. <a href="https://github.com/jquery/jquery/issues/2070">a jQuery bug</a>,
  69. nested options using <code>data-*</code> attributes
  70. <a href="https://github.com/select2/select2/issues/2969">do not work in jQuery 1.x</a>.
  71. </p>
  72. <pre class="prettyprint linenums">
  73. &lt;select data-ajax--url="http://example.org/api/test" data-ajax--cache="true"&gt;&lt;/select&gt;
  74. </pre>
  75. <p>
  76. Which will be interpreted the same as initializing Select2 with...
  77. </p>
  78. <pre class="prettyprint linenums">
  79. $("select").select2({
  80. ajax: {
  81. url: "http://example.org/api/test",
  82. cache: "true"
  83. }
  84. });
  85. </pre>
  86. <p>
  87. The value of the option is subject to jQuery's
  88. <a href="https://api.jquery.com/data/#data-html5">parsing rules</a> for
  89. HTML5 data attributes.
  90. </p>
  91. <h2 id="amd">
  92. AMD compatibility
  93. </h2>
  94. <p>
  95. You can find more information on how to integrate Select2 with your
  96. existing AMD-based project by
  97. <a href="announcements-4.0.html#amd-builds">viewing the 4.0 release notes</a>.
  98. Select2 automatically loads some modules when the adapters are being
  99. automatically constructed, so those who are using Select2 with a custom
  100. AMD build using their own system will need to specify the paths that are
  101. generated to the Select2 modules.
  102. </p>
  103. <div class="row">
  104. <div class="col-sm-6">
  105. <dl class="dl-horizontal">
  106. <dt>Key</dt>
  107. <dd>
  108. <code>amdBase</code>
  109. </dd>
  110. <dt>Default</dt>
  111. <dd>
  112. <code>select2/</code>
  113. </dd>
  114. </dl>
  115. </div>
  116. <div class="col-sm-6">
  117. <dl class="dl-horizontal">
  118. <dt>Key</dt>
  119. <dd>
  120. <code>amdLanguageBase</code>
  121. </dd>
  122. <dt>Default</dt>
  123. <dd>
  124. <code>select2/i18n/</code>
  125. </dd>
  126. </dl>
  127. </div>
  128. </div>
  129. <p>
  130. If you are using Select2 in your own code base with your own AMD build
  131. system, it is recommended to use the full source of Select2 instead of the
  132. custom AMD build (<code>select2.amd.js</code>). This will allow you to
  133. apply your own build steps, such as minification and concatenation,
  134. independent of the Select2 defaults. You will need to set the path to the
  135. source directories in the <code>amdBase</code> and
  136. <code>amdLanguage</code> base options, which can be set globally through
  137. <a href="#defaults">the default options</a>.
  138. </p>
  139. <h2>
  140. Display
  141. </h2>
  142. <p>
  143. Select2 provides options that allow you to directly affect how the
  144. container that holds the current selection is displayed.
  145. </p>
  146. <h3 id="placeholder">
  147. Placeholders
  148. </h3>
  149. <p>
  150. Select2 can display a placeholder for a single-value select that will
  151. replace an option, or be shown when no options are selected for
  152. multiple-value selects. You can find an example on the
  153. <a href="examples.html#placeholders">example page</a>.
  154. </p>
  155. <div class="row">
  156. <div class="col-sm-4">
  157. <dl class="dl-horizontal">
  158. <dt>Key</dt>
  159. <dd>
  160. <code>placeholder</code>
  161. </dd>
  162. <dt>Value</dt>
  163. <dd>string or object</dd>
  164. </dl>
  165. <hr />
  166. <dl class="dl-horizontal">
  167. <dt>Adapter</dt>
  168. <dd>
  169. <code title="select2/selection/base">SelectionAdapter</code>
  170. </dd>
  171. <dt>Decorator</dt>
  172. <dd>
  173. <code title="select2/selection/placeholder">Placeholder</code>
  174. and
  175. <code title="select2/dropdown/hidePlaceholder">HidePlaceholder</code>
  176. </dd>
  177. </dl>
  178. </div>
  179. <div class="col-sm-8">
  180. <div class="alert alert-warning">
  181. <strong>Heads up!</strong>
  182. Because browsers assume that the first <code>option</code> in
  183. single-value select boxes is selected, you should add an empty
  184. <code>&lt;option&gt;&lt;/option&gt;</code> tag that the placeholder
  185. should use or it may not work.
  186. </div>
  187. </div>
  188. </div>
  189. <p>
  190. If the <strong>value is a string</strong>, the placeholder will be
  191. displayed when a <strong>blank option</strong> is used as the placeholder.
  192. The <strong>value</strong> will be the message to show to users as the
  193. placeholders.
  194. </p>
  195. <pre class="prettyprint linenums">
  196. placeholder: "Select a repository",
  197. </pre>
  198. <p>
  199. If the <strong>value is an object</strong>, the object should be
  200. compatible with Select2's internal objects. The <code>id</code> should
  201. be the id to look for when determining if the placeholder should be
  202. displayed. The <code>text</code> should be the placeholder to display
  203. when that option is selected.
  204. </p>
  205. <pre class="prettyprint linenums">
  206. placeholder: {
  207. id: "-1",
  208. text: "Select a repository"
  209. }
  210. </pre>
  211. <div class="alert alert-info">
  212. You should <strong>pass in an object</strong> when you are using a
  213. framework that <strong>creates its own placeholder option</strong>. The
  214. <strong>id</strong> should be the same as the <code>value</code>
  215. attribute on the <code>option</code>.
  216. </div>
  217. <p id="allowClear">
  218. You can allow a selected option to be cleared back to the placeholder by
  219. enabling the <code>allowClear</code> option.
  220. </p>
  221. <div class="row">
  222. <div class="col-sm-6">
  223. <dl class="dl-horizontal">
  224. <dt>Key</dt>
  225. <dd><code>allowClear</code></dd>
  226. <dt>Value</dt>
  227. <dd>boolean</dd>
  228. </dl>
  229. </div>
  230. <div class="col-sm-6">
  231. <dl class="dl-horizontal">
  232. <dt>Adapter</dt>
  233. <dd>
  234. <code title="select2/selection/base">SelectionAdapter</code>
  235. </dd>
  236. <dt>Decorator</dt>
  237. <dd>
  238. <code title="select2/selection/allowClear">AllowClear</code>
  239. </dd>
  240. </dl>
  241. </div>
  242. </div>
  243. <p>
  244. This will display an "x" that the user can click to clear the current
  245. selection. It is designed to be used for cases where a single selection
  246. can be made.
  247. </p>
  248. <h3 id="multiple">
  249. Multiple selections
  250. </h3>
  251. <p>
  252. Select2 can display either a single selection or multiple selections.
  253. </p>
  254. <dl class="dl-horizontal">
  255. <dt>Key</dt>
  256. <dd><code>multiple</code></dd>
  257. <dt>Value</dt>
  258. <dd>boolean (<code>true</code> or <code>false</code>)</dd>
  259. </dl>
  260. <p>
  261. This option will determine what the <code>SelectAdapter</code> (used by
  262. default) should use to set the value of the underlying <code>select</code>
  263. element. It will also determine if the <code>MultipleSelection</code>
  264. adapter should be used.
  265. </p>
  266. <h3 id="width">
  267. Container width
  268. </h3>
  269. <p>
  270. Select2 will try to match the width of the original element as closely as
  271. possible. Sometimes this isn't perfect, which is what you can tell Select2
  272. how to determine the width.
  273. </p>
  274. <div class="row">
  275. <div class="col-sm-8">
  276. <table class="table table-striped table-bordered">
  277. <thead>
  278. <tr>
  279. <th>Value</th>
  280. <th>Description</th>
  281. </tr>
  282. </thead>
  283. <tbody>
  284. <tr>
  285. <td><code>"element"</code></td>
  286. <td>
  287. Uses javascript to calculate the width of the source element.
  288. </td>
  289. </tr>
  290. <tr>
  291. <td><code>"style"</code></td>
  292. <td>
  293. Copies the value of the width <code>style</code> attribute set on the source element.
  294. </td>
  295. </tr>
  296. <tr>
  297. <td><code>"resolve"</code></td>
  298. <td>
  299. Tries to use <code>style</code> to determine the width, falling back to <code>element</code>.
  300. </td>
  301. </tr>
  302. <tr>
  303. <td>Anything else</td>
  304. <td>
  305. The value of the <code>width</code> option is directly set as the width of the container.
  306. </td>
  307. </tr>
  308. </tbody>
  309. </table>
  310. </div>
  311. <div class="col-sm-4">
  312. <dl class="dl-horizontal">
  313. <dt>Key</dt>
  314. <dd><code>width</code></dd>
  315. <dt>Value</dt>
  316. <dd>string</dd>
  317. </dl>
  318. </div>
  319. </div>
  320. <h3 id="language">
  321. Internationalization (Language support)
  322. </h3>
  323. <p>
  324. Messages will be displayed to users when necessary, such as when no
  325. search results were found or more characters need to be entered in order
  326. for a search to be made. These messages have been
  327. <a href="community.html#translations">translated into many languages</a>
  328. by contributors to Select2, but you can also provide your own
  329. translations.
  330. </p>
  331. <div class="row">
  332. <div class="col-sm-4">
  333. <dl class="dl-horizontal">
  334. <dt>Key</dt>
  335. <dd><code>language</code></dd>
  336. <dt>Value</dt>
  337. <dd>object or string</dd>
  338. </dl>
  339. <hr />
  340. <dl class="dl-horizontal">
  341. <dt>Module</dt>
  342. <dd>
  343. <code title="select2/translation">Translation</code>
  344. </dd>
  345. </dl>
  346. </div>
  347. <div class="col-sm-8">
  348. <p class="alert alert-warning">
  349. <strong>Heads up!</strong> When using translations provided by Select2,
  350. you must make sure to include the translation file in your page after
  351. Select2.
  352. </p>
  353. </div>
  354. </div>
  355. <p>
  356. When a string is passed in as the language, Select2 will try to resolve
  357. it into a language file. This allows you to specify your own language
  358. files, which must be defined as an AMD module. If the language file
  359. cannot be found, Select2 will assume it is a language code controlled by
  360. Select2, and it will try to load the translations for that language
  361. instead.
  362. </p>
  363. <p>
  364. You can include your own translations by providing an object similar to
  365. the one below.
  366. </p>
  367. <pre class="prettyprint linenums">
  368. language: {
  369. // You can find all of the options in the language files provided in the
  370. // build. They all must be functions that return the string that should be
  371. // displayed.
  372. inputTooShort: function () {
  373. return "You must enter more characters...";
  374. }
  375. }
  376. </pre>
  377. <h3 id="templating">
  378. Templating results and selections
  379. </h3>
  380. <p>
  381. By default, Select2 will display the option text within the list of
  382. results and when the option has been selected. Select2 comes with options
  383. that allow you to further customize the display of results and selections,
  384. allowing you to display them however you want.
  385. </p>
  386. <h4 id="templateSelection">
  387. Customizing the display of selections
  388. </h4>
  389. <p>
  390. When an option is displayed after it has been selected, it is passed
  391. through a formatting function that determines what is displayed. By
  392. default, the function only returns the <code>text</code> key of the data
  393. object.
  394. </p>
  395. <dl class="dl-horizontal">
  396. <dt>Key</dt>
  397. <dd><code>templateSelection</code></dd>
  398. <dt>Value</dt>
  399. <dd>A function taking a <code>selection</code> object</dd>
  400. </dl>
  401. <div class="alert alert-info">
  402. <strong>Anything rendered as a selection is templated.</strong>
  403. This includes placeholders and pre-existing selections that are displayed,
  404. so you must ensure that your templating functions can support them.
  405. </div>
  406. <p>
  407. The <code>templateSelection</code> function should return a string
  408. containing the text to be displayed, or an object (such as a jQuery
  409. object) that contains the data that should be displayed.
  410. </p>
  411. <p>
  412. <strong>Strings are assumed to contain only text</strong> and will be
  413. passed through the <code>escapeMarkup</code> function, which strips any
  414. HTML markup.
  415. </p>
  416. <p>
  417. <strong>
  418. Anything else will be passed
  419. <a href="https://api.jquery.com/append/">directly to <code>jQuery.fn.append</code></a>
  420. </strong> and will be handled directly by jQuery. Any markup, such as
  421. HTML, returned will not be escaped and it is up to you to escape any
  422. malicious input provided by users.
  423. </p>
  424. <h4 id="templateResult">
  425. Customizing the display of results
  426. </h4>
  427. <p>
  428. When an option is displayed after it has been selected, it is passed
  429. through a formatting function that determines what is displayed. By
  430. default, the function only returns the <code>text</code> key of the data
  431. object.
  432. </p>
  433. <dl class="dl-horizontal">
  434. <dt>Key</dt>
  435. <dd><code>templateSelection</code></dd>
  436. <dt>Value</dt>
  437. <dd>A function taking a <code>selection</code> object</dd>
  438. </dl>
  439. <div class="alert alert-info">
  440. <strong>Anything rendered in the results is templated.</strong>
  441. This includes results such as the "Searching..." and "Loading more..."
  442. text which will periodically be displayed, which allows you to add more
  443. advanced formatting to these automatically generated options.
  444. </div>
  445. <p>
  446. The <code>templateResult</code> function should return a string
  447. containing the text to be displayed, or an object (such as a jQuery
  448. object) that contains the data that should be displayed. It can also
  449. return <code>null</code>, which will prevent the option from being
  450. displayed in the results list.
  451. </p>
  452. <p>
  453. <strong>Strings are assumed to contain only text</strong> and will be
  454. passed through the <code>escapeMarkup</code> function, which strips any
  455. HTML markup.
  456. </p>
  457. <p>
  458. <strong>
  459. Anything else will be passed
  460. <a href="https://api.jquery.com/append/">directly to <code>jQuery.fn.append</code></a>
  461. </strong> and will be handled directly by jQuery. Any markup, such as
  462. HTML, returned will not be escaped and it is up to you to escape any
  463. malicious input provided by users.
  464. </p>
  465. <h2>
  466. Results
  467. </h2>
  468. <p>
  469. Select2 can work on many different data sets ranging from local options,
  470. the same way that a <code>&lt;select&gt;</code> typically works, from
  471. remote options where a server generates the results that users can select
  472. from.
  473. </p>
  474. <h3 id="data">
  475. Array
  476. </h3>
  477. <p>
  478. Select2 allows creating the results based on an array of data objects that
  479. is included when initializing Select2.
  480. </p>
  481. <div class="row">
  482. <div class="col-sm-6">
  483. <dl class="dl-horizontal">
  484. <dt>Key</dt>
  485. <dd><code>data</code></dd>
  486. <dt>Value</dt>
  487. <dd>array of objects</dd>
  488. </dl>
  489. </div>
  490. <div class="col-sm-6">
  491. <dl class="dl-horizontal">
  492. <dt>Adapter</dt>
  493. <dd>
  494. <code title="select2/data/array">ArrayAdapter</code>
  495. </dd>
  496. </dl>
  497. </div>
  498. </div>
  499. <p>
  500. The objects that the users can select from should be passed as an array
  501. with each object containing <code>id</code> and <code>text</code>
  502. properties.
  503. </p>
  504. <h3 id="ajax">
  505. AJAX
  506. </h3>
  507. <p>
  508. Select2 allows searching for results from remote data sources using AJAX
  509. requests.
  510. </p>
  511. <div class="row">
  512. <div class="col-sm-6">
  513. <dl class="dl-horizontal">
  514. <dt>Key</dt>
  515. <dd><code>ajax</code></dd>
  516. <dt>Value</dt>
  517. <dd>object</dd>
  518. </dl>
  519. </div>
  520. <div class="col-sm-6">
  521. <dl class="dl-horizontal">
  522. <dt>Adapter</dt>
  523. <dd>
  524. <code title="select2/data/ajax">AjaxAdapter</code>
  525. </dd>
  526. </dl>
  527. </div>
  528. </div>
  529. <p>
  530. All options passed to this option will be directly passed to the
  531. <code>$.ajax</code> function that executes AJAX requests. There are a few
  532. custom options that Select2 will intercept, allowing you to customize the
  533. request as it is being made.
  534. <pre class="prettyprint linenums">
  535. ajax: {
  536. // The number of milliseconds to wait for the user to stop typing before
  537. // issuing the ajax request.
  538. delay: 250,
  539. // You can craft a custom url based on the parameters that are passed into the
  540. // request. This is useful if you are using a framework which has
  541. // JavaScript-based functions for generating the urls to make requests to.
  542. //
  543. // @param params The object containing the parameters used to generate the
  544. // request.
  545. // @returns The url that the request should be made to.
  546. url: function (params) {
  547. return UrlGenerator.Random();
  548. },
  549. // You can pass custom data into the request based on the parameters used to
  550. // make the request. For `GET` requests, the default method, these are the
  551. // query parameters that are appended to the url. For `POST` requests, this
  552. // is the form data that will be passed into the request. For other requests,
  553. // the data returned from here should be customized based on what jQuery and
  554. // your server are expecting.
  555. //
  556. // @param params The object containing the parameters used to generate the
  557. // request.
  558. // @returns Data to be directly passed into the request.
  559. data: function (params) {
  560. var queryParameters = {
  561. q: params.term
  562. }
  563. return queryParameters;
  564. },
  565. // You can modify the results that are returned from the server, allowing you
  566. // to make last-minute changes to the data, or find the correct part of the
  567. // response to pass to Select2. Keep in mind that results should be passed as
  568. // an array of objects.
  569. //
  570. // @param data The data as it is returned directly by jQuery.
  571. // @returns An object containing the results data as well as any required
  572. // metadata that is used by plugins. The object should contain an array of
  573. // data objects as the `results` key.
  574. processResults: function (data) {
  575. return {
  576. results: data
  577. };
  578. },
  579. // You can use a custom AJAX transport function if you do not want to use the
  580. // default one provided by jQuery.
  581. //
  582. // @param params The object containing the parameters used to generate the
  583. // request.
  584. // @param success A callback function that takes `data`, the results from the
  585. // request.
  586. // @param failure A callback function that indicates that the request could
  587. // not be completed.
  588. // @returns An object that has an `abort` function that can be called to abort
  589. // the request if needed.
  590. transport: function (params, success, failure) {
  591. var $request = $.ajax(params);
  592. $request.then(success);
  593. $request.fail(failure);
  594. return $request;
  595. }
  596. }
  597. </pre>
  598. </p>
  599. <h3 id="tags">
  600. Tags
  601. </h3>
  602. <p>
  603. Users can create their own options based on the text that they have
  604. entered.
  605. </p>
  606. <div class="row">
  607. <div class="col-sm-6">
  608. <dl class="dl-horizontal">
  609. <dt>Key</dt>
  610. <dd><code>tags</code></dd>
  611. <dt>Value</dt>
  612. <dd>boolean / array of objects</dd>
  613. </dl>
  614. </div>
  615. <div class="col-sm-6">
  616. <dl class="dl-horizontal">
  617. <dt>Adapter</dt>
  618. <dd>
  619. <code title="select2/data/base">DataAdapter</code>
  620. </dd>
  621. <dt>Decorator</dt>
  622. <dd>
  623. <code title="select2/data/tags">Tags</code>
  624. </dd>
  625. </dl>
  626. </div>
  627. </div>
  628. <p>
  629. If the <code>tags</code> option is passed into Select2, if a user types
  630. anything into the search box which doesn't already exist, it will be
  631. displayed at the top and the user will be able to select it.
  632. </p>
  633. <p>
  634. <strong>For backwards compatibility</strong>, if an array of objects is
  635. passed in with the <code>tags</code> option, the options will be
  636. automatically created and the user will be able to select from them.
  637. This is the <strong>same as how <a href="#data">array data</a>
  638. works</strong>, and has similar limitations.
  639. </p>
  640. <h3 id="matcher">
  641. Change how options are matched when searching
  642. </h3>
  643. <p>
  644. When users filter down the results by entering search terms into the
  645. search box, Select2 uses an internal "matcher" to match search terms to
  646. results. <strong>When a remote data set is used, Select2 expects that the
  647. returned results have already been filtered.</strong>
  648. </p>
  649. <dl class="dl-horizontal">
  650. <dt>Key</dt>
  651. <dd>
  652. <code>matcher</code>
  653. </dd>
  654. <dt>Value</dt>
  655. <dd>
  656. A function taking search <code>params</code> and the
  657. <code>data</code> object.
  658. </dd>
  659. </dl>
  660. <p>
  661. Select2 will pass the individual data objects that have been passed back
  662. from the data adapter into the <code>matcher</code> individually to
  663. determine if they should be displayed. Only the first-level objects will
  664. be passed in, so <strong>if you are working with nested data, you need to
  665. match those individually</strong>.
  666. </p>
  667. <pre class="prettyprint linenums">
  668. matcher: function (params, data) {
  669. // If there are no search terms, return all of the data
  670. if ($.trim(params.term) === '') {
  671. return data;
  672. }
  673. // `params.term` should be the term that is used for searching
  674. // `data.text` is the text that is displayed for the data object
  675. if (data.text.indexOf(params.term) > -1) {
  676. var modifiedData = $.extend({}, data, true);
  677. modifiedData.text += ' (matched)';
  678. // You can return modified objects from here
  679. // This includes matching the `children` how you want in nested data sets
  680. return modifiedData;
  681. }
  682. // Return `null` if the term should not be displayed
  683. return null;
  684. }
  685. </pre>
  686. <p>
  687. This allows for more advanced matching when working with nested objects,
  688. allowing you to handle them however you want. For those who are not
  689. looking to implement highly customized matching, but instead are just
  690. looking to change the matching algorithm for the text, a
  691. <a href="#compat-matcher">compatibility modules</a> has been created to
  692. make it easier.
  693. </p>
  694. </section>
  695. <section id="dropdown">
  696. <div class="page-header">
  697. <h1>Dropdown</h1>
  698. </div>
  699. <p>
  700. Select2 allows you to change the way that the dropdown works, allowing you
  701. to do anything from attach it to a different location in the document or
  702. add a search box.
  703. </p>
  704. <h2 id="dropdownParent">
  705. Attached to body
  706. </h2>
  707. <p>
  708. By default, Select2 will attach the dropdown to the end of the body and
  709. will absolutely position it to appear below the selection container.
  710. </p>
  711. <div class="row">
  712. <div class="col-sm-4">
  713. <dl class="dl-horizontal">
  714. <dt>Key</dt>
  715. <dd><code>dropdownParent</code></dd>
  716. <dt>Value</dt>
  717. <dd>jQuery element or DOM node</dd>
  718. <hr />
  719. <dt>Adapter</dt>
  720. <dd>
  721. <code title="select2/dropdown">DropdownAdapter</code>
  722. </dd>
  723. <dt>Decorator</dt>
  724. <dd>
  725. <code title="select2/dropdown/attachBody">AttachBody</code>
  726. </dd>
  727. </dl>
  728. </div>
  729. <div class="col-sm-8">
  730. <div class="alert alert-warning">
  731. <strong>Heads up!</strong>
  732. This will cause DOM events to be raised outside of the standard
  733. Select2 DOM container. This can cause issues with
  734. third-party components such as modals.
  735. </div>
  736. </div>
  737. </div>
  738. <p>
  739. When the dropdown is attached to the body, you are not limited to just
  740. displaying the dropdown below the container. Select2 will display above
  741. the container if there is not enough space below the container, but there
  742. is enough space above it. You are also not limited to displaying the
  743. dropdown within the parent container, which means Select2 will render
  744. correctly inside of modals and other small containers.
  745. </p>
  746. <h2 id="dropdown-attachContainer">
  747. Attached below the container
  748. </h2>
  749. <p>
  750. Select2 can place the dropdown directly after the selection container, so
  751. it will appear in the same location within the DOM as the rest of Select2.
  752. </p>
  753. <div class="row">
  754. <div class="col-sm-4">
  755. <dl class="dl-horizontal">
  756. <dt>Adapter</dt>
  757. <dd>
  758. <code title="select2/dropdown">DropdownAdapter</code>
  759. </dd>
  760. <dt>Decorator</dt>
  761. <dd>
  762. <code title="select2/dropdown/attachContainer">AttachContainer</code>
  763. </dd>
  764. </dl>
  765. </div>
  766. <div class="col-sm-8">
  767. <div class="alert alert-warning">
  768. <strong>Check your build.</strong> This module is only included in the
  769. <a href="index.html#builds-full" class="alert-link">full builds</a> of
  770. Select2.
  771. </div>
  772. </div>
  773. </div>
  774. <div class="alert alert-info">
  775. <strong>
  776. <a href="https://harvesthq.github.io/chosen/">Harvest Chosen</a>
  777. migrators!
  778. </strong>
  779. If you are migrating to Select2 from Chosen, this option will cause
  780. Select2 to position the dropdown in a similar way.
  781. </div>
  782. <h2 id="dropdown-search">
  783. Search
  784. </h2>
  785. <p>
  786. Users can filter down the results by typing a search term into a box that
  787. is displayed at the top of the dropdown.
  788. </p>
  789. <dl class="dl-horizontal">
  790. <dt>Adapter</dt>
  791. <dd>
  792. <code title="select2/dropdown">DropdownAdapter</code>
  793. </dd>
  794. <dt>Decorator</dt>
  795. <dd>
  796. <code title="select2/dropdown/search">DropdownSearch</code>
  797. </dd>
  798. </dl>
  799. <p>
  800. A search box is added to the top of the dropdown automatically for select
  801. boxes where only a single option can be selected.
  802. </p>
  803. <h2 id="dropdown-select-on-close">
  804. Select the highlighted option on close
  805. </h2>
  806. <p>
  807. When users close the dropdown, the last highlighted option can be
  808. automatically selected. This is commonly used in combination with
  809. <a href="#tags">tagging</a> and <a href="#placeholder">placeholders</a>
  810. and other situations where the user is required to select an option, or
  811. they need to be able to quickly select multiple options.
  812. </p>
  813. <dl class="dl-horizontal">
  814. <dt>Adapter</dt>
  815. <dd>
  816. <code title="select2/results">ResultsAdapter</code>
  817. </dd>
  818. <dt>Decorator</dt>
  819. <dd>
  820. <code title="select2/dropdown/selectOnClose">SelectOnClose</code>
  821. </dd>
  822. </dl>
  823. <h2 id="closeOnSelect">
  824. Close the dropdown when a result is selected
  825. </h2>
  826. <p>
  827. Select2 will automatically close the dropdown when an element is selected,
  828. similar to what is done with a normal select box. This behavior can be
  829. changed though to keep the dropdown open when results are selected,
  830. allowing for multiple options to be selected quickly.
  831. </p>
  832. <div class="row">
  833. <div class="col-sm-6">
  834. <dl class="dl-horizontal">
  835. <dt>Key</dt>
  836. <dd><code>closeOnSelect</code></dd>
  837. <dt>Default</dt>
  838. <dd><code>true</code></dd>
  839. </dl>
  840. </div>
  841. <div class="col-sm-6">
  842. <dl class="dl-horizontal">
  843. <dt>Adapter</dt>
  844. <dd>
  845. <code title="select2/dropdown">DropdownAdapter</code>
  846. </dd>
  847. <dt>Decorator</dt>
  848. <dd>
  849. <code title="select2/dropdown/closeOnSelect">CloseOnSelect</code>
  850. </dd>
  851. </dl>
  852. </div>
  853. </div>
  854. <p>
  855. If this decorator is not used (or <code>closeOnSelect</code> is set to
  856. <code>false</code>), the dropdown will not automatically close when a
  857. result is selected. The dropdown will also never close if the
  858. <kbd>ctrl</kbd> key is held down when the result is selected.
  859. </p>
  860. </section>
  861. <section id="events">
  862. <div class="page-header">
  863. <h1>Events</h1>
  864. </div>
  865. <p>
  866. Select2 has an internal event system that is used to notify parts of the
  867. component that state has changed, as well as an adapter that allows some
  868. of these events to be relayed to the outside word.
  869. </p>
  870. <dl class="dl-horizontal">
  871. <dt>Adapter</dt>
  872. <dd>
  873. <code title="select2/selection">SelectionAdapter</code>
  874. </dd>
  875. <dt>Decorator</dt>
  876. <dd>
  877. <code title="select2/selection/eventRelay">EventRelay</code>
  878. </dd>
  879. </dl>
  880. <h2>
  881. Public events
  882. </h2>
  883. <p>
  884. All public events are relayed using the jQuery event system, and they are
  885. triggered on the <code>&lt;select&gt;</code> element that Select2 is
  886. attached to. You can attach to them using the
  887. <a href="https://api.jquery.com/on/"><code>.on</code> method</a> provided
  888. by jQuery.
  889. </p>
  890. <h2>
  891. Internal events
  892. </h2>
  893. <p>
  894. Select2 triggers internal events using its own internal event system,
  895. which allows adapters to communicate with each other. These events are not
  896. accessible through the jQuery event system.
  897. </p>
  898. <p>
  899. You can find more information on the public events triggered by individual
  900. adapters in <a href="#adapters">the individual adapter documentation</a>.
  901. </p>
  902. </section>
  903. <section id="adapters">
  904. <div class="page-header">
  905. <h1>Adapters</h1>
  906. </div>
  907. <p>
  908. Select2 allows plugins to add additional functionality through the core
  909. adapters. You can change almost anything involving the way Select2 works
  910. to the way Select2 interacts with the page by modifying the core adapters.
  911. Most third-party plugins should provide decorators (used to wrap adapters)
  912. and custom adapters that you can use.
  913. </p>
  914. <p>
  915. Each adapter contains a set of methods which will must always be defined.
  916. Along with the global methods that all adapters must implement, these
  917. methods must be implemented.
  918. </p>
  919. <h2>
  920. All adapters
  921. </h2>
  922. <p>
  923. All adapters must implement a set of methods that Select2 will use to
  924. display them and bind any internal events.
  925. </p>
  926. <pre class="prettyprint linenums">
  927. // The basic HTML that should be rendered by Select2. A jQuery or DOM element
  928. // should be returned, which will automatically be placed by Select2 within the
  929. // DOM.
  930. //
  931. // @returns A jQuery or DOM element that contains any elements that must be
  932. // rendered by Select2.
  933. Adapter.render = function () {
  934. return $jq;
  935. };
  936. // Bind to any Select2 or DOM events.
  937. //
  938. // @param container The Select2 object that is bound to the jQuery element. You
  939. // can listen to Select2 events with `on` and trigger Select2 events using the
  940. // `trigger` method.
  941. // @param $container The jQuery DOM node that all default adapters will be
  942. // rendered within.
  943. Adapter.bind = function (container, $container) { };
  944. // Position the DOM element within the Select2 DOM container, or in another
  945. // place. This allows adapters to be located outside of the Select2 DOM,
  946. // such as at the end of the document or in a specific place within the Select2
  947. // DOM node.
  948. //
  949. // Note: This method is not called on data adapters.
  950. //
  951. // @param $rendered The rendered DOM element that was returned from the call to
  952. // `render`. This may have been modified by Select2, but the root element
  953. // will always be the same.
  954. // @param $defaultContainer The default container that Select2 will typically
  955. // place the rendered DOM element within. For most adapters, this is the
  956. // Select2 DOM element.
  957. Adapter.position = function ($rendered, $defaultContainer) { };
  958. // Destroy any events or DOM elements that have been created.
  959. // This is called when `select2("destroy")` is called on an element.
  960. Adapter.destroy = function () { };
  961. </pre>
  962. <h2 id="selectionAdapter">
  963. Container (selection)
  964. </h2>
  965. <p>
  966. The selection is what is shown to the user as a replacement of the
  967. standard <code>&lt;select&gt;</code> box. It controls the display of the
  968. selection option(s), as well anything else that needs to be embedded
  969. within the container, such as a search box.
  970. </p>
  971. <dl class="dl-horizontal">
  972. <dt>Key</dt>
  973. <dd>
  974. <code>selectionAdapter</code>
  975. </dd>
  976. <dt>Default</dt>
  977. <dd>
  978. <code title="select2/selection/single">SingleSelection</code> or
  979. <code title="select2/selection/multiple">MultipleSelection</code>
  980. </dd>
  981. <dt>Base</dt>
  982. <dd>
  983. <code title="select2/selection/base">BaseSelection</code>
  984. </dd>
  985. </dl>
  986. <pre class="prettyprint linenums">
  987. // Update the selected data.
  988. //
  989. // @param data An array of data objects that have been generated by the data
  990. // adapter. If no objects should be selected, an empty array will be passed.
  991. //
  992. // Note: An array will always be passed into this method, even if Select2 is
  993. // attached to a source which only accepts a single selection.
  994. SelectionAdapter.update = function (data) { };
  995. </pre>
  996. <h2 id="dataAdapter">
  997. Data set
  998. </h2>
  999. <p>
  1000. The data set is what Select2 uses to generate the possible results that
  1001. can be selected, as well as the currently selected results.
  1002. </p>
  1003. <dl class="dl-horizontal">
  1004. <dt>Key</dt>
  1005. <dd>
  1006. <code>dataAdapter</code>
  1007. </dd>
  1008. <dt>Default</dt>
  1009. <dd>
  1010. <code title="select2/data/select">SelectAdapter</code>
  1011. </dd>
  1012. <dt>Base</dt>
  1013. <dd>
  1014. <code title="select2/data/base">BaseAdapter</code>
  1015. </dd>
  1016. </dl>
  1017. <pre class="prettyprint linenums">
  1018. // Get the currently selected options. This is called when trying to get the
  1019. // initial selection for Select2, as well as when Select2 needs to determine
  1020. // what options within the results are selected.
  1021. //
  1022. // @param callback A function that should be called when the current selection
  1023. // has been retrieved. The first parameter to the function should be an array
  1024. // of data objects.
  1025. DataAdapter.current = function (callback) {
  1026. callback(currentData);
  1027. }
  1028. // Get a set of options that are filtered based on the parameters that have
  1029. // been passed on in.
  1030. //
  1031. // @param params An object containing any number of parameters that the query
  1032. // could be affected by. Only the core parameters will be documented.
  1033. // @param params.term A user-supplied term. This is typically the value of the
  1034. // search box, if one exists, but can also be an empty string or null value.
  1035. // @param params.page The specific page that should be loaded. This is typically
  1036. // provided when working with remote data sets, which rely on pagination to
  1037. // determine what objects should be displayed.
  1038. // @param callback The function that should be called with the queried results.
  1039. DataAdapter.query = function (params, callback) {
  1040. callback(queryiedData);
  1041. }
  1042. </pre>
  1043. <h2 id="dropdownAdapter">
  1044. Dropdown
  1045. </h2>
  1046. <p>
  1047. The dropdown adapter defines the main container that the dropdown should
  1048. be held in. <strong>It does not define any extra methods that can be used
  1049. for decorators</strong>, but it is common for decorators to attach to the
  1050. <code>render</code> and <code>position</code> methods to alter how the
  1051. dropdown is altered and positioned.
  1052. </p>
  1053. <dl class="dl-horizontal">
  1054. <dt>Key</dt>
  1055. <dd>
  1056. <code>dropdownAdapter</code>
  1057. </dd>
  1058. <dt>Default</dt>
  1059. <dd>
  1060. <code title="select2/dropdown">DropdownAdapter</code>
  1061. </dd>
  1062. </dl>
  1063. <h2 id="resultsAdapter">
  1064. Results
  1065. </h2>
  1066. <p>
  1067. The results adapter controls the list of results that the user can select
  1068. from. While the results adapter does not define any additional methods
  1069. that must be implemented, it makes extensive use of the Select2 event
  1070. system for controlling the display of results and messages.
  1071. </p>
  1072. <dl class="dl-horizontal">
  1073. <dt>Key</dt>
  1074. <dd>
  1075. <code>resultsAdapter</code>
  1076. </dd>
  1077. <dt>Default</dt>
  1078. <dd>
  1079. <code title="select2/results">ResultsAdapter</code>
  1080. </dd>
  1081. </dl>
  1082. </section>
  1083. <section id="defaults">
  1084. <div class="page-header">
  1085. <h1>Setting default options</h1>
  1086. </div>
  1087. <p>
  1088. In some cases, you need to set the default options for all instances of
  1089. Select2 in your web application. This is especially useful when you are
  1090. migrating from past versions of Select2, or you are using non-standard
  1091. options <a href="#amd">like custom AMD builds</a>. Select2 exposes the
  1092. default options through <code>$.fn.select2.defaults</code>, which allows
  1093. you to set them globally.
  1094. </p>
  1095. <p>
  1096. When setting options globally, any past defaults that have been set will
  1097. be overriden. Default options are only used when an option is requested
  1098. that has not been set during initialization.
  1099. </p>
  1100. <p>
  1101. <strong>You can set default options</strong> by calling
  1102. <code>$.fn.select2.defaults.set("key", "value")</code>. The key that is
  1103. set should take the same format as keys set using
  1104. <a href="#data-attributes">HTML <code>data-*</code> attributes</a> which
  1105. means that two dashes (<code>--</code>) will be replaced by a level of
  1106. nesting, and a single dash (<code>-</code>) will convert it to a camelCase
  1107. string.
  1108. </p>
  1109. <pre class="prettyprint linenums">
  1110. $.fn.select2.defaults.set("theme", "classic");
  1111. </pre>
  1112. <p>
  1113. <strong>You can reset the default options</strong> by calling
  1114. <code>$.fn.select2.defaults.reset()</code>.
  1115. </p>
  1116. </section>
  1117. <section id="compatibility">
  1118. <div class="page-header">
  1119. <h1>Backwards compatibility</h1>
  1120. </div>
  1121. <p>
  1122. Select2 offers limited backwards compatibility with the previously 3.5.x
  1123. release line, allowing people more efficiently transfer across releases
  1124. and get the latest features. For many of the larger changes, such as the
  1125. change in how custom data adapters work, compatibility modules were
  1126. created that will be used to assist in the upgrade process. It is not
  1127. recommended to rely on these compatibility modules, as they will not
  1128. always exist, but they make upgrading easier for major changes.
  1129. </p>
  1130. <p>
  1131. <strong>The compatibility modules are only included in the
  1132. <a href="index.html#builds-full" class="alert-link">full builds</a> of
  1133. Select2</strong>. These files end in <code>.full.js</code>, and the
  1134. compatibility modules are prefixed with <code>select2/compat</code>.
  1135. </p>
  1136. <h2 id="compat-matcher">
  1137. Simplified function for matching data objects
  1138. </h2>
  1139. <p class="alert alert-info">
  1140. <a href="announcements-4.0.html#new-matcher" class="alert-link">Added in Select2 4.0.0.</a>
  1141. This method was added to make upgrading easier from earlier versions of
  1142. Select2.
  1143. </p>
  1144. <p>
  1145. During the <a href="announcements-4.0.html">Select2 4.0.0 release</a>, the
  1146. <code>matcher</code> function was changed to allow for more complex
  1147. matching of nested objects.
  1148. </p>
  1149. <div class="row">
  1150. <div class="col-sm-6">
  1151. <dl class="dl-horizontal">
  1152. <dt>Key</dt>
  1153. <dd>
  1154. <code>matcher</code>
  1155. </dd>
  1156. <dt>Value</dt>
  1157. <dd>
  1158. A function taking a search <code>term</code> and the data object
  1159. <code>text</code>.
  1160. </dd>
  1161. </dl>
  1162. </div>
  1163. <div class="col-sm-6">
  1164. <dl class="dl-horizontal">
  1165. <dt>Adapter</dt>
  1166. <dd>
  1167. <code title="select2/compat/matcher">oldMatcher</code>
  1168. </dd>
  1169. </dl>
  1170. </div>
  1171. </div>
  1172. <p>
  1173. The <a href="examples.html#matcher">custom matcher example</a> provides a
  1174. guide for how to use this in your own application. For those upgrading
  1175. from older versions of Select2, you just need to wrap your old
  1176. <code>matcher</code> with this function to maintain compatibility.
  1177. </p>
  1178. <h2 id="initSelection">
  1179. Old initial selections with <code>initSelection</code>
  1180. </h2>
  1181. <p class="alert alert-warning">
  1182. <a href="announcements-4.0.html#removed-initselection" class="alert-link">Deprecated in Select2 4.0.</a>
  1183. This has been replaced by another option and is only available in the
  1184. <a href="index.html#builds-full" class="alert-link">full builds</a> of
  1185. Select2.
  1186. </p>
  1187. <p>
  1188. In the past, Select2 required an option called <code>initSelection</code>
  1189. that was defined whenever a custom data source was being used, allowing
  1190. for the initial selection for the component to be determined. This has
  1191. been replaced by the <code>current</code> method on the
  1192. <a href="#dataAdapter">data adapter</a>.
  1193. </p>
  1194. <div class="row">
  1195. <div class="col-sm-6">
  1196. <dl class="dl-horizontal">
  1197. <dt>Key</dt>
  1198. <dd>
  1199. <code>initSelection</code>
  1200. </dd>
  1201. <dt>Value</dt>
  1202. <dd>
  1203. A function taking a <code>callback</code>
  1204. </dd>
  1205. </dl>
  1206. </div>
  1207. <div class="col-sm-6">
  1208. <dl class="dl-horizontal">
  1209. <dt>Adapter</dt>
  1210. <dd>
  1211. <code title="select2/data/base">DataAdapter</code>
  1212. </dd>
  1213. <dt>Decorator</dt>
  1214. <dd>
  1215. <code title="select2/compat/initSelection">InitSelection</code>
  1216. </dd>
  1217. </dl>
  1218. </div>
  1219. </div>
  1220. <h2 id="query">
  1221. Querying old data with <code>query</code>
  1222. </h2>
  1223. <p class="alert alert-warning">
  1224. <a href="announcements-4.0.html#query-to-data-adapter" class="alert-link">Deprecated in Select2 4.0.</a>
  1225. This has been replaced by another option and is only available in the
  1226. <a href="index.html#builds-full" class="alert-link">full builds</a> of
  1227. Select2.
  1228. </p>
  1229. <p>
  1230. In the past, Select2 supported an option called <code>query</code> that
  1231. allowed for a custom data source to be used. This option has been replaced
  1232. by the <code>query</code> method on the
  1233. <a href="#dataAdapter">data adapter</a> and takes a very similar set of
  1234. parameters.
  1235. </p>
  1236. <div class="row">
  1237. <div class="col-sm-6">
  1238. <dl class="dl-horizontal">
  1239. <dt>Key</dt>
  1240. <dd>
  1241. <code>query</code>
  1242. </dd>
  1243. <dt>Value</dt>
  1244. <dd>
  1245. A function taking <code>params</code> (including a <code>callback</code>)
  1246. </dd>
  1247. </dl>
  1248. </div>
  1249. <div class="col-sm-6">
  1250. <dl class="dl-horizontal">
  1251. <dt>Adapter</dt>
  1252. <dd>
  1253. <code title="select2/data/base">DataAdapter</code>
  1254. </dd>
  1255. <dt>Decorator</dt>
  1256. <dd>
  1257. <code title="select2/compat/query">Query</code>
  1258. </dd>
  1259. </dl>
  1260. </div>
  1261. </div>
  1262. </section>
  1263. </div>
  1264. <script type="text/javascript">
  1265. prettyPrint();
  1266. </script>