Advertisement
GeorgePashev_88

Web 3 HTML Client - Frontend

Dec 15th, 2023
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.74 KB | None | 0 0
  1. <html>
  2. <head>
  3.    <style>
  4.       table, th, td {
  5.          border: 1px solid black;
  6.          border-collapse: collapse;
  7.       }
  8.       td, th {
  9.          padding: 10px;
  10.       }
  11.    </style>
  12. </head>
  13. <body>
  14.    <h2>STUDENTI FRONT END</h2>
  15.    <p>BlockChain Front End for Studenti Contract</p><br>
  16.  
  17.    <h3> Resulting Table: </h3>
  18.    <div id="container"></div>
  19.    <script>
  20.  
  21. function httpGet(theUrl, callbackThen)
  22. {
  23.     const fetchPromise = fetch(
  24.   theUrl
  25. );
  26.  
  27. fetchPromise.then((response) => {
  28.   const jsonPromise = response.json();
  29.   jsonPromise.then((data) => {
  30.     console.log(data);
  31.     convert(data);
  32.   });
  33. });
  34. }
  35.  
  36.  
  37.       // Function to convert JSON data to HTML table
  38.       function convert(jsonData1) {
  39.  
  40.  
  41.          
  42.          // Sample JSON data
  43.          let jsonData = jsonData1;
  44.          
  45.          // Get the container element where the table will be inserted
  46.          let container = document.getElementById("container");
  47.          container.replaceChildren();
  48.          // Create the table element
  49.          let table = document.createElement("table");
  50.          
  51.          // Get the keys (column names) of the first object in the JSON data
  52.          let cols = Object.keys(jsonData[0]);
  53.          
  54.          // Create the header element
  55.          let thead = document.createElement("thead");
  56.          let tr = document.createElement("tr");
  57.          
  58.          // Loop through the column names and create header cells
  59.          cols.forEach((item) => {
  60.             let th = document.createElement("th");
  61.             th.innerText = item; // Set the column name as the text of the header cell
  62.             tr.appendChild(th); // Append the header cell to the header row
  63.          });
  64.          thead.appendChild(tr); // Append the header row to the header
  65.          table.append(tr) // Append the header to the table
  66.          
  67.          // Loop through the JSON data and create table rows
  68.          jsonData.forEach((item) => {
  69.             let tr = document.createElement("tr");
  70.            
  71.             // Get the values of the current object in the JSON data
  72.             let vals = Object.values(item);
  73.            
  74.             // Loop through the values and create table cells
  75.             vals.forEach((elem) => {
  76.                let td = document.createElement("td");
  77.                td.innerText = elem; // Set the value as the text of the table cell
  78.                tr.appendChild(td); // Append the table cell to the table row
  79.             });
  80.             table.appendChild(tr); // Append the table row to the table
  81.          });
  82.          container.appendChild(table) // Append the table to the container element
  83.       }
  84.  
  85.       httpGet('http://127.0.0.1:5000/api/students', function(data){convert(data)});
  86.  
  87.  
  88.    </script>
  89. </body>
  90. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement