data.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. {% highlight html linenos %}
  24. <script type="text/javascript">
  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. </script>
  33. <select class="js-example-data-array"></select>
  34. <select class="js-example-data-array-selected">
  35. <option value="2" selected="selected">duplicate</option>
  36. </select>
  37. {% endhighlight %}
  38. <h2 id="data-ajax" >Loading remote data</h2>
  39. <p>
  40. Select2 comes with AJAX support built in, using jQuery's AJAX methods.
  41. In this example, we can search for repositories using GitHub's API.
  42. </p>
  43. <p>
  44. <select class="js-example-data-ajax form-control">
  45. <option value="3620194" selected="selected">select2/select2</option>
  46. </select>
  47. </p>
  48. <p>
  49. When using Select2 with remote data, the HTML required for the
  50. <code>select</code> is the same as any other Select2. If you need to
  51. provide default selections, you just need to include an
  52. <code>option</code> for each selection that contains the value and text
  53. that should be displayed.
  54. </p>
  55. {% highlight html linenos %}
  56. <select class="js-data-example-ajax">
  57. <option value="3620194" selected="selected">select2/select2</option>
  58. </select>
  59. {% endhighlight %}
  60. <p>
  61. You can configure how Select2 searches for remote data using the
  62. <code>ajax</code> option. More information on the individual options
  63. that Select2 handles can be found in the
  64. <a href="options.html#ajax">options documentation for <code>ajax</code></a>.
  65. </p>
  66. {% highlight js linenos %}
  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, params) {
  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, except to indicate that infinite
  82. // scrolling can be used
  83. params.page = params.page || 1;
  84. return {
  85. results: data.items,
  86. pagination: {
  87. more: (params.page * 30) < data.total_count
  88. }
  89. };
  90. },
  91. cache: true
  92. },
  93. escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
  94. minimumInputLength: 1,
  95. templateResult: formatRepo, // omitted for brevity, see the source of this page
  96. templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
  97. });
  98. {% endhighlight %}
  99. <p>
  100. Select2 will pass any options in the <code>ajax</code> object to
  101. jQuery's <code>$.ajax</code> function, or the <code>transport</code>
  102. function you specify.
  103. </p>
  104. </section>