examples.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. ---
  2. layout: default
  3. title: Examples - Select2
  4. slug: examples
  5. ---
  6. <script type="text/javascript" src="dist/js/i18n/es.js"></script>
  7. <div class="container">
  8. <section id="basic" class="row">
  9. <div class="col-md-4">
  10. <h1>The basics</h1>
  11. <p>
  12. Select2 can take a regular select box like this...
  13. </p>
  14. <p>
  15. <select class="js-states form-control"></select>
  16. </p>
  17. <p>
  18. and turn it into this...
  19. </p>
  20. <p>
  21. <select class="js-example-basic-single js-states form-control"></select>
  22. </p>
  23. </div>
  24. <div class="col-md-8">
  25. <h2>Example code</h2>
  26. <pre class="code" data-fill-from=".js-code-basic"></pre>
  27. <script type="text/x-example-code" class="js-code-basic">
  28. $(document).ready(function() {
  29. $(".js-example-basic-single").select2();
  30. });
  31. <select class="js-example-basic-single">
  32. <option value="AL">Alabama</option>
  33. ...
  34. <option value="WY">Wyoming</option>
  35. </select>
  36. </script>
  37. </div>
  38. </section>
  39. <section id="multiple" class="row">
  40. <div class="col-md-4">
  41. <h1>Multiple select boxes</h1>
  42. <p>
  43. <select class="js-states" multiple="multiple"></select>
  44. </p>
  45. <p>
  46. Select2 also supports multi-value select boxes. The select below is declared with the <code>multiple</code> attribute.
  47. </p>
  48. <p>
  49. <select class="js-example-basic-multiple js-states form-control" multiple="multiple"></select>
  50. </p>
  51. </div>
  52. <div class="col-md-8">
  53. <h2>Example code</h2>
  54. <pre data-fill-from=".js-code-multiple"></pre>
  55. <script type="text/x-example-code" class="js-code-multiple">
  56. $(".js-example-basic-multiple").select2();
  57. <select class="js-example-basic-multiple" multiple="multiple">
  58. <option value="AL">Alabama</option>
  59. ...
  60. <option value="WY">Wyoming</option>
  61. </select>
  62. </script>
  63. </div>
  64. </section>
  65. <section id="placeholders" class="row">
  66. <div class="col-md-4">
  67. <h1>Placeholders</h1>
  68. <p>
  69. A placeholder value can be defined and will be displayed until a selection is made.
  70. </p>
  71. <p>
  72. <select class="js-example-placeholder-single js-states form-control">
  73. <option></option>
  74. </select>
  75. </p>
  76. <p>
  77. This works for multiple select boxes as well.
  78. </p>
  79. <p>
  80. <select class="js-example-placeholder-multiple js-states form-control" multiple="multiple"></select>
  81. </p>
  82. </div>
  83. <div class="col-md-8">
  84. <h2>Example code</h2>
  85. <pre data-fill-from=".js-code-placeholder"></pre>
  86. <script type="text/x-example-code" class="js-code-placeholder">
  87. $(".js-example-placeholder-single").select2({
  88. placeholder: "Select a state"
  89. });
  90. $(".js-example-placeholder-multiple").select2({
  91. placeholder: "Select a state"
  92. });
  93. </script>
  94. </div>
  95. </section>
  96. <section id="data-array" class="row">
  97. <div class="col-md-4">
  98. <h1>Loading array data</h1>
  99. <p>
  100. Select2 provides a way to load the data from a local array.
  101. </p>
  102. <p>
  103. <select class="js-example-data-array form-control"></select>
  104. </p>
  105. <p>
  106. You can provide initial selections with array data by providing the
  107. option tag for the selected values, similar to how it would be done for
  108. a standard select.
  109. </p>
  110. <p>
  111. <select class="js-example-data-array-selected form-control">
  112. <option value="2" selected="selected">duplicate</option>
  113. </select>
  114. </p>
  115. </div>
  116. <div class="col-md-8">
  117. <h2>Example code</h2>
  118. <pre data-fill-from=".js-code-data-array"></pre>
  119. <script type="text/x-example-code" class="js-code-data-array">
  120. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  121. $(".js-example-data-array").select2({
  122. data: data
  123. })
  124. $(".js-example-data-array-selected").select2({
  125. data: data
  126. })
  127. <select class="js-example-data-array-selected"></select>
  128. <select class="js-example-data-array-selected">
  129. <option value="2" selected="selected">duplicate</option>
  130. </select>
  131. </script>
  132. </div>
  133. </section>
  134. <section id="data-ajax" class="row">
  135. <div class="col-md-12">
  136. <h1>Loading remote data</h1>
  137. <p>
  138. Select2 comes with AJAX support built in, using jQuery's AJAX methods.
  139. </p>
  140. <p>
  141. <select class="js-example-data-ajax form-control">
  142. <option value="ivaynberg/select2" selected="selected">ivaynberg/select2</option>
  143. </select>
  144. </p>
  145. <pre data-fill-from=".js-code-data-ajax"></pre>
  146. <p>
  147. Select2 will pass any options in the <code>ajax</code> object to
  148. jQuery's <code>$.ajax</code> function.
  149. </p>
  150. <script type="text/x-example-code" class="js-code-data-ajax">
  151. </script>
  152. </div>
  153. </section>
  154. <section id="disabled-results" class="row">
  155. <div class="col-md-4">
  156. <h1>Disabled results</h1>
  157. <p>
  158. Select2 will correctly handled disabled results, both with data coming
  159. from a standard select (when the <code>disabled</code> attribute is set)
  160. and from remote sources, where the object has
  161. <code>disabled: true</code> set.
  162. </p>
  163. <p>
  164. <select class="js-example-disabled-results form-control">
  165. <option value="one">First</option>
  166. <option value="two" disabled="disabled">Second (disabled)</option>
  167. <option value="three">Third</option>
  168. </select>
  169. </p>
  170. </div>
  171. <div class="col-md-8">
  172. <h2>Example code</h2>
  173. <pre data-fill-from=".js-code-disabled-results"></pre>
  174. <script type="text/x-example-code" class="js-code-disabled-results">
  175. <select class="js-example-disabled-results">
  176. <option value="one">First</option>
  177. <option value="two" disabled="disabled">Second (disabled)</option>
  178. <option value="three">Third</option>
  179. </select>
  180. </script>
  181. </div>
  182. </section>
  183. <section id="tagss" class="row">
  184. <div class="col-md-4">
  185. <h1>Programmatic access</h1>
  186. <p>
  187. Select2 supports methods that allow programmatic control of the
  188. component.
  189. </p>
  190. <p>
  191. <button class="js-programmatic-open btn btn-success">
  192. Open
  193. </button>
  194. <button class="js-programmatic-close btn btn-success">
  195. Close
  196. </button>
  197. <button class="js-programmatic-init btn btn-danger">
  198. Init
  199. </button>
  200. <button class="js-programmatic-destroy btn btn-danger">
  201. Destroy
  202. </button>
  203. </p>
  204. <p>
  205. <select class="js-example-programmatic js-states form-control"></select>
  206. </p>
  207. </div>
  208. <div class="col-md-8">
  209. <h2>Example code</h2>
  210. <pre data-fill-from=".js-code-programmatic"></pre>
  211. <script type="text/javascript" class="js-code-programmatic">
  212. var $example = $(".js-example-programmatic");
  213. $(".js-programmatic-open").on("click", function () { $example.select2("open"); });
  214. $(".js-programmatic-close").on("click", function () { $example.select2("close"); });
  215. $(".js-programmatic-init").on("click", function () { $example.select2(); });
  216. $(".js-programmatic-destroy").on("click", function () { $example.select2("destroy"); });
  217. </script>
  218. </div>
  219. </section>
  220. <section id="tags" class="row">
  221. <div class="col-md-4">
  222. <h1>Tagging support</h1>
  223. <p>
  224. Select2 can be used to quickly set up fields used for tagging.
  225. </p>
  226. <p>
  227. <select class="js-example-tags form-control">
  228. <option value="one">First</option>
  229. <option value="other">Other</option>
  230. </select>
  231. </p>
  232. <p>
  233. Note that when tagging is enabled the user can select from pre-existing
  234. options or create a new tag by picking the first choice, which is what
  235. the user has typed into the search box so far.
  236. </p>
  237. </div>
  238. <div class="col-md-8">
  239. <h2>Example code</h2>
  240. <pre data-fill-from=".js-code-tags"></pre>
  241. <script type="text/x-example-code" class="js-code-tags">
  242. $(".js-example-tags").select2({
  243. tags: true
  244. })
  245. </script>
  246. </div>
  247. </section>
  248. <section id="matcher" class="row">
  249. <div class="col-md-4">
  250. <h1>Custom matcher</h1>
  251. <p>
  252. Unlike other dropdowns on this page, this one matches options only if
  253. the term appears in the beginning of the string as opposed to anywhere:
  254. </p>
  255. <p>
  256. <select class="js-example-matcher-start js-states form-control"></select>
  257. </p>
  258. <p>
  259. This custom matcher uses a
  260. <a href="compatibility.html">compatibility module</a> that is only
  261. bundled in the
  262. <a href="getting-started#versions">full version of Select2</a>. You also
  263. have the option of using a
  264. <a href="options.html#matcher">more complex matcher</a>.
  265. </p>
  266. </div>
  267. <div class="col-md-8">
  268. <h2>Example code</h2>
  269. <pre data-fill-from=".js-code-matcher-start"></pre>
  270. <script type="text/x-example-code" class="js-code-matcher-start">
  271. function matchStart (term, text) {
  272. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  273. return true;
  274. }
  275. return false;
  276. }
  277. $.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  278. $(".js-example-matcher-start").select2({
  279. matcher: oldMatcher(matchStart)
  280. })
  281. });
  282. </script>
  283. </div>
  284. </section>
  285. <section id="diacritics" class="row">
  286. <div class="col-md-4">
  287. <h1>Diacritics support</h1>
  288. <p>
  289. Select2's default matcher will ignore diacritics, making it easier for
  290. users to filter results in international selects. Type "aero" into the
  291. select below.
  292. </p>
  293. <p>
  294. <select class="js-example-diacritics form-control">
  295. <option>Aeróbics</option>
  296. <option>Aeróbics en Agua</option>
  297. <option>Aerografía</option>
  298. <option>Aeromodelaje</option>
  299. <option>Águilas</option>
  300. <option>Ajedrez</option>
  301. <option>Ala Delta</option>
  302. <option>Álbumes de Música</option>
  303. <option>Alusivos</option>
  304. <option>Análisis de Escritura a Mano</option>
  305. </select>
  306. </p>
  307. </div>
  308. <div class="col-md-8">
  309. <h2>Example code</h2>
  310. <pre data-fill-from=".js-code-language"></pre>
  311. <script type="text/x-example-code" class="js-code-language">
  312. $(".js-example-diacritics").select2();
  313. </script>
  314. </div>
  315. </section>
  316. <section id="language" class="row">
  317. <div class="col-md-4">
  318. <h1>Multiple languages</h1>
  319. <p>
  320. Select2 supports displaying the messages in different languages, as well
  321. as provding your own
  322. <a href="options.html#language">custom messages</a>
  323. that can be displayed.
  324. </p>
  325. <p>
  326. <select class="js-example-language js-states form-control">
  327. </select>
  328. </p>
  329. <p>
  330. The language does not have to be defined when Select2 is being
  331. initialized, but instead can be defined in the <code>[lang]</code>
  332. attribute of any parent elements as <code>[lang="es"]</code>.
  333. </p>
  334. </div>
  335. <div class="col-md-8">
  336. <h2>Example code</h2>
  337. <pre data-fill-from=".js-code-language"></pre>
  338. <script type="text/x-example-code" class="js-code-language">
  339. $(".js-example-language").select2({
  340. language: "es"
  341. });
  342. </script>
  343. </div>
  344. </section>
  345. <section id="themes" class="row">
  346. <div class="col-md-4">
  347. <h1>Theme support</h1>
  348. <p>
  349. Select2 supports custom themes using the
  350. <a href="options.html#theme">theme option</a>
  351. so you can style Select2 to match the rest of your application.
  352. </p>
  353. <p>
  354. <select class="js-example-theme-single js-states form-control">
  355. </select>
  356. </p>
  357. <p>
  358. These are using the <code>classic</code> theme, which matches the old
  359. look of Select2.
  360. </p>
  361. <p>
  362. <select class="js-example-theme-multiple js-states form-control" multiple="multiple"></select>
  363. </p>
  364. </div>
  365. <div class="col-md-8">
  366. <h2>Example code</h2>
  367. <pre data-fill-from=".js-code-theme"></pre>
  368. <script type="text/javascript" class="js-code-theme">
  369. $(".js-example-theme-single").select2({
  370. theme: "classic"
  371. });
  372. $(".js-example-theme-multiple").select2({
  373. theme: "classic"
  374. });
  375. </script>
  376. </div>
  377. </section>
  378. </div>
  379. <select class="js-source-states" style="display: none;">
  380. <optgroup label="Alaskan/Hawaiian Time Zone">
  381. <option value="AK">Alaska</option>
  382. <option value="HI">Hawaii</option>
  383. </optgroup>
  384. <optgroup label="Pacific Time Zone">
  385. <option value="CA">California</option>
  386. <option value="NV">Nevada</option>
  387. <option value="OR">Oregon</option>
  388. <option value="WA">Washington</option>
  389. </optgroup>
  390. <optgroup label="Mountain Time Zone">
  391. <option value="AZ">Arizona</option>
  392. <option value="CO">Colorado</option>
  393. <option value="ID">Idaho</option>
  394. <option value="MT">Montana</option>
  395. <option value="NE">Nebraska</option>
  396. <option value="NM">New Mexico</option>
  397. <option value="ND">North Dakota</option>
  398. <option value="UT">Utah</option>
  399. <option value="WY">Wyoming</option>
  400. </optgroup>
  401. <optgroup label="Central Time Zone">
  402. <option value="AL">Alabama</option>
  403. <option value="AR">Arkansas</option>
  404. <option value="IL">Illinois</option>
  405. <option value="IA">Iowa</option>
  406. <option value="KS">Kansas</option>
  407. <option value="KY">Kentucky</option>
  408. <option value="LA">Louisiana</option>
  409. <option value="MN">Minnesota</option>
  410. <option value="MS">Mississippi</option>
  411. <option value="MO">Missouri</option>
  412. <option value="OK">Oklahoma</option>
  413. <option value="SD">South Dakota</option>
  414. <option value="TX">Texas</option>
  415. <option value="TN">Tennessee</option>
  416. <option value="WI">Wisconsin</option>
  417. </optgroup>
  418. <optgroup label="Eastern Time Zone">
  419. <option value="CT">Connecticut</option>
  420. <option value="DE">Delaware</option>
  421. <option value="FL">Florida</option>
  422. <option value="GA">Georgia</option>
  423. <option value="IN">Indiana</option>
  424. <option value="ME">Maine</option>
  425. <option value="MD">Maryland</option>
  426. <option value="MA">Massachusetts</option>
  427. <option value="MI">Michigan</option>
  428. <option value="NH">New Hampshire</option>
  429. <option value="NJ">New Jersey</option>
  430. <option value="NY">New York</option>
  431. <option value="NC">North Carolina</option>
  432. <option value="OH">Ohio</option>
  433. <option value="PA">Pennsylvania</option>
  434. <option value="RI">Rhode Island</option>
  435. <option value="SC">South Carolina</option>
  436. <option value="VT">Vermont</option>
  437. <option value="VA">Virginia</option>
  438. <option value="WV">West Virginia</option>
  439. </optgroup>
  440. </select>
  441. <script type="text/javascript">
  442. var $states = $(".js-source-states");
  443. var statesOptions = $states.html();
  444. $states.remove();
  445. $(".js-states").append(statesOptions);
  446. $("[data-fill-from]").each(function () {
  447. var $this = $(this);
  448. var codeContainer = $this.data("fill-from");
  449. var $container = $(codeContainer);
  450. var code = $.trim($container.html());
  451. $this.text(code);
  452. $this.addClass("prettyprint linenums");
  453. });
  454. prettyPrint();
  455. $.fn.select2.amd.require(
  456. ["select2/core", "select2/utils", "select2/compat/matcher"],
  457. function (Select2, Utils, oldMatcher) {
  458. var $basicSingle = $(".js-example-basic-single");
  459. var $basicMultiple = $(".js-example-basic-multiple");
  460. var $placeholderSingle = $(".js-example-placeholder-single");
  461. var $placeholderMultiple = $(".js-example-placeholder-multiple");
  462. var $dataArray = $(".js-example-data-array");
  463. var $dataArraySelected = $(".js-example-data-array-selected");
  464. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  465. var $ajax = $(".js-example-data-ajax");
  466. var $disabledResults = $(".js-example-disabled-results");
  467. var $tags = $(".js-example-tags");
  468. var $matcherStart = $('.js-example-matcher-start');
  469. var $diacritics = $(".js-example-diacritics");
  470. var $language = $(".js-example-language");
  471. $basicSingle.select2();
  472. $basicMultiple.select2()
  473. $placeholderSingle.select2({
  474. placeholder: "Select a state"
  475. });
  476. $placeholderMultiple.select2({
  477. placeholder: "Select a state"
  478. });
  479. $dataArray.select2({
  480. data: data
  481. });
  482. $dataArraySelected.select2({
  483. data: data
  484. });
  485. $ajax.select2({
  486. ajax: {
  487. url: "https://api.github.com/search/repositories",
  488. dataType: 'json',
  489. delay: 250,
  490. data: function (params) {
  491. return {
  492. q: params.term, // search term
  493. page: params.page
  494. };
  495. },
  496. processResults: function (data, page) {
  497. // parse the results into the format expected by Select2.
  498. // since we are using custom formatting functions we do not need to
  499. // alter the remote JSON data
  500. return data.items;
  501. },
  502. cache: true
  503. },
  504. minimumInputLength: 1,
  505. templateResult: function (repo) {
  506. if (repo.loading) return repo.text;
  507. var markup = '<div class="clearfix">' +
  508. '<div class="col-sm-1">' +
  509. '<img src="' + repo.owner.avatar_url + '" style="max-width: 100%" />' +
  510. '</div>' +
  511. '<div clas="col-sm-10">' +
  512. '<div class="clearfix">' +
  513. '<div class="col-sm-6">' + repo.full_name + '</div>' +
  514. '<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + repo.forks_count + '</div>' +
  515. '<div class="col-sm-2"><i class="fa fa-star"></i> ' + repo.stargazers_count + '</div>' +
  516. '</div>';
  517. if (repo.description) {
  518. markup += '<div>' + repo.description + '</div>';
  519. }
  520. markup += '</div></div>';
  521. return markup;
  522. },
  523. templateSelection: function (repo) {
  524. return repo.full_name || repo.text;
  525. }
  526. });
  527. $disabledResults.select2();
  528. $(".js-example-programmatic").select2();
  529. $tags.select2({
  530. tags: ['red', 'blue', 'green']
  531. });
  532. function matchStart (term, text) {
  533. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  534. return true;
  535. }
  536. return false;
  537. }
  538. $matcherStart.select2({
  539. matcher: oldMatcher(matchStart)
  540. });
  541. $diacritics.select2();
  542. $language.select2({
  543. language: "es"
  544. });
  545. });
  546. </script>