Spaces:
Running
Running
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>fetch api & Datatable</title> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.2.0/css/bootstrap.min.css"> | |
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.1/css/dataTables.bootstrap5.min.css"> | |
<style> | |
section{ | |
position: relative; | |
width: 100%; | |
padding: 0px 8% 20px; | |
} | |
header h1{ | |
color: #8e44ad; | |
font-size: 2em; | |
margin:30px 0px | |
} | |
</style> | |
</head> | |
<body> | |
<section> | |
<header> | |
<h1>Data table</h1> | |
</header> | |
<!-- table area --> | |
<table id="example" class="table table-striped" style="width:100%"> | |
<thead> | |
<tr> | |
<th>Zone </th> | |
<th>Sr No</th> | |
<th>Name</th> | |
<th>Fathers Name</th> | |
<th>Mobile</th> | |
<th>Name_E</th> | |
<th>Father's Name E</th> | |
</tr> | |
</thead> | |
<tbody> | |
</tbody> | |
</table> | |
</section> | |
<!-- js library --> | |
<script src="https://code.jquery.com/jquery-3.5.1.js"></script> | |
<script src="https://cdn.datatables.net/1.13.1/js/jquery.dataTables.min.js"></script> | |
<script src="https://cdn.datatables.net/1.13.1/js/dataTables.bootstrap5.min.js"></script> | |
<script> | |
fetch('http://basantagarwal.com/csvjson_short.json') | |
.then(res=>res.json()) | |
.then(data=>{ | |
console.log('data',data); | |
displayProducts(data); | |
}) | |
async function displayProducts(products) { | |
let html = ''; | |
await products.forEach((product, index, array) => { | |
html += '<tr>'; | |
html += `<td> | |
${product.Zone} | |
</td> | |
<td>${product.SrNo}</td> | |
<td>${product.NAME}</td> | |
<td>${product.FATHERS_NAME}</td> | |
<td>${product.Mobile}</td> | |
<td>${product.NAME_E}</td> | |
<td>${product.FATHERSN_E}</td>`; | |
html += '</tr>'; | |
}) | |
document.querySelector('tbody').innerHTML = await html; | |
// | |
$(document).ready(function () { | |
$('#example').DataTable(); | |
}); | |
} | |
</script> | |
</body> | |
</html> |