options.html 48 KB

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