Lattice Grid Buy a licence

demo D93

Conditional Editing: Read-Only Columns and Cells in htmx

Columns that never edit, and cells that depend on the record

edit.enabled: (row) => …

This grid locks some columns entirely and makes others read-only per row, so a status cell stops editing once an order ships. It expresses which cells a user may change as a rule on the record, not a manual check scattered through the app.

Building…
Loading a live grid…

This is the htmx version. A server-rendered table is upgraded in place, so the page is readable before any script runs and gains the full grid when the module loads, which suits a progressively enhanced app.

The configuration

<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">

<!-- A real, server-rendered table: readable with JavaScript off. data-field on
     each header names the column that cell becomes, and data-id on each row is
     the key the grid carries it under. -->
<table id="read-only-rules-table" data-lattice-grid>
  <thead>
    <tr>
      <th data-field="id">Order</th>
      <th data-field="reference">Reference</th>
      <th data-field="stage">Stage</th>
      <th data-field="budget">Budget</th>
      <th data-field="circuits">Circuits</th>
    </tr>
  </thead>
  <tbody>
    <tr data-id="WO-00001"><td>WO-00001</td><td>REQ-9090</td><td>done</td><td>8580</td><td>27</td></tr>
    <tr data-id="WO-00002"><td>WO-00002</td><td>INC-7104</td><td>done</td><td>23160</td><td>20</td></tr>
    <tr data-id="WO-00003"><td>WO-00003</td><td>REQ-7362</td><td>blocked</td><td>17685</td><td>30</td></tr>
    <!-- the rest of the rows, rendered by your server -->
  </tbody>
</table>

<script type="module">
  import { hydrateTable } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/htmx.esm.min.js';

  // The markup carries the rows; a server-rendered table's column types are
  // declared in the config hydrateTable takes. The grid replaces the table
  // where it stood.
  const grid = hydrateTable(document.getElementById('read-only-rules-table'), {
    selection: 'multiple',
    edit: true,
    columns: [
      // No edit block at all, so the column is never editable. That is the
      // default rather than something to switch off.
      { field: 'id', title: 'Order', layout: { width: 120, pin: 'start' } },
      { field: 'reference', title: 'Reference', layout: { width: 130 } },
      { field: 'stage', title: 'Stage', filter: { type: 'set' }, edit: true },
      {
        // Editable only while the order is still open: the rule reads the row.
        field: 'budget', title: 'Budget', type: 'number', layout: { width: 140 },
        format: { style: 'currency', currency: 'GBP' }, total: 'sum',
        edit: { enabled: ({ data }) => data?.stage !== 'done' },
      },
      {
        field: 'circuits', title: 'Circuits', type: 'number', layout: { width: 110 },
        edit: { enabled: ({ data }) => data?.stage !== 'done' && data?.signedOff !== true },
      },
    ],
  });
</script>

Locking a column, or a single cell, without a second grid

Some columns should never open an editor, and some cells should only be locked depending on the row they sit in: a total column that is always computed, or a status column that stops being editable once an order is shipped. Lattice Grid handles both with one option, edit.enabled: (row) => …, set either as a plain boolean for a column-wide rule or as a function for a per-cell one. When the function returns false for a row, that cell never opens an editor on double-click or keystroke and is skipped by Tab-to-edit navigation, so a mixed grid of editable and locked cells needs no second read-only grid layered on top and no disabled overlay drawn over individual cells. The function receives the row already resolved for that cell, so a rule such as “locked once status is closed” costs one property check per cell rather than a pass over the dataset. Because the read-only state is expressed at the data layer rather than the render layer, a screen reader encounters the cell with the correct read-only role from the first render, not after an editor briefly mounts and is torn down. This is the mechanism a JavaScript data grid needs to mix computed, permission-gated, and freely editable cells in one table without maintaining a second component for the parts that cannot change.

How do I make specific cells read-only in a data grid?

Set edit.enabled on the column to a function of the row, returning false for any row where that cell should not be editable, or a plain false to lock the whole column. Locked cells skip the editor on double-click and keystroke, are excluded from Tab-to-edit focus order, and are read to assistive technology as non-editable from the first render, with no separate overlay or second grid required.