data.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <section>
  2. <h1 id="data" class="page-header">
  3. Data sources
  4. </h1>
  5. <p>In addition to handling options from a standard <code>&lt;select&gt;</code>, Select2 can also retrieve the results from other data sources.</p>
  6. <h2 id="data-array" >Loading array data</h2>
  7. <p>
  8. Select2 provides a way to load the data from a local array.
  9. You can provide initial selections with array data by providing the
  10. option tag for the selected values, similar to how it would be done for
  11. a standard select.
  12. </p>
  13. <div class="s2-example">
  14. <p>
  15. <select class="js-example-data-array form-control"></select>
  16. </p>
  17. <p>
  18. <select class="js-example-data-array-selected form-control">
  19. <option value="2" selected="selected">duplicate</option>
  20. </select>
  21. </p>
  22. </div>
  23. <pre data-fill-from=".js-code-data-array"></pre>
  24. <script type="text/x-example-code" class="js-code-data-array">
  25. var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
  26. $(".js-example-data-array").select2({
  27. data: data
  28. })
  29. $(".js-example-data-array-selected").select2({
  30. data: data
  31. })
  32. <select class="js-example-data-array"></select>
  33. <select class="js-example-data-array-selected">
  34. <option value="2" selected="selected">duplicate</option>
  35. </select>
  36. </script>
  37. <h2 id="data-ajax" >Loading remote data</h2>
  38. <p>
  39. Select2 comes with AJAX support built in, using jQuery's AJAX methods.
  40. In this example, we can search for repositories using GitHub's API.
  41. </p>
  42. <p>
  43. <select class="js-example-data-ajax form-control">
  44. <option value="3620194" selected="selected">select2/select2</option>
  45. </select>
  46. </p>
  47. <p>
  48. When using Select2 with remote data, the HTML required for the
  49. <code>select</code> is the same as any other Select2. If you need to
  50. provide default selections, you just need to include an
  51. <code>option</code> for each selection that contains the value and text
  52. that should be displayed.
  53. </p>
  54. <pre data-fill-from=".js-code-data-ajax-html"></pre>
  55. <p>
  56. You can configure how Select2 searches for remote data using the
  57. <code>ajax</code> option. More information on the individual options
  58. that Select2 handles can be found in the
  59. <a href="options.html#ajax">options documentation for <code>ajax</code></a>.
  60. </p>
  61. <pre data-fill-from=".js-code-data-ajax"></pre>
  62. <p>
  63. Select2 will pass any options in the <code>ajax</code> object to
  64. jQuery's <code>$.ajax</code> function, or the <code>transport</code>
  65. function you specify.
  66. </p>
  67. <script type="text/x-example-code" class="js-code-data-ajax">
  68. $(".js-data-example-ajax").select2({
  69. ajax: {
  70. url: "https://api.github.com/search/repositories",
  71. dataType: 'json',
  72. delay: 250,
  73. data: function (params) {
  74. return {
  75. q: params.term, // search term
  76. page: params.page
  77. };
  78. },
  79. processResults: function (data, page) {
  80. // parse the results into the format expected by Select2.
  81. // since we are using custom formatting functions we do not need to
  82. // alter the remote JSON data
  83. return {
  84. results: data.items
  85. };
  86. },
  87. cache: true
  88. },
  89. escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
  90. minimumInputLength: 1,
  91. templateResult: formatRepo, // omitted for brevity, see the source of this page
  92. templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
  93. });
  94. </script>
  95. <script type="text/x-example-code" class="js-code-data-ajax-html">
  96. <select class="js-data-example-ajax">
  97. <option value="3620194" selected="selected">select2/select2</option>
  98. </select>
  99. </script>
  100. </section>