I want to add data from a json file into a HTML table, but I get stuck on how to do it correctly...
I have a json file where the weather for three cities is set into objects. For each city I have 3 days of weather (min temp, max temp, wind and a picture/picto). The table shows the weather for one city at a time (a 3 day forecast).
I also have a select dropdown where you can choose a different city for the forecast of that city.
How can I put the json data in the table and also change it when another city is chosen...
My json file:
[{
"city": "Amsterdam",
"weer": [
{
"minTemp": 6,
"maxTemp": 14,
"wind": "geen",
"picto": "picto/rain%20(1).png"
},
{
"minTemp": 4,
"maxTemp": 13,
"wind": "NO",
"picto": "picto/rainsun.png"
},
{
"minTemp": 5,
"maxTemp": 15,
"wind": "NW",
"picto": "picto/rainsun.png"
},
{
"minTemp": 9,
"maxTemp": 16,
"wind": "ZW",
"picto": "picto/rain%20(1).png"
}
]
},
{
"city": "Antwerpen",
"weer": [
{
"minTemp": 10,
"maxTemp": 18,
"wind": "NO",
"picto": "picto/rain%20(1).png"
},
{
"minTemp": 10,
"maxTemp": 18,
"wind": "NO",
"picto": "picto/rainsun.png"
},
{
"minTemp": 10,
"maxTemp": 18,
"wind": "NO",
"picto": "picto/rainsun.png"
},
{
"minTemp": 10,
"maxTemp": 18,
"wind": "NO",
"picto": "picto/sun.png"
}
]
},
{
"city": "Barcalona",
"weer": [
{
"minTemp": 16,
"maxTemp": 24,
"wind": "NO",
"picto": "picto/sun.png"
},
{
"minTemp": 14,
"maxTemp": 22,
"wind": "NW",
"picto": "picto/sun.png"
},
{
"minTemp": 15,
"maxTemp": 23,
"wind": "ZW",
"picto": "picto/rainsun.png"
},
{
"minTemp": 18,
"maxTemp": 25,
"wind": "Z",
"picto": "picto/sun.png"
}
]
}]
My Javascript file:
async function getJson(url) {
let response = await fetch(url);
return response.json();
}
async function main() {
let weerdata = await getJson('europa_weer1.json');
let options = "";
// to set the city names in the select "steden" (cities)
for (var stad in weerdata){
options += '<option>' + weerdata[stad].city + '</option>';
}
console.log(options);
document.getElementById('steden').innerHTML = options;
}
main();
// to set the date in table aswell, for the days of the forecast.
showDate();
function showDate() {
const today = new Date();
const tomorrow = new Date(today);
const nextDay = new Date(today);
const dayAfter = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
nextDay.setDate(nextDay.getDate() + 2);
dayAfter.setDate(dayAfter.getDate() + 3);
document.getElementById('day1').innerHTML = today.toDateString();
document.getElementById('day2').innerHTML = tomorrow.toDateString();
document.getElementById('day3').innerHTML = nextDay.toDateString();
document.getElementById('day4').innerHTML = dayAfter.toDateString();
}
My HTML table:
<div class="total">
<h1>Het weer in Europese steden</h1>
<p>
<label for="steden">Kies je stad:</label>
<select id="steden" name="steden" >
</select>
</p>
<table class="week" id="weer">
<tr class="days" id="header">
<th id="day1"></th>
<th id="day2"></th>
<th id="day3"></th>
<th id="day4"></th>
</tr>
<tr class="picto" id="pics">
<td><img id="img1" src="" height="40" alt=""></td>
<td><img id="img2" src="" height="40" alt=""></td>
<td><img id="img3" src="" height="40" alt=""></td>
<td><img id="img4" src="" height="40" alt=""></td>
</tr>
<tr class="min" id="mTemp">
<td>Min. Temperatuur</td>
<td>Min. Temperatuur</td>
<td>Min. Temperatuur</td>
<td>Min. Temperatuur</td>
</tr>
<tr class="mintemp" id="mNum">
<td id="min1"></td>
<td id="min2"></td>
<td id="min3"></td>
<td id="min4"></td>
</tr>
<tr class="max" id="maTemp">
<td>Max. Temperatuur</td>
<td>Max. Temperatuur</td>
<td>Max. Temperatuur</td>
<td>Max. Temperatuur</td>
</tr>
<tr class="maxtemp" id="maNum">
<td id="max1"></td>
<td id="max2"></td>
<td id="max3"></td>
<td id="max4"></td>
</tr>
<tr class="wind" id="wind">
<td id="wind1"></td>
<td id="wind2"></td>
<td id="wind3"></td>
<td id="wind4"></td>
</tr>
</table>
</div>
<script src="script/weersteden.js">
</script>