options.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. <h2>
  19. Display
  20. </h2>
  21. <h3 id="placeholder">
  22. Placeholders
  23. </h3>
  24. <p>
  25. Select2 can display a placeholder for a single-value select that will
  26. replace an option, or be shown when no options are selected for
  27. multiple-value selects. You can find an example on the
  28. <a href="examples.html#placeholders">example page</a>.
  29. </p>
  30. <div class="row">
  31. <div class="col-sm-4">
  32. <dl class="dl-horizontal">
  33. <dt>Key</dt>
  34. <dd>
  35. <code>placeholder</code>
  36. </dd>
  37. <dt>Value</dt>
  38. <dd>string or object</dd>
  39. </dl>
  40. <hr />
  41. <dl class="dl-horizontal">
  42. <dt>Adapter</dt>
  43. <dd>
  44. <code title="select2/selection/base">SelectionAdapter</code>
  45. </dd>
  46. <dt>Decorator</dt>
  47. <dd>
  48. <code title="select2/selection/placeholder">Placeholder</code>
  49. </dd>
  50. </dl>
  51. </div>
  52. <div class="col-sm-8">
  53. <div class="alert alert-warning">
  54. <strong>Heads up!</strong>
  55. Because browsers assume that the first <code>option</code> in
  56. single-value select boxes is selected, you must add an empty
  57. <code>&lt;option&gt;&lt;/option&gt;</code> tag that the placeholder
  58. should use, or it will not work.
  59. </div>
  60. </div>
  61. </div>
  62. <p>
  63. If the <strong>value is a string</strong>, the placeholder will be
  64. displayed when a <strong>blank option</strong> is used as the placeholder.
  65. The <strong>value</strong> will be the message to show to users as the
  66. placeholders.
  67. </p>
  68. <p>
  69. If the <strong>value is an object</strong>, the object should be
  70. compatible with Select2's internal objects. The <code>id</code> should
  71. be the id to look for when determining if the placeholder should be
  72. displayed. The <code>text</code> should be the placeholder to display
  73. when that option is selected.
  74. </p>
  75. <div class="alert alert-info">
  76. You <strong>pass in an object</strong> when you are using a framework that
  77. <strong>creates its own placeholder option</strong>. The
  78. <strong>id</strong> should be the same as the <code>value</code>
  79. attribute on the <code>option</code>.
  80. </div>
  81. <h3 id="translation">
  82. Internationalization (Language support)
  83. </h3>
  84. <p>
  85. Messages will be displayed to users when necessary, such as when no
  86. search results were found or more characters need to be entered in order
  87. for a search to be made. These messages have been
  88. <a href="community.html#translations">translated into many languages</a>
  89. by contributors to Select2, but you can also provide your own
  90. translations.
  91. </p>
  92. <div class="row">
  93. <div class="col-sm-4">
  94. <dl class="dl-horizontal">
  95. <dt>Key</dt>
  96. <dd><code>language</code></dd>
  97. <dt>Value</dt>
  98. <dd>object or string</dd>
  99. </dl>
  100. <hr />
  101. <dl class="dl-horizontal">
  102. <dt>Module</dt>
  103. <dd>
  104. <code title="select2/translation">Translation</code>
  105. </dd>
  106. </dl>
  107. </div>
  108. <div class="col-sm-8">
  109. <p class="alert alert-warning">
  110. <strong>Heads up!</strong> When using translations provided by Select2,
  111. you must make sure to include the translation file in your page after
  112. Select2.
  113. </p>
  114. </div>
  115. </div>
  116. <p>
  117. When a string is passed in as the language, Select2 will try to resolve
  118. it into a language file. This allows you to specify your own language
  119. files, which must be defined as an AMD module. If the language file
  120. cannot be found, Select2 will assume it is a language code controlled by
  121. Select2, and it will try to load the translations for that language
  122. instead.
  123. </p>
  124. <p>
  125. You can include your own translations by providing an object similar to
  126. the one below.
  127. </p>
  128. <pre class="prettyprint">
  129. language: {
  130. // You can find all of the options in the language files provided in the
  131. // build. They all must be functions that return the string that should be
  132. // displayed.
  133. inputTooShort: function () {
  134. return "You must enter more characters...";
  135. }
  136. }
  137. </pre>
  138. <h2>
  139. Results
  140. </h2>
  141. <p>
  142. Select2 can work on many different data sets ranging from local options,
  143. the same way that a <code>&lt;select&gt;</code> typically works, from
  144. remove options where a server generates the results that users can select
  145. from.
  146. </p>
  147. <h3 id="data">
  148. Array
  149. </h3>
  150. <p>
  151. Select2 allows creating the results based on an array of data objects that
  152. is included when initializing Select2.
  153. </p>
  154. <div class="row">
  155. <div class="col-sm-6">
  156. <dl class="dl-horizontal">
  157. <dt>Key</dt>
  158. <dd><code>data</code></dd>
  159. <dt>Value</dt>
  160. <dd>array of objects</dd>
  161. </dl>
  162. </div>
  163. <div class="col-sm-6">
  164. <dl class="dl-horizontal">
  165. <dt>Adapter</dt>
  166. <dd>
  167. <code title="select2/data/array">ArrayAdapter</code>
  168. </dd>
  169. </dl>
  170. </div>
  171. </div>
  172. <p>
  173. The objects that the users can select from should be passed as an array
  174. with each object containing <code>id</code> and <code>text</code>
  175. properties.
  176. </p>
  177. <h3 id="ajax">
  178. AJAX
  179. </h3>
  180. <p>
  181. Select2 allows searching for results from remote data sources using AJAX
  182. requests.
  183. </p>
  184. <div class="row">
  185. <div class="col-sm-6">
  186. <dl class="dl-horizontal">
  187. <dt>Key</dt>
  188. <dd><code>ajax</code></dd>
  189. <dt>Value</dt>
  190. <dd>object</dd>
  191. </dl>
  192. </div>
  193. <div class="col-sm-6">
  194. <dl class="dl-horizontal">
  195. <dt>Adapter</dt>
  196. <dd>
  197. <code title="select2/data/ajax">AjaxAdapter</code>
  198. </dd>
  199. </dl>
  200. </div>
  201. </div>
  202. <p>
  203. All options passed to this option will be directly passed to the
  204. <code>$.ajax</code> function that executes AJAX requests. There are a few
  205. custom options that Select2 will intercept, allowing you to customize the
  206. request as it is being made.
  207. <pre class="prettyprint">
  208. ajax: {
  209. // The number of milliseconds to wait for the user to stop typing before
  210. // issuing the ajax request.
  211. delay: 250,
  212. // You can craft a custom url based on the parameters that are passed into the
  213. // request. This is useful if you are using a framework which has
  214. // JavaScript-based functions for generating the urls to make requests to.
  215. //
  216. // @param params The object containing the parameters used to generate the
  217. // request.
  218. // @returns The url that the request should be made to.
  219. url: function (params) {
  220. return UrlGenerator.Random();
  221. },
  222. // You can pass custom data into the request based on the parameters used to
  223. // make the request. For `GET` requests, the default method, these are the
  224. // query parameters that are appended to the url. For `POST` requests, this
  225. // is the form data that will be passed into the request. For other requests,
  226. // the data returned from here should be customized based on what jQuery and
  227. // your server are expecting.
  228. //
  229. // @param params The object containing the parameters used to generate the
  230. // request.
  231. // @returns Data to be directly passed into the request.
  232. data: function (params) {
  233. var queryParameters = {
  234. q: params.term
  235. }
  236. return queryParameters;
  237. },
  238. // You can modify the results that are returned from the server, allowing you
  239. // to make last-minute changes to the data, or find the correct part of the
  240. // response to pass to Select2. Keep in mind that results should be passed as
  241. // an array of objects.
  242. //
  243. // @param data The data as it is returned directly by jQuery.
  244. // @returns An array of objects that will be rendered by Select2.
  245. processResults: function (data) {
  246. return data;
  247. }
  248. }
  249. </pre>
  250. </p>
  251. <h3 id="tags">
  252. Tags
  253. </h3>
  254. <p>
  255. Users can create their own options based on the text that they have
  256. entered.
  257. </p>
  258. <div class="row">
  259. <div class="col-sm-6">
  260. <dl class="dl-horizontal">
  261. <dt>Key</dt>
  262. <dd><code>tags</code></dd>
  263. <dt>Value</dt>
  264. <dd>boolean / array of objects</dd>
  265. </dl>
  266. </div>
  267. <div class="col-sm-6">
  268. <dl class="dl-horizontal">
  269. <dt>Adapter</dt>
  270. <dd>
  271. <code title="select2/data/base">DataAdapter</code>
  272. </dd>
  273. <dt>Decorator</dt>
  274. <dd>
  275. <code title="select2/data/tags">Tags</code>
  276. </dd>
  277. </dl>
  278. </div>
  279. </div>
  280. <p>
  281. If the <code>tags</code> option is passed into Select2, if a user types
  282. anything into the search box which doesn't already exist, it will be
  283. displayed at the top and the user will be able to select it.
  284. </p>
  285. <p>
  286. <strong>For backwards compatibility</strong>, if an array of objects is
  287. passed in with the <code>tags</code> option, the options will be
  288. automatically created and the user will be able to select from them.
  289. This is the <strong>same as how <a href="#data">array data</a>
  290. works</strong>, and has similar limitations.
  291. </p>
  292. </section>
  293. <section id="adapters">
  294. <div class="page-header">
  295. <h1>Adapters</h1>
  296. </div>
  297. </section>
  298. </div>
  299. <script type="text/javascript">
  300. prettyPrint();
  301. </script>