examples.html 23 KB

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