Getting Started with vanilla-grid
<vn-grid> is a virtualized data grid: it renders only the rows in view, so it
stays fast from ten rows to a million. This guide builds one step by step.
Each step shows the running example and, under it, the example's whole
source: a complete page you can copy and open as it is.
What you need. One script, vanilla-grid.bundle.js, from the build's
dist/vanilla-components/latest/vanilla-grid/ folder. The examples load it
from path/to/: replace that with where you serve it, and serve the page over
HTTP (not file://). The grid adds its own stylesheets to the page. In the
source tree, vanilla-grid.js does the same job and loads its feature files
itself.
1. Your first grid
Declare the columns in markup, one <vn-grid-column> each: column-key names
the row field, header the header text, type how values are formatted and
sorted, width the starting width in pixels.
The rows always come from a DataManager. StaticDataManager holds an array in
memory. Once window.VanillaGridReady resolves, attach it with
setDataManager(), build the grid with initializeGrid(), and load the rows
with loadRowsAsync().
The grid fills its container, so give the container a height: here it is the whole page.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Your first grid</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
vn-grid { display: block; height: 100%; }
</style>
</head>
<body>
<!-- The columns are declared in markup: one <vn-grid-column> per column. -->
<vn-grid id="people">
<vn-grid-column kind="data" column-key="id" header="ID" type="number" width="60"></vn-grid-column>
<vn-grid-column kind="data" column-key="name" header="Name" type="string" width="160"></vn-grid-column>
<vn-grid-column kind="data" column-key="city" header="City" type="string" width="130"></vn-grid-column>
<vn-grid-column kind="data" column-key="age" header="Age" type="number" width="80"></vn-grid-column>
</vn-grid>
<script src="path/to/vanilla-grid/vanilla-grid.bundle.js"></script>
<script type="module">
// Wait until the grid's scripts have loaded.
await window.VanillaGridReady;
const rows = [
{ id: 1, name: 'Ada Lovelace', city: 'London', age: 36 },
{ id: 2, name: 'Alan Turing', city: 'Manchester', age: 41 },
{ id: 3, name: 'Grace Hopper', city: 'New York', age: 85 },
{ id: 4, name: 'Edsger Dijkstra', city: 'Austin', age: 72 },
{ id: 5, name: 'Barbara Liskov', city: 'Boston', age: 86 },
{ id: 6, name: 'Donald Knuth', city: 'Stanford', age: 88 },
{ id: 7, name: 'Margaret Hamilton', city: 'Boston', age: 90 },
{ id: 8, name: 'Tim Berners-Lee', city: 'Geneva', age: 71 },
{ id: 9, name: 'Radia Perlman', city: 'Seattle', age: 75 },
{ id: 10, name: 'Ken Thompson', city: 'Murray Hill', age: 83 },
];
const grid = document.getElementById('people');
// The rows come from a DataManager; StaticDataManager holds them in memory.
grid.setDataManager(new StaticDataManager({ rows }));
grid.initializeGrid();
await grid.loadRowsAsync();
</script>
</body>
</html>
2. What works without code
The first grid already lets the user:
- sort by clicking a header, and add columns to the sort with Shift+click;
- resize a column by dragging its header's right edge, and reorder columns by dragging a header;
- filter a column from the funnel in its header;
- hide, freeze or group by a column from the menu a right-click on its header opens.
Two attributes change how it starts. sort-by="age:desc" sorts by Age,
highest first. storage-mode="local" saves the user's column widths, order,
sort and filters in localStorage, so the next visit starts where this one
left off.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Built-in interactions</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
vn-grid { display: block; height: 100%; }
</style>
</head>
<body>
<!-- sort-by: the initial sort. storage-mode="local": the user's column
widths, order, sort and filters are saved for the next visit. -->
<vn-grid id="people" sort-by="age:desc" storage-mode="local">
<vn-grid-column kind="data" column-key="id" header="ID" type="number" width="60"></vn-grid-column>
<vn-grid-column kind="data" column-key="name" header="Name" type="string" width="160"></vn-grid-column>
<vn-grid-column kind="data" column-key="city" header="City" type="string" width="130"></vn-grid-column>
<vn-grid-column kind="data" column-key="age" header="Age" type="number" width="80"></vn-grid-column>
</vn-grid>
<script src="path/to/vanilla-grid/vanilla-grid.bundle.js"></script>
<script type="module">
await window.VanillaGridReady;
const rows = [
{ id: 1, name: 'Ada Lovelace', city: 'London', age: 36 },
{ id: 2, name: 'Alan Turing', city: 'Manchester', age: 41 },
{ id: 3, name: 'Grace Hopper', city: 'New York', age: 85 },
{ id: 4, name: 'Edsger Dijkstra', city: 'Austin', age: 72 },
{ id: 5, name: 'Barbara Liskov', city: 'Boston', age: 86 },
{ id: 6, name: 'Donald Knuth', city: 'Stanford', age: 88 },
{ id: 7, name: 'Margaret Hamilton', city: 'Boston', age: 90 },
{ id: 8, name: 'Tim Berners-Lee', city: 'Geneva', age: 71 },
{ id: 9, name: 'Radia Perlman', city: 'Seattle', age: 75 },
{ id: 10, name: 'Ken Thompson', city: 'Murray Hill', age: 83 },
];
const grid = document.getElementById('people');
grid.setDataManager(new StaticDataManager({ rows }));
grid.initializeGrid();
await grid.loadRowsAsync();
</script>
</body>
</html>
3. Themes
The theme attribute picks one of the built-in themes (default,
default-dark, material, fiori, carbon, fluent, glow, apple,
ledger, and the dark variant of most of them), and setTheme() switches it
at runtime. A theme is page-wide: two grids on different themes on one page
both show the last one loaded.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Themes</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
body { display: flex; flex-direction: column; }
label { padding: 8px; }
vn-grid { display: block; flex: 1; min-height: 0; }
</style>
</head>
<body>
<label>Theme
<select id="theme">
<option value="default">Default</option>
<option value="default-dark">Default Dark</option>
<option value="material">Material</option>
<option value="fiori">Fiori</option>
<option value="carbon">Carbon</option>
<option value="fluent">Fluent</option>
<option value="glow">Glow</option>
<option value="apple">Apple</option>
<option value="ledger">Ledger</option>
</select>
</label>
<!-- theme: the grid's starting theme. -->
<vn-grid id="people" theme="default">
<vn-grid-column kind="data" column-key="id" header="ID" type="number" width="60"></vn-grid-column>
<vn-grid-column kind="data" column-key="name" header="Name" type="string" width="160"></vn-grid-column>
<vn-grid-column kind="data" column-key="city" header="City" type="string" width="130"></vn-grid-column>
<vn-grid-column kind="data" column-key="age" header="Age" type="number" width="80"></vn-grid-column>
</vn-grid>
<script src="path/to/vanilla-grid/vanilla-grid.bundle.js"></script>
<script type="module">
await window.VanillaGridReady;
const rows = [
{ id: 1, name: 'Ada Lovelace', city: 'London', age: 36 },
{ id: 2, name: 'Alan Turing', city: 'Manchester', age: 41 },
{ id: 3, name: 'Grace Hopper', city: 'New York', age: 85 },
{ id: 4, name: 'Edsger Dijkstra', city: 'Austin', age: 72 },
{ id: 5, name: 'Barbara Liskov', city: 'Boston', age: 86 },
{ id: 6, name: 'Donald Knuth', city: 'Stanford', age: 88 },
{ id: 7, name: 'Margaret Hamilton', city: 'Boston', age: 90 },
{ id: 8, name: 'Tim Berners-Lee', city: 'Geneva', age: 71 },
];
const grid = document.getElementById('people');
grid.setDataManager(new StaticDataManager({ rows }));
grid.initializeGrid();
await grid.loadRowsAsync();
// setTheme() switches the theme at runtime.
document.getElementById('theme').addEventListener('change', (e) => {
grid.setTheme(e.target.value);
});
</script>
</body>
</html>
4. Row selection
selection-mode="multiple" adds a checkbox column (single allows one row),
and row-key-field names the field that identifies a row. The grid fires
vn-grid-selection-changed on every change, with the selected rows in
event.detail.selectedRows.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Row selection</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
body { display: flex; flex-direction: column; }
p { margin: 0; padding: 8px; }
vn-grid { display: block; flex: 1; min-height: 0; }
</style>
</head>
<body>
<p id="selection">Nothing selected</p>
<!-- selection-mode: 'single' or 'multiple' adds a checkbox column.
row-key-field: the field that identifies a row. -->
<vn-grid id="people" selection-mode="multiple" row-key-field="id">
<vn-grid-column kind="data" column-key="id" header="ID" type="number" width="60"></vn-grid-column>
<vn-grid-column kind="data" column-key="name" header="Name" type="string" width="160"></vn-grid-column>
<vn-grid-column kind="data" column-key="city" header="City" type="string" width="130"></vn-grid-column>
<vn-grid-column kind="data" column-key="age" header="Age" type="number" width="80"></vn-grid-column>
</vn-grid>
<script src="path/to/vanilla-grid/vanilla-grid.bundle.js"></script>
<script type="module">
await window.VanillaGridReady;
const rows = [
{ id: 1, name: 'Ada Lovelace', city: 'London', age: 36 },
{ id: 2, name: 'Alan Turing', city: 'Manchester', age: 41 },
{ id: 3, name: 'Grace Hopper', city: 'New York', age: 85 },
{ id: 4, name: 'Edsger Dijkstra', city: 'Austin', age: 72 },
{ id: 5, name: 'Barbara Liskov', city: 'Boston', age: 86 },
{ id: 6, name: 'Donald Knuth', city: 'Stanford', age: 88 },
{ id: 7, name: 'Margaret Hamilton', city: 'Boston', age: 90 },
{ id: 8, name: 'Tim Berners-Lee', city: 'Geneva', age: 71 },
];
const grid = document.getElementById('people');
grid.setDataManager(new StaticDataManager({ rows }));
grid.initializeGrid();
await grid.loadRowsAsync();
// Fired on every selection change; it bubbles up to the <vn-grid>.
grid.addEventListener('vn-grid-selection-changed', (e) => {
const names = e.detail.selectedRows.map((row) => row.name);
document.getElementById('selection').textContent =
names.length ? 'Selected: ' + names.join(', ') : 'Nothing selected';
});
</script>
</body>
</html>
5. A large dataset
Nothing changes for 100,000 rows: the grid renders only what is in view, so scrolling, sorting and filtering stay fast. The large-dataset-js sample goes up to two million rows.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>A large dataset</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
vn-grid { display: block; height: 100%; }
</style>
</head>
<body>
<vn-grid id="people">
<vn-grid-column kind="data" column-key="id" header="ID" type="number" width="90"></vn-grid-column>
<vn-grid-column kind="data" column-key="name" header="Name" type="string" width="160"></vn-grid-column>
<vn-grid-column kind="data" column-key="city" header="City" type="string" width="130"></vn-grid-column>
<vn-grid-column kind="data" column-key="age" header="Age" type="number" width="80"></vn-grid-column>
</vn-grid>
<script src="path/to/vanilla-grid/vanilla-grid.bundle.js"></script>
<script type="module">
await window.VanillaGridReady;
// 100,000 generated rows. The grid renders only the rows in view, so
// scrolling, sorting and filtering stay fast.
const cities = ['London', 'Paris', 'Rome', 'Berlin', 'Madrid', 'Vienna', 'Lisbon', 'Oslo'];
const rows = Array.from({ length: 100000 }, (_, i) => ({
id: i + 1,
name: 'Person ' + (i + 1),
city: cities[i % cities.length],
age: 20 + (i * 7) % 60,
}));
const grid = document.getElementById('people');
grid.setDataManager(new StaticDataManager({ rows }));
grid.initializeGrid();
await grid.loadRowsAsync();
</script>
</body>
</html>
6. Grouping
group-by="country:asc" groups the rows by Country when the grid starts; list
more key:direction entries, comma-separated, for more levels. The group bar
above the header shows the grouping and removes it, and the header menu groups
by another column.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Grouping</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
vn-grid { display: block; height: 100%; }
</style>
</head>
<body>
<!-- group-by: the starting groups, outermost first, each with a direction. -->
<vn-grid id="people" group-by="country:asc">
<vn-grid-column kind="data" column-key="id" header="ID" type="number" width="60"></vn-grid-column>
<vn-grid-column kind="data" column-key="name" header="Name" type="string" width="160"></vn-grid-column>
<vn-grid-column kind="data" column-key="country" header="Country" type="string" width="130"></vn-grid-column>
<vn-grid-column kind="data" column-key="city" header="City" type="string" width="130"></vn-grid-column>
</vn-grid>
<script src="path/to/vanilla-grid/vanilla-grid.bundle.js"></script>
<script type="module">
await window.VanillaGridReady;
const rows = [
{ id: 1, name: 'Ada Lovelace', country: 'United Kingdom', city: 'London' },
{ id: 2, name: 'Alan Turing', country: 'United Kingdom', city: 'Manchester' },
{ id: 3, name: 'Grace Hopper', country: 'United States', city: 'New York' },
{ id: 4, name: 'Edsger Dijkstra', country: 'Netherlands', city: 'Rotterdam' },
{ id: 5, name: 'Barbara Liskov', country: 'United States', city: 'Boston' },
{ id: 6, name: 'Donald Knuth', country: 'United States', city: 'Stanford' },
{ id: 7, name: 'Margaret Hamilton', country: 'United States', city: 'Boston' },
{ id: 8, name: 'Tim Berners-Lee', country: 'United Kingdom', city: 'London' },
{ id: 9, name: 'Guido van Rossum', country: 'Netherlands', city: 'Haarlem' },
];
const grid = document.getElementById('people');
grid.setDataManager(new StaticDataManager({ rows }));
grid.initializeGrid();
await grid.loadRowsAsync();
</script>
</body>
</html>
7. Tree rows
Rows that point at a parent show as a tree. tree-parent-field names the field
holding the parent's key (null for a top-level row), row-key-field the
field that key matches, and tree-column the column that shows the indent and
the expand toggles.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tree rows</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
vn-grid { display: block; height: 100%; }
</style>
</head>
<body>
<!-- row-key-field: the field that identifies a row; a parent is named by it.
tree-parent-field: the field naming a row's parent (null for a root).
tree-column: the column that shows the indent and the toggles. -->
<vn-grid id="files" row-key-field="id" tree-parent-field="parentId" tree-column="name">
<vn-grid-column kind="data" column-key="name" header="Name" type="string" width="220"></vn-grid-column>
<vn-grid-column kind="data" column-key="kind" header="Kind" type="string" width="100"></vn-grid-column>
<vn-grid-column kind="data" column-key="size" header="Size (KB)" type="number" width="120"></vn-grid-column>
</vn-grid>
<script src="path/to/vanilla-grid/vanilla-grid.bundle.js"></script>
<script type="module">
await window.VanillaGridReady;
// Each row points at its parent by the parent's id.
const rows = [
{ id: 1, parentId: null, name: 'src', kind: 'Folder', size: null },
{ id: 2, parentId: 1, name: 'components', kind: 'Folder', size: null },
{ id: 3, parentId: 2, name: 'grid.js', kind: 'File', size: 42 },
{ id: 4, parentId: 2, name: 'toolbar.js', kind: 'File', size: 18 },
{ id: 5, parentId: 1, name: 'index.js', kind: 'File', size: 3 },
{ id: 6, parentId: null, name: 'docs', kind: 'Folder', size: null },
{ id: 7, parentId: 6, name: 'getting-started.md', kind: 'File', size: 12 },
{ id: 8, parentId: null, name: 'README.md', kind: 'File', size: 6 },
];
const grid = document.getElementById('files');
grid.setDataManager(new StaticDataManager({ rows }));
grid.initializeGrid();
await grid.loadRowsAsync();
</script>
</body>
</html>
Next steps
- The README lists every attribute, option, method and event.
- Getting started with vanilla-grid-toolbar adds a status line, a search box and commands above the grid.
- The sample apps load real data: northwind-orders-js from an OData service, anilist-anime-js from GraphQL, and file-system-js shows a tree of up to a million rows.
- The technical docs explain how each feature works.