examples.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. </p>
  198. <p>
  199. <select class="js-example-programmatic js-states form-control"></select>
  200. </p>
  201. </div>
  202. <div class="col-md-8">
  203. <h2>Example code</h2>
  204. <pre data-fill-from=".js-code-programmatic"></pre>
  205. <script type="text/javascript" class="js-code-programmatic">
  206. var $example = $(".js-example-programmatic");
  207. $(".js-programmatic-open").on("click", function () { $example.select2("open"); });
  208. $(".js-programmatic-close").on("click", function () { $example.select2("close"); });
  209. </script>
  210. </div>
  211. </section>
  212. <section id="tags" class="row">
  213. <div class="col-md-4">
  214. <h1>Tagging support</h1>
  215. <p>
  216. Select2 can be used to quickly set up fields used for tagging.
  217. </p>
  218. <p>
  219. <select class="js-example-tags form-control">
  220. <option value="one">First</option>
  221. <option value="other">Other</option>
  222. </select>
  223. </p>
  224. <p>
  225. Note that when tagging is enabled the user can select from pre-existing
  226. options or create a new tag by picking the first choice, which is what
  227. the user has typed into the search box so far.
  228. </p>
  229. </div>
  230. <div class="col-md-8">
  231. <h2>Example code</h2>
  232. <pre data-fill-from=".js-code-tags"></pre>
  233. <script type="text/x-example-code" class="js-code-tags">
  234. $(".js-example-tags").select2({
  235. tags: true
  236. })
  237. </script>
  238. </div>
  239. </section>
  240. <section id="matcher" class="row">
  241. <div class="col-md-4">
  242. <h1>Custom matcher</h1>
  243. <p>
  244. Unlike other dropdowns on this page, this one matches options only if
  245. the term appears in the beginning of the string as opposed to anywhere:
  246. </p>
  247. <p>
  248. <select class="js-example-matcher-start js-states form-control"></select>
  249. </p>
  250. <p>
  251. This custom matcher uses a
  252. <a href="compatibility.html">compatibility module</a> that is only
  253. bundled in the
  254. <a href="getting-started#versions">full version of Select2</a>. You also
  255. have the option of using a
  256. <a href="options.html#matcher">more complex matcher</a>.
  257. </p>
  258. </div>
  259. <div class="col-md-8">
  260. <h2>Example code</h2>
  261. <pre data-fill-from=".js-code-matcher-start"></pre>
  262. <script type="text/x-example-code" class="js-code-matcher-start">
  263. function matchStart (term, text) {
  264. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  265. return true;
  266. }
  267. return false;
  268. }
  269. $.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  270. $(".js-example-matcher-start").select2({
  271. matcher: oldMatcher(matchStart)
  272. })
  273. });
  274. </script>
  275. </div>
  276. </section>
  277. <section id="language" class="row">
  278. <div class="col-md-4">
  279. <h1>Multiple languages</h1>
  280. <p>
  281. Select2 supports displaying the messages in different languages, as well
  282. as provding your own
  283. <a href="options.html#language">custom messages</a>
  284. that can be displayed.
  285. </p>
  286. <p>
  287. <select class="js-example-language js-states form-control">
  288. </select>
  289. </p>
  290. <p>
  291. The language does not have to be defined when Select2 is being
  292. initialized, but instead can be defined in the <code>[lang]</code>
  293. attribute of any parent elements as <code>[lang="es"]</code>.
  294. </p>
  295. </div>
  296. <div class="col-md-8">
  297. <h2>Example code</h2>
  298. <pre data-fill-from=".js-code-language"></pre>
  299. <script type="text/x-example-code" class="js-code-language">
  300. $(".js-example-language").select2({
  301. language: "es"
  302. })
  303. </script>
  304. </div>
  305. </section>
  306. </div>
  307. <select class="js-source-states" style="display: none;">
  308. <optgroup label="Alaskan/Hawaiian Time Zone">
  309. <option value="AK">Alaska</option>
  310. <option value="HI">Hawaii</option>
  311. </optgroup>
  312. <optgroup label="Pacific Time Zone">
  313. <option value="CA">California</option>
  314. <option value="NV">Nevada</option>
  315. <option value="OR">Oregon</option>
  316. <option value="WA">Washington</option>
  317. </optgroup>
  318. <optgroup label="Mountain Time Zone">
  319. <option value="AZ">Arizona</option>
  320. <option value="CO">Colorado</option>
  321. <option value="ID">Idaho</option>
  322. <option value="MT">Montana</option>
  323. <option value="NE">Nebraska</option>
  324. <option value="NM">New Mexico</option>
  325. <option value="ND">North Dakota</option>
  326. <option value="UT">Utah</option>
  327. <option value="WY">Wyoming</option>
  328. </optgroup>
  329. <optgroup label="Central Time Zone">
  330. <option value="AL">Alabama</option>
  331. <option value="AR">Arkansas</option>
  332. <option value="IL">Illinois</option>
  333. <option value="IA">Iowa</option>
  334. <option value="KS">Kansas</option>
  335. <option value="KY">Kentucky</option>
  336. <option value="LA">Louisiana</option>
  337. <option value="MN">Minnesota</option>
  338. <option value="MS">Mississippi</option>
  339. <option value="MO">Missouri</option>
  340. <option value="OK">Oklahoma</option>
  341. <option value="SD">South Dakota</option>
  342. <option value="TX">Texas</option>
  343. <option value="TN">Tennessee</option>
  344. <option value="WI">Wisconsin</option>
  345. </optgroup>
  346. <optgroup label="Eastern Time Zone">
  347. <option value="CT">Connecticut</option>
  348. <option value="DE">Delaware</option>
  349. <option value="FL">Florida</option>
  350. <option value="GA">Georgia</option>
  351. <option value="IN">Indiana</option>
  352. <option value="ME">Maine</option>
  353. <option value="MD">Maryland</option>
  354. <option value="MA">Massachusetts</option>
  355. <option value="MI">Michigan</option>
  356. <option value="NH">New Hampshire</option>
  357. <option value="NJ">New Jersey</option>
  358. <option value="NY">New York</option>
  359. <option value="NC">North Carolina</option>
  360. <option value="OH">Ohio</option>
  361. <option value="PA">Pennsylvania</option>
  362. <option value="RI">Rhode Island</option>
  363. <option value="SC">South Carolina</option>
  364. <option value="VT">Vermont</option>
  365. <option value="VA">Virginia</option>
  366. <option value="WV">West Virginia</option>
  367. </optgroup>
  368. </select>
  369. <script type="text/javascript">
  370. var $states = $(".js-source-states");
  371. var statesOptions = $states.html();
  372. $states.remove();
  373. $(".js-states").append(statesOptions);
  374. $("[data-fill-from]").each(function () {
  375. var $this = $(this);
  376. var codeContainer = $this.data("fill-from");
  377. var $container = $(codeContainer);
  378. var code = $.trim($container.html());
  379. $this.text(code);
  380. $this.addClass("prettyprint linenums");
  381. });
  382. prettyPrint();
  383. $.fn.select2.amd.require(
  384. ["select2/core", "select2/utils", "select2/compat/matcher"],
  385. function (Select2, Utils, oldMatcher) {
  386. var $basicSingle = $(".js-example-basic-single");
  387. var $basicMultiple = $(".js-example-basic-multiple");
  388. var $placeholderSingle = $(".js-example-placeholder-single");
  389. var $placeholderMultiple = $(".js-example-placeholder-multiple");
  390. var $dataArray = $(".js-example-data-array");
  391. var $dataArraySelected = $(".js-example-data-array-selected");
  392. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  393. var $ajax = $(".js-example-data-ajax");
  394. var $disabledResults = $(".js-example-disabled-results");
  395. var $tags = $(".js-example-tags");
  396. var $matcherStart = $('.js-example-matcher-start');
  397. var $language = $(".js-example-language");
  398. $basicSingle.select2();
  399. $basicMultiple.select2()
  400. $placeholderSingle.select2({
  401. placeholder: "Select a state"
  402. });
  403. $placeholderMultiple.select2({
  404. placeholder: "Select a state"
  405. });
  406. $dataArray.select2({
  407. data: data
  408. });
  409. $dataArraySelected.select2({
  410. data: data
  411. });
  412. $ajax.select2({
  413. ajax: {
  414. url: "https://api.github.com/search/repositories",
  415. dataType: 'json',
  416. delay: 250,
  417. data: function (params) {
  418. return {
  419. q: params.term, // search term
  420. page: params.page
  421. };
  422. },
  423. processResults: function (data, page) {
  424. // parse the results into the format expected by Select2.
  425. // since we are using custom formatting functions we do not need to
  426. // alter the remote JSON data
  427. return data.items;
  428. },
  429. cache: true
  430. },
  431. minimumInputLength: 1,
  432. templateResult: function (repo) {
  433. var markup = '<div class="clearfix">' +
  434. '<div class="col-sm-1">' +
  435. '<img src="' + repo.owner.avatar_url + '" style="max-width: 100%" />' +
  436. '</div>' +
  437. '<div clas="col-sm-10">' +
  438. '<div class="clearfix">' +
  439. '<div class="col-sm-6">' + repo.full_name + '</div>' +
  440. '<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + repo.forks_count + '</div>' +
  441. '<div class="col-sm-2"><i class="fa fa-star"></i> ' + repo.stargazers_count + '</div>' +
  442. '</div>';
  443. if (repo.description) {
  444. markup += '<div>' + repo.description + '</div>';
  445. }
  446. markup += '</div></div>';
  447. return markup;
  448. },
  449. templateSelection: function (repo) {
  450. return repo.full_name || repo.text;
  451. }
  452. });
  453. $disabledResults.select2();
  454. $(".js-example-programmatic").select2();
  455. $tags.select2({
  456. tags: ['red', 'blue', 'green']
  457. });
  458. function matchStart (term, text) {
  459. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  460. return true;
  461. }
  462. return false;
  463. }
  464. $matcherStart.select2({
  465. matcher: oldMatcher(matchStart)
  466. });
  467. $language.select2({
  468. language: "es"
  469. });
  470. });
  471. </script>