Despite successfully loading the API country data (as evidenced by the console.log()
entry and the accompanying picture of my work desk), the country information does not display when hovering the mouse cursor over the search bar field (expecting a dropdown of all countries) or typing in a country name to filter the list. What am I missing in this scenario?
My intention was to present the selected country data (capital, currency, language, timezone, etc.) from the API in the designated box, with the country flag serving as the background image!
// A function to format numbers with suffix letters
function formatNumber(labelValue) {
return Math.abs(Number(labelValue)) >= 1.0e+9
? Math.abs(Number(labelValue)) / 1.0e+9 + "B"
: Math.abs(Number(labelValue)) >= 1.0e+6
? Math.abs(Number(labelValue)) / 1.0e+6 + "M"
: Math.abs(Number(labelValue)) >= 1.0e+3
? Math.abs(Number(labelValue)) / 1.0e+3 + "K"
: Math.abs(Number(labelValue));
}
const settings = {
async: true,
crossDomain: true,
url: `https://restcountries.com/v3.1/all`,
method: 'GET',
headers: {
'X-RapidAPI-Key': 'b56451a2dcmsh7ba7285037af267p1435dajsn71561b599c58',
'X-RapidAPI-Host': 'rest-countries10.p.rapidapi.com'
}
};
$.ajax(settings).done(function (response) {
console.log(response);
});
var vm = Vue.createApp({
el: '#main',
data: {
search: '',
selectedName: '',
selectedCode: '',
selectedCapital: '',
selectedLanguage: '',
selectedCurrency: '',
selectedLatlng: '',
selectedLatlngUrl: '',
selectedPopulation: '',
selectedArea: '',
selectedSubregion: '',
selectedGini: '',
selectedTimezone: '',
selectedFlagSrc: '',
},
computed: {
countries () {
var self = this;
var countries;
// Fetch JSON data from API
$.ajax({
async: false,
url: `https://restcountries.com/v3.1/all`,
success: function(data){
countries = data;
}
});
// Filter the countries by name in all languages
countries = countries.filter(function(value, index, array) {
return value['name'].toLowerCase().includes(self.search.toLowerCase())
|| Object.values(value['translations']).join(' ').toLowerCase().includes(self.search.toLowerCase());
});
return countries;
},
},
// Automatically select the first country in the list upon loading
beforeMount() {
var self = this;
var found = false;
this.countries.forEach(function(element) {
if(element['alpha2Code'] === 'US') {
self.selectCountry(element);
found = true;
}
});
if(!found)
this.selectCountry(this.countries[0]);
},
methods: {
// Return the country name
getName (country) {
return (country['name']);
},
// Return the URL of the country flag
getFlagSrc (country) {
return (country['flag'] || 'N/A');
},
// Set the selected country data
selectCountry (country) {
var self = this;
$('section').animate({
opacity: 0
}, 150, function() {
self.selectedName = (country['name'] || 'N/A');
self.selectedFlagSrc = self.getFlagSrc(country);
self.selectedCode = (country['alpha2Code'] || 'N/A') + ' / ' + (country['alpha3Code'] || 'N/A');
self.selectedCapital = (country['capital'] || 'N/A');
var arrayLanguage = [];
country['languages'].forEach(function(element) {
arrayLanguage.push(element['name']);
});
self.selectedLanguage = (country['languages'].length > 0) ? arrayLanguage.join(', ') : 'N/A';
var arrayCurrency = [];
country['currencies'].forEach(function(element) {
arrayCurrency.push(element['name'] + ' ' + element['symbol']);
});
self.selectedCurrency = (country['currencies'].length > 0) ? arrayCurrency.join(', ') : 'N/A';
self.selectedLatlng = (country['Latlng'].length > 0) ? ('Lat: ' + country['Latlng'][0] + ', Lng: ' + country['Latlng'][1]) : 'N/A';
self.selectedLatlngUrl = (country['Latlng'].length > 0) ? ('https://www.google.com/maps/?q=' + country['Latlng'][0] + ',' + country['Latlng'][1]) : '';
self.selectedPopulation = country['Population'] ? formatNumber(country['Population']) : 'N/A';
self.selectedArea = country['Area'] ? (formatNumber(country['Area']) + ' km²') : 'N/A';
self.selectedSubregion = (country['Subregion'] || 'N/A');
self.selectedGini = country['Gini'] ? (country['Gini'] + '%') : 'N/A';
self.selectedTimezone = (country['Timezones'].length > 0) ? country['Timezones'].join(', ') : 'N/A';
$('section').animate({
opacity: 1
});
});
},
}
});