Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // THIS FUNCTION FETCHES COUTRIES LIST FROM API
- // I am using this part of select2 (https://select2.org/data-sources/ajax)
- $("#country").select2({
- ajax: {
- url: "https://restcountries.eu/rest/v2/name/",
- dataType: 'json',
- delay: 150,
- data: function (params) {
- return {
- // probably this is the part I should change but I have no clue
- q = params.term, // search term
- };
- },
- processResults: function (data, params) {
- // parse the results into the format expected by Select2
- // since we are using custom formatting functions we do not need to
- // alter the remote JSON data, except to indicate that infinite
- return {
- results: data.items,
- };
- },
- cache: true
- },
- placeholder: 'Select Country',
- minimumInputLength: 1,
- templateResult: formatCountry,
- templateSelection: formatCountrySelection
- });
- function formatCountry (country) {
- if (country.loading) {
- return country.text;
- }
- var $container = $(
- "<div class='select2-result-country clearfix'>" +
- "<div class='select2-result-country__avatar'><img src='" + country.flag + "' /></div>" +
- "<div class='select2-result-country__title'></div>" +
- "</div>"
- );
- $container.find(".select2-result-country__title").text(country.name);
- return $container;
- }
- function formatCountrySelection (country) {
- return country.name || country.text;
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement