data.html 3.5 KB

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