examples.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. </div>
  346. <select class="js-source-states" style="display: none;">
  347. <optgroup label="Alaskan/Hawaiian Time Zone">
  348. <option value="AK">Alaska</option>
  349. <option value="HI">Hawaii</option>
  350. </optgroup>
  351. <optgroup label="Pacific Time Zone">
  352. <option value="CA">California</option>
  353. <option value="NV">Nevada</option>
  354. <option value="OR">Oregon</option>
  355. <option value="WA">Washington</option>
  356. </optgroup>
  357. <optgroup label="Mountain Time Zone">
  358. <option value="AZ">Arizona</option>
  359. <option value="CO">Colorado</option>
  360. <option value="ID">Idaho</option>
  361. <option value="MT">Montana</option>
  362. <option value="NE">Nebraska</option>
  363. <option value="NM">New Mexico</option>
  364. <option value="ND">North Dakota</option>
  365. <option value="UT">Utah</option>
  366. <option value="WY">Wyoming</option>
  367. </optgroup>
  368. <optgroup label="Central Time Zone">
  369. <option value="AL">Alabama</option>
  370. <option value="AR">Arkansas</option>
  371. <option value="IL">Illinois</option>
  372. <option value="IA">Iowa</option>
  373. <option value="KS">Kansas</option>
  374. <option value="KY">Kentucky</option>
  375. <option value="LA">Louisiana</option>
  376. <option value="MN">Minnesota</option>
  377. <option value="MS">Mississippi</option>
  378. <option value="MO">Missouri</option>
  379. <option value="OK">Oklahoma</option>
  380. <option value="SD">South Dakota</option>
  381. <option value="TX">Texas</option>
  382. <option value="TN">Tennessee</option>
  383. <option value="WI">Wisconsin</option>
  384. </optgroup>
  385. <optgroup label="Eastern Time Zone">
  386. <option value="CT">Connecticut</option>
  387. <option value="DE">Delaware</option>
  388. <option value="FL">Florida</option>
  389. <option value="GA">Georgia</option>
  390. <option value="IN">Indiana</option>
  391. <option value="ME">Maine</option>
  392. <option value="MD">Maryland</option>
  393. <option value="MA">Massachusetts</option>
  394. <option value="MI">Michigan</option>
  395. <option value="NH">New Hampshire</option>
  396. <option value="NJ">New Jersey</option>
  397. <option value="NY">New York</option>
  398. <option value="NC">North Carolina</option>
  399. <option value="OH">Ohio</option>
  400. <option value="PA">Pennsylvania</option>
  401. <option value="RI">Rhode Island</option>
  402. <option value="SC">South Carolina</option>
  403. <option value="VT">Vermont</option>
  404. <option value="VA">Virginia</option>
  405. <option value="WV">West Virginia</option>
  406. </optgroup>
  407. </select>
  408. <script type="text/javascript">
  409. var $states = $(".js-source-states");
  410. var statesOptions = $states.html();
  411. $states.remove();
  412. $(".js-states").append(statesOptions);
  413. $("[data-fill-from]").each(function () {
  414. var $this = $(this);
  415. var codeContainer = $this.data("fill-from");
  416. var $container = $(codeContainer);
  417. var code = $.trim($container.html());
  418. $this.text(code);
  419. $this.addClass("prettyprint linenums");
  420. });
  421. prettyPrint();
  422. $.fn.select2.amd.require(
  423. ["select2/core", "select2/utils", "select2/compat/matcher"],
  424. function (Select2, Utils, oldMatcher) {
  425. var $basicSingle = $(".js-example-basic-single");
  426. var $basicMultiple = $(".js-example-basic-multiple");
  427. var $placeholderSingle = $(".js-example-placeholder-single");
  428. var $placeholderMultiple = $(".js-example-placeholder-multiple");
  429. var $dataArray = $(".js-example-data-array");
  430. var $dataArraySelected = $(".js-example-data-array-selected");
  431. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  432. var $ajax = $(".js-example-data-ajax");
  433. var $disabledResults = $(".js-example-disabled-results");
  434. var $tags = $(".js-example-tags");
  435. var $matcherStart = $('.js-example-matcher-start');
  436. var $diacritics = $(".js-example-diacritics");
  437. var $language = $(".js-example-language");
  438. $basicSingle.select2({
  439. theme: "classic"
  440. });
  441. $basicMultiple.select2()
  442. $placeholderSingle.select2({
  443. placeholder: "Select a state"
  444. });
  445. $placeholderMultiple.select2({
  446. placeholder: "Select a state"
  447. });
  448. $dataArray.select2({
  449. data: data
  450. });
  451. $dataArraySelected.select2({
  452. data: data
  453. });
  454. $ajax.select2({
  455. ajax: {
  456. url: "https://api.github.com/search/repositories",
  457. dataType: 'json',
  458. delay: 250,
  459. data: function (params) {
  460. return {
  461. q: params.term, // search term
  462. page: params.page
  463. };
  464. },
  465. processResults: function (data, page) {
  466. // parse the results into the format expected by Select2.
  467. // since we are using custom formatting functions we do not need to
  468. // alter the remote JSON data
  469. return data.items;
  470. },
  471. cache: true
  472. },
  473. minimumInputLength: 1,
  474. templateResult: function (repo) {
  475. if (repo.loading) return repo.text;
  476. var markup = '<div class="clearfix">' +
  477. '<div class="col-sm-1">' +
  478. '<img src="' + repo.owner.avatar_url + '" style="max-width: 100%" />' +
  479. '</div>' +
  480. '<div clas="col-sm-10">' +
  481. '<div class="clearfix">' +
  482. '<div class="col-sm-6">' + repo.full_name + '</div>' +
  483. '<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + repo.forks_count + '</div>' +
  484. '<div class="col-sm-2"><i class="fa fa-star"></i> ' + repo.stargazers_count + '</div>' +
  485. '</div>';
  486. if (repo.description) {
  487. markup += '<div>' + repo.description + '</div>';
  488. }
  489. markup += '</div></div>';
  490. return markup;
  491. },
  492. templateSelection: function (repo) {
  493. return repo.full_name || repo.text;
  494. }
  495. });
  496. $disabledResults.select2();
  497. $(".js-example-programmatic").select2();
  498. $tags.select2({
  499. tags: ['red', 'blue', 'green']
  500. });
  501. function matchStart (term, text) {
  502. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  503. return true;
  504. }
  505. return false;
  506. }
  507. $matcherStart.select2({
  508. matcher: oldMatcher(matchStart)
  509. });
  510. $diacritics.select2();
  511. $language.select2({
  512. language: "es"
  513. });
  514. });
  515. </script>