ammaan commited on
Commit
6d1201c
1 Parent(s): 014aa6f

Create index.js

Browse files
Files changed (1) hide show
  1. static/index.js +53 -0
static/index.js ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.addEventListener("DOMContentLoaded", function () {
2
+ fetchData();
3
+ });
4
+
5
+ async function fetchData() {
6
+ try {
7
+ const response = await fetch("getdata");
8
+ if (!response.ok) {
9
+ throw new Error("Failed to fetch data");
10
+ }
11
+ const data = await response.json();
12
+ displayData(data);
13
+ } catch (error) {
14
+ console.log("Error Fetching data", error);
15
+ }
16
+ }
17
+
18
+ function displayData(data) {
19
+ const dataDiv = document.getElementById("data");
20
+ if (data) {
21
+ const html = `
22
+ <table>
23
+ <thead>
24
+ <tr>
25
+ <th>Country</th>
26
+ <th>Alpha-2 Code</th>
27
+ <th>Alpha-3 Code</th>
28
+ <th>Region</th>
29
+ <th>Subregion</th>
30
+ <th>region code</th>
31
+ </tr>
32
+ </thead>
33
+ <tbody>
34
+ ${data.data
35
+ ?.map(
36
+ (row) => `
37
+ <tr>
38
+ <td>${row[0]}</td>
39
+ <td>${row[1]}</td>
40
+ <td>${row[2]}</td>
41
+ <td>${row[3]}</td>
42
+ <td>${row[4]}</td>
43
+ <td>${row[5]}</td>
44
+ </tr>
45
+ `
46
+ )
47
+ .join("")}
48
+ </tbody>
49
+ </table>
50
+ `;
51
+ dataDiv.innerHTML = html;
52
+ }
53
+ }