options.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 overriden. 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>
  41. Display
  42. </h2>
  43. <p>
  44. Select2 provides options that allow you to directly affect how the
  45. container that holds the current selection is displayed.
  46. </p>
  47. <h3 id="placeholder">
  48. Placeholders
  49. </h3>
  50. <p>
  51. Select2 can display a placeholder for a single-value select that will
  52. replace an option, or be shown when no options are selected for
  53. multiple-value selects. You can find an example on the
  54. <a href="examples.html#placeholders">example page</a>.
  55. </p>
  56. <div class="row">
  57. <div class="col-sm-4">
  58. <dl class="dl-horizontal">
  59. <dt>Key</dt>
  60. <dd>
  61. <code>placeholder</code>
  62. </dd>
  63. <dt>Value</dt>
  64. <dd>string or object</dd>
  65. </dl>
  66. <hr />
  67. <dl class="dl-horizontal">
  68. <dt>Adapter</dt>
  69. <dd>
  70. <code title="select2/selection/base">SelectionAdapter</code>
  71. </dd>
  72. <dt>Decorator</dt>
  73. <dd>
  74. <code title="select2/selection/placeholder">Placeholder</code>
  75. </dd>
  76. </dl>
  77. </div>
  78. <div class="col-sm-8">
  79. <div class="alert alert-warning">
  80. <strong>Heads up!</strong>
  81. Because browsers assume that the first <code>option</code> in
  82. single-value select boxes is selected, you must add an empty
  83. <code>&lt;option&gt;&lt;/option&gt;</code> tag that the placeholder
  84. should use, or it will not work.
  85. </div>
  86. </div>
  87. </div>
  88. <p>
  89. If the <strong>value is a string</strong>, the placeholder will be
  90. displayed when a <strong>blank option</strong> is used as the placeholder.
  91. The <strong>value</strong> will be the message to show to users as the
  92. placeholders.
  93. </p>
  94. <p>
  95. If the <strong>value is an object</strong>, the object should be
  96. compatible with Select2's internal objects. The <code>id</code> should
  97. be the id to look for when determining if the placeholder should be
  98. displayed. The <code>text</code> should be the placeholder to display
  99. when that option is selected.
  100. </p>
  101. <div class="alert alert-info">
  102. You <strong>pass in an object</strong> when you are using a framework that
  103. <strong>creates its own placeholder option</strong>. The
  104. <strong>id</strong> should be the same as the <code>value</code>
  105. attribute on the <code>option</code>.
  106. </div>
  107. <h3 id="language">
  108. Internationalization (Language support)
  109. </h3>
  110. <p>
  111. Messages will be displayed to users when necessary, such as when no
  112. search results were found or more characters need to be entered in order
  113. for a search to be made. These messages have been
  114. <a href="community.html#translations">translated into many languages</a>
  115. by contributors to Select2, but you can also provide your own
  116. translations.
  117. </p>
  118. <div class="row">
  119. <div class="col-sm-4">
  120. <dl class="dl-horizontal">
  121. <dt>Key</dt>
  122. <dd><code>language</code></dd>
  123. <dt>Value</dt>
  124. <dd>object or string</dd>
  125. </dl>
  126. <hr />
  127. <dl class="dl-horizontal">
  128. <dt>Module</dt>
  129. <dd>
  130. <code title="select2/translation">Translation</code>
  131. </dd>
  132. </dl>
  133. </div>
  134. <div class="col-sm-8">
  135. <p class="alert alert-warning">
  136. <strong>Heads up!</strong> When using translations provided by Select2,
  137. you must make sure to include the translation file in your page after
  138. Select2.
  139. </p>
  140. </div>
  141. </div>
  142. <p>
  143. When a string is passed in as the language, Select2 will try to resolve
  144. it into a language file. This allows you to specify your own language
  145. files, which must be defined as an AMD module. If the language file
  146. cannot be found, Select2 will assume it is a language code controlled by
  147. Select2, and it will try to load the translations for that language
  148. instead.
  149. </p>
  150. <p>
  151. You can include your own translations by providing an object similar to
  152. the one below.
  153. </p>
  154. <pre class="prettyprint">
  155. language: {
  156. // You can find all of the options in the language files provided in the
  157. // build. They all must be functions that return the string that should be
  158. // displayed.
  159. inputTooShort: function () {
  160. return "You must enter more characters...";
  161. }
  162. }
  163. </pre>
  164. <h2>
  165. Results
  166. </h2>
  167. <p>
  168. Select2 can work on many different data sets ranging from local options,
  169. the same way that a <code>&lt;select&gt;</code> typically works, from
  170. remote options where a server generates the results that users can select
  171. from.
  172. </p>
  173. <h3 id="data">
  174. Array
  175. </h3>
  176. <p>
  177. Select2 allows creating the results based on an array of data objects that
  178. is included when initializing Select2.
  179. </p>
  180. <div class="row">
  181. <div class="col-sm-6">
  182. <dl class="dl-horizontal">
  183. <dt>Key</dt>
  184. <dd><code>data</code></dd>
  185. <dt>Value</dt>
  186. <dd>array of objects</dd>
  187. </dl>
  188. </div>
  189. <div class="col-sm-6">
  190. <dl class="dl-horizontal">
  191. <dt>Adapter</dt>
  192. <dd>
  193. <code title="select2/data/array">ArrayAdapter</code>
  194. </dd>
  195. </dl>
  196. </div>
  197. </div>
  198. <p>
  199. The objects that the users can select from should be passed as an array
  200. with each object containing <code>id</code> and <code>text</code>
  201. properties.
  202. </p>
  203. <h3 id="ajax">
  204. AJAX
  205. </h3>
  206. <p>
  207. Select2 allows searching for results from remote data sources using AJAX
  208. requests.
  209. </p>
  210. <div class="row">
  211. <div class="col-sm-6">
  212. <dl class="dl-horizontal">
  213. <dt>Key</dt>
  214. <dd><code>ajax</code></dd>
  215. <dt>Value</dt>
  216. <dd>object</dd>
  217. </dl>
  218. </div>
  219. <div class="col-sm-6">
  220. <dl class="dl-horizontal">
  221. <dt>Adapter</dt>
  222. <dd>
  223. <code title="select2/data/ajax">AjaxAdapter</code>
  224. </dd>
  225. </dl>
  226. </div>
  227. </div>
  228. <p>
  229. All options passed to this option will be directly passed to the
  230. <code>$.ajax</code> function that executes AJAX requests. There are a few
  231. custom options that Select2 will intercept, allowing you to customize the
  232. request as it is being made.
  233. <pre class="prettyprint">
  234. ajax: {
  235. // The number of milliseconds to wait for the user to stop typing before
  236. // issuing the ajax request.
  237. delay: 250,
  238. // You can craft a custom url based on the parameters that are passed into the
  239. // request. This is useful if you are using a framework which has
  240. // JavaScript-based functions for generating the urls to make requests to.
  241. //
  242. // @param params The object containing the parameters used to generate the
  243. // request.
  244. // @returns The url that the request should be made to.
  245. url: function (params) {
  246. return UrlGenerator.Random();
  247. },
  248. // You can pass custom data into the request based on the parameters used to
  249. // make the request. For `GET` requests, the default method, these are the
  250. // query parameters that are appended to the url. For `POST` requests, this
  251. // is the form data that will be passed into the request. For other requests,
  252. // the data returned from here should be customized based on what jQuery and
  253. // your server are expecting.
  254. //
  255. // @param params The object containing the parameters used to generate the
  256. // request.
  257. // @returns Data to be directly passed into the request.
  258. data: function (params) {
  259. var queryParameters = {
  260. q: params.term
  261. }
  262. return queryParameters;
  263. },
  264. // You can modify the results that are returned from the server, allowing you
  265. // to make last-minute changes to the data, or find the correct part of the
  266. // response to pass to Select2. Keep in mind that results should be passed as
  267. // an array of objects.
  268. //
  269. // @param data The data as it is returned directly by jQuery.
  270. // @returns An array of objects that will be rendered by Select2.
  271. processResults: function (data) {
  272. return data;
  273. }
  274. }
  275. </pre>
  276. </p>
  277. <h3 id="tags">
  278. Tags
  279. </h3>
  280. <p>
  281. Users can create their own options based on the text that they have
  282. entered.
  283. </p>
  284. <div class="row">
  285. <div class="col-sm-6">
  286. <dl class="dl-horizontal">
  287. <dt>Key</dt>
  288. <dd><code>tags</code></dd>
  289. <dt>Value</dt>
  290. <dd>boolean / array of objects</dd>
  291. </dl>
  292. </div>
  293. <div class="col-sm-6">
  294. <dl class="dl-horizontal">
  295. <dt>Adapter</dt>
  296. <dd>
  297. <code title="select2/data/base">DataAdapter</code>
  298. </dd>
  299. <dt>Decorator</dt>
  300. <dd>
  301. <code title="select2/data/tags">Tags</code>
  302. </dd>
  303. </dl>
  304. </div>
  305. </div>
  306. <p>
  307. If the <code>tags</code> option is passed into Select2, if a user types
  308. anything into the search box which doesn't already exist, it will be
  309. displayed at the top and the user will be able to select it.
  310. </p>
  311. <p>
  312. <strong>For backwards compatibility</strong>, if an array of objects is
  313. passed in with the <code>tags</code> option, the options will be
  314. automatically created and the user will be able to select from them.
  315. This is the <strong>same as how <a href="#data">array data</a>
  316. works</strong>, and has similar limitations.
  317. </p>
  318. </section>
  319. <section id="adapters">
  320. <div class="page-header">
  321. <h1>Adapters</h1>
  322. </div>
  323. <p>
  324. Select2 allows plugins to add additional functionality through the core
  325. adapters. You can change almost anything involving the way Select2 works
  326. to the way Select2 interacts with the page by modifying the core adapters.
  327. Most third-party plugins should provide decorators (used to wrap adapters)
  328. and custom adpaters that you can use.
  329. </p>
  330. <p>
  331. Each adapter contains a set of methods which will must always be defined.
  332. Along with the global methods that all adapters must implement, these
  333. methods must be implemented.
  334. </p>
  335. <h2>
  336. All adapters
  337. </h2>
  338. <p>
  339. All adapters must implement a set of methods that Select2 will use to
  340. display them and bind any internal events.
  341. </p>
  342. <pre class="prettyprint linenums">
  343. // The basic HTML that should be rendered by Select2. A jQuery or DOM element
  344. // should be returned, which will automatically be placed by Select2 within the
  345. // DOM.
  346. //
  347. // @returns A jQuery or DOM element that contains any elements that must be
  348. // rendered by Select2.
  349. Adapter.render = function () {
  350. return $jq;
  351. };
  352. // Bind to any Select2 or DOM events.
  353. //
  354. // @param container The Select2 object that is bound to the jQuery element. You
  355. // can listen to Select2 events with `on` and trigger Select2 events using the
  356. // `trigger` method.
  357. // @param $container The jQuery DOM node that all default adapters will be
  358. // rendered within.
  359. Adapter.bind = function (container, $container) { };
  360. // Position the DOM element within the Select2 DOM container, or in another
  361. // place. This allows adapters to be located outside of the Select2 DOM,
  362. // such as at the end of the document or in a specific place within the Select2
  363. // DOM node.
  364. //
  365. // Note: This method is not called on data adapters.
  366. //
  367. // @param $rendered The rendered DOM element that was returned from the call to
  368. // `render`. This may have been modified by Select2, but the root element
  369. // will always be the same.
  370. // @param $defaultContainer The default container that Select2 will typically
  371. // place the rendered DOM element within. For most adapters, this is the
  372. // Select2 DOM element.
  373. Adapter.position = function ($rendered, $defaultContainer) { };
  374. // Destroy any events or DOM elements that have been created.
  375. // This is called when `select2("destroy")` is called on an element.
  376. Adapter.destroy = function () { };
  377. </pre>
  378. <h2 id="selectionAdapter">
  379. Container (selection)
  380. </h2>
  381. <p>
  382. The selection is what is shown to the user as a replacement of the
  383. standard <code>&lt;select&gt;</code> box. It controls the display of the
  384. selection option(s), as well anything else that needs to be embedded
  385. within the container, such as a search box.
  386. </p>
  387. <dl class="dl-horizontal">
  388. <dt>Key</dt>
  389. <dd>
  390. <code>selectionAdapter</code>
  391. </dd>
  392. <dt>Default</dt>
  393. <dd>
  394. <code title="select2/selection/single">SingleSelection</code> or
  395. <code title="select2/selection/multiple">MultipleSelection</code>
  396. </dd>
  397. <dt>Base</dt>
  398. <dd>
  399. <code title="select2/selection/base">BaseSelection</code>
  400. </dd>
  401. </dl>
  402. <pre class="prettyprint linenums">
  403. // Update the selected data.
  404. //
  405. // @param data An array of data objects that have been generated by the data
  406. // adapter. If no objects should be selected, an empty array will be passed.
  407. //
  408. // Note: An array will always be passed into this method, even if Select2 is
  409. // attached to a source which only accepts a single selection.
  410. SelectionAdapter.update = function (data) { };
  411. </pre>
  412. <h2 id="dataAdapter">
  413. Data set
  414. </h2>
  415. <p>
  416. The data set is what Select2 uses to generate the possible results that
  417. can be selected, as well as the currently selected results.
  418. </p>
  419. <dl class="dl-horizontal">
  420. <dt>Key</dt>
  421. <dd>
  422. <code>dataAdapter</code>
  423. </dd>
  424. <dt>Default</dt>
  425. <dd>
  426. <code title="select2/data/select">SelectAdapter</code>
  427. </dd>
  428. <dt>Base</dt>
  429. <dd>
  430. <code title="select2/data/base">BaseAdapter</code>
  431. </dd>
  432. </dl>
  433. <pre class="prettyprint linenums">
  434. // Get the currently selected options. This is called when trying to get the
  435. // initial selection for Select2, as well as when Select2 needs to determine
  436. // what options within the results are selected.
  437. //
  438. // @param callback A function that should be called when the current selection
  439. // has been retrieved. The first paramter to the function should be an array
  440. // of data objects.
  441. DataAdapter.current = function (callback) {
  442. callback(currentData);
  443. }
  444. // Get a set of options that are filtered based on the parameters that have
  445. // been passed on in.
  446. //
  447. // @param params An object containing any number of parameters that the query
  448. // could be affected by. Only the core parameters will be documented.
  449. // @param params.term A user-supplied term. This is typically the value of the
  450. // search box, if one exists, but can also be an empty string or null value.
  451. // @param params.page The specific page that should be loaded. This is typically
  452. // provided when working with remote data sets, which rely on pagination to
  453. // determine what objects should be displayed.
  454. // @param callback The function that should be called with the queried results.
  455. DataAdapter.query = function (params, callback) {
  456. callback(queryiedData);
  457. }
  458. </pre>
  459. <h2 id="dropdownAdapter">
  460. Dropdown
  461. </h2>
  462. <p>
  463. The dropdown adapter defines the main container that the dropdown should
  464. be held in. <strong>It does not define any extra methods that can be used
  465. for decorators</strong>, but it is common for decorators to attach to the
  466. <code>render</code> and <code>position</code> methods to alter how the
  467. dropdown is altered and positioned.
  468. </p>
  469. <dl class="dl-horizontal">
  470. <dt>Key</dt>
  471. <dd>
  472. <code>dropdownAdapter</code>
  473. </dd>
  474. <dt>Default</dt>
  475. <dd>
  476. <code title="select2/dropdown">DropdownAdapter</code>
  477. </dd>
  478. </dl>
  479. <h2 id="resultsAdapter">
  480. Results
  481. </h2>
  482. <p>
  483. The results adapter controls the list of results that the user can select
  484. from. While the results adapter does not define any additional methods
  485. that must be implemented, it makes extensive use of the Select2 event
  486. system for cotrolling the display of results and messages.
  487. </p>
  488. <dl class="dl-horizontal">
  489. <dt>Key</dt>
  490. <dd>
  491. <code>resultsAdapter</code>
  492. </dd>
  493. <dt>Default</dt>
  494. <dd>
  495. <code title="select2/results">ResultsAdapter</code>
  496. </dd>
  497. </dl>
  498. </section>
  499. </div>
  500. <script type="text/javascript">
  501. prettyPrint();
  502. </script>