JSFiddle - React, Tailwind, and code Playground
by stitot
HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Simple Virtualized DataGrid</title>
<style>
body { font-family: system-ui, sans-serif; margin: 16px; }
.grid {
border: 1px solid #ddd;
width: 100%;
max-width: 1100px;
}
.header, .row {
display: grid;
grid-template-columns: repeat(20, minmax(80px, 1fr));
align-items: center;
}
.header {
position: sticky;
top: 0;
background: #fafafa;
z-index: 2;
border-bottom: 1px solid #eee;
font-weight: 600;
}
.cell {
padding: 6px 8px;
border-right: 1px solid #f0f0f0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.row .cell:last-child, .header .cell:last-child { border-right: 0; }
.scroller {
height: 480px;
overflow: auto;
position: relative;
}
.spacer {
position: relative;
width: 100%;
}
.rowsLayer {
position: absolute;
left: 0;
right: 0;
top: 0;
will-change: transform;
contain: layout paint;
}
.row {
border-bottom: 1px solid #f3f3f3;
box-sizing: border-box;
}
.row:nth-child(even) { background: #000; }
.row:hover { background: #f3f7ff; }
</style>
</head>
<body>
<div class="grid" id="grid"></div>
<script>
// --- Demo data (replace with your real data access) ---
const ROWS = 100000;
const COLS = 20;
function getCellValue(rowIndex, colIndex) {
// Keep this fast. In real life, read from arrays/typed arrays/etc.
return `R${rowIndex} · C${colIndex}`;
}
// --- Virtualized grid ---
function createVirtualGrid(root, { rows, cols, rowHeight = 28, overscan = 8 }) {
// Header
const header = document.createElement('div');
header.className = 'header';
for (let...