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