Getting Started with vanilla-grid-toolbar
<vn-grid-toolbar> is a command bar for a <vn-grid>: its items follow the
grid's state (rows loaded, a search running, rows selected) and run the grid's
commands. It needs a grid on the page. Each step shows the running example
and its whole source, a complete page you can copy.
What you need. The grid's script, then vanilla-grid-toolbar.bundle.js
from the build's dist/vanilla-components/latest/vanilla-grid-toolbar/
folder, in that order. Start with the
grid's getting-started guide if the grid
is new to you.
1. Status, search and reload
The grid attribute names the grid by its id. The toolbar's content goes in
a <template data-vn-grid-toolbar="empty">, the template it shows while no
row is selected:
<vn-grid-toolbar-status>shows how many rows are loaded;<vn-grid-toolbar-search>searches the grid as the user types,delaymilliseconds after the last key;<vn-grid-toolbar-button command="reload">reloads the rows.
<vn-grid-toolbar-group align="start"> and align="end" place the items at
the two ends of the bar.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>A toolbar for the grid</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
body { display: flex; flex-direction: column; }
vn-grid { display: block; flex: 1; min-height: 0; }
</style>
</head>
<body>
<!-- grid: the id of the <vn-grid> this toolbar drives.
The "empty" template is what the toolbar shows while nothing is selected. -->
<vn-grid-toolbar grid="people">
<template data-vn-grid-toolbar="empty">
<vn-grid-toolbar-group align="start">
<vn-grid-toolbar-status></vn-grid-toolbar-status>
</vn-grid-toolbar-group>
<vn-grid-toolbar-group align="end">
<vn-grid-toolbar-search delay="300"></vn-grid-toolbar-search>
<vn-grid-toolbar-button command="reload" title-key="refreshTitle">↻</vn-grid-toolbar-button>
</vn-grid-toolbar-group>
</template>
</vn-grid-toolbar>
<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>
<!-- The toolbar's script loads after the grid's. -->
<script src="path/to/vanilla-grid/vanilla-grid.bundle.js"></script>
<script src="path/to/vanilla-grid-toolbar/vanilla-grid-toolbar.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();
</script>
</body>
</html>
2. A template for selected rows
With selection-mode="multiple" on the grid, a second template,
data-vn-grid-toolbar="selected", replaces the first while any row is
selected. Here it shows the selection count, exports the selected rows to an
Excel file, and clears the selection. <vn-grid-toolbar-spacer grow> pushes
the items after it to the bar's end.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>A toolbar that follows the selection</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
body { display: flex; flex-direction: column; }
vn-grid { display: block; flex: 1; min-height: 0; }
</style>
</head>
<body>
<!-- Two templates: "empty" while nothing is selected, "selected" once a row is. -->
<vn-grid-toolbar grid="people">
<template data-vn-grid-toolbar="empty">
<vn-grid-toolbar-status></vn-grid-toolbar-status>
<vn-grid-toolbar-spacer grow></vn-grid-toolbar-spacer>
<vn-grid-toolbar-search delay="300"></vn-grid-toolbar-search>
</template>
<template data-vn-grid-toolbar="selected">
<vn-grid-toolbar-selection-count></vn-grid-toolbar-selection-count>
<vn-grid-toolbar-spacer grow></vn-grid-toolbar-spacer>
<vn-grid-toolbar-export scope="selected"></vn-grid-toolbar-export>
<vn-grid-toolbar-clear-selection></vn-grid-toolbar-clear-selection>
</template>
</vn-grid-toolbar>
<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 src="path/to/vanilla-grid-toolbar/vanilla-grid-toolbar.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();
</script>
</body>
</html>
Next steps
- The usage guide covers every built-in item, command buttons, custom items, localization and theming.
- The README lists the attributes, methods, events and standard commands.
- Getting started with vanilla-gantt-toolbar links the same toolbar to a Gantt's task list.