JSFiddle - React, Tailwind, and code Playground
by KevinGabbert
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Include DataTables CSS from CDN -->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css">
</head>
<body>
<div class="container mt-5">
<br>
<div id="table-container">
<!-- Placeholder for the table -->
</div>
</div>
<!-- Link jQuery and DataTables JS from CDN -->
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
<!-- Your script section -->
<script type="text/javascript">
$(document).ready(function () {
// Fetch and populate grid on page load
fetchDataAndUpdateGrid();
});
function fetchDataAndUpdateGrid() {
// Generate fake data for demonstration
var fakeData = generateFakeData();
// Update the grid with fetched data
updateGrid(fakeData);
}
function updateGrid(data) {
// Remove existing table before updating
$("#table-container").empty();
var table = $("<table>").attr("id", "example").addClass("display");
var thead = $("<thead>").append("<tr><th>Name</th><th>Age</th></tr>");
var tbody = $("<tbody>");
$.each(data, function (index, item) {
var row = $("<tr>").append(
$("<td>").text(item.Name),
$("<td>").text(item.Age)
);
tbody.append(row);
});
table.append(thead, tbody);
// Append the table to the container
$("#table-container").append(table);
// Initialize DataTables after adding to DOM
$('#example').DataTable();
}
function generateFakeData() {
var fakeData = [];
for...