Editable data grid

Select a cell, change its value, then save or cancel.

Notebook print run
Notebook Editor Copies
Coast walksAda120
City notesSam80
Forest pathsLin60
View markup
<div class="ex-data-grid" data-ex-grid>
  <div class="ex-grid-scroll">
    <table>
      <caption>Notebook print run</caption>
      <thead><tr>
        <th scope="col">Notebook</th>
        <th scope="col" data-field="editor" data-type="text" data-required data-maxlength="40">Editor</th>
        <th scope="col" data-field="copies" data-type="number" data-required data-min="0" data-max="5000" data-step="1">Copies</th>
      </tr></thead>
      <tbody>
        <tr data-row-id="coast"><th scope="row">Coast walks</th><td>Ada</td><td>120</td></tr>
        <tr data-row-id="city"><th scope="row">City notes</th><td>Sam</td><td>80</td></tr>
        <tr data-row-id="forest"><th scope="row">Forest paths</th><td>Lin</td><td>60</td></tr>
      </tbody>
    </table>
  </div>
</div>
Use in a project

Use the grid in a project

Copy recipes/editable-data-grid.html. Load data-grid.css after the core styles and load data-grid.mjs as a module. Start from a labelled table with stable row and field identifiers.

The demo keeps edits on this page until reload. To store changes, supply an async onSave callback to enhanceDataGrid. Resolve it after your service accepts the edit. Reject a failed save so the user can correct or retry it.

Connect a save function
import { enhanceDataGrid } from "./visual-guide-8/data-grid.mjs";

const grid = document.querySelector("[data-ex-grid]");
enhanceDataGrid(grid, { onSave: saveCell });

Your saveCell function receives rowId, field, previousValue, value and an abort signal. Return the promise for the save.

Download visual guide

Setup instructions · Source for AI tools