demo D48
Conditional Row Styling: rowClass and rowStyle in htmx
Styling whole rows from the record, and how it survives recycling
rowClass · rowStyle
This grid styles whole rows from the record itself, a cancelled order or a suspended account, and keeps that styling correct as rows recycle on scroll. It expresses a row-level state once instead of colouring every cell to match by hand.
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.108.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="row-class-and-style-table" data-lattice-grid>
<thead>
<tr>
<th data-field="circuit">Circuit</th>
<th data-field="region">Region</th>
<th data-field="status">Status</th>
<th data-field="bandwidth">Bandwidth</th>
<th data-field="charge">Monthly charge</th>
</tr>
</thead>
<tbody>
<tr data-id="0"><td>DFB-EDI-100000</td><td>Edinburgh</td><td>live</td><td>100</td><td>94.88</td></tr>
<tr data-id="1"><td>MPL-BIR-100001</td><td>Birmingham</td><td>provisioning</td><td>10000</td><td>4324.6</td></tr>
<tr data-id="2"><td>DFB-LON-100002</td><td>London</td><td>live</td><td>2000</td><td>1031.02</td></tr>
<!-- the rest of the rows, rendered by your server -->
</tbody>
</table>
<style>
.lat-row.lg-demo-ceased { opacity: 0.55; font-style: italic; }
.lat-row.lg-demo-provisioning { box-shadow: inset 3px 0 0 #1a6bc7; }
</style>
<script type="module">
import { hydrateTable } from 'https://cdn.latticegrid.dev/lattice-grid@1.108.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('row-class-and-style-table'), {
// Both are re-evaluated on every repaint and are given the row rather than
// a cell, so they can style on something the row does not show. Name a
// class from rowClass (define it in CSS, as above); return a style object
// from rowStyle when the value is a continuum with no sensible set of classes.
rowClass: (p) => {
const status = p.data.status;
if (status === 'ceased') return 'lg-demo-ceased';
if (status === 'provisioning') return 'lg-demo-provisioning';
return '';
},
rowStyle: (p) => {
const charge = Number(p.data.charge) || 0;
if (charge < 1500) return {};
const weight = Math.min(1, (charge - 1500) / 2500);
return { background: `rgba(176, 48, 48, ${(weight * 0.14).toFixed(3)})` };
},
columns: [
{ field: 'circuit', title: 'Circuit', layout: { width: 180 } },
{ field: 'region', title: 'Region' },
{ field: 'status', title: 'Status', filter: { type: 'set' } },
{ field: 'bandwidth', title: 'Bandwidth', type: 'number', filter: { type: 'number' } },
{ field: 'charge', title: 'Monthly charge', type: 'number',
format: { style: 'currency', currency: 'GBP' }, filter: { type: 'number' } },
],
});
</script>
Styling a whole row from its own data
Some formatting decisions apply to a record, not a cell: a cancelled order, a suspended account, a row flagged for review. Colouring every cell to express that is repetitive and easy to leave inconsistent once a column is added. Lattice Grid exposes this through rowClass and rowStyle, which take the row’s data and return a class name or a style object applied to the row element itself, so status colouring lives in one place rather than being duplicated per column. A developer reaches for this when the condition concerns the record as a whole, such as shading every row where status is "overdue", or giving archived rows a muted background regardless of which columns are visible.
The detail that matters in a JavaScript data grid built on row recycling is that the class or style must be re-evaluated as rows are recycled, not set once at first render. Lattice Grid calls rowClass and rowStyle again each time a recycled row element is bound to new data, so a virtualised view never leaves a stale class from a previous record underneath fresh data. The callback runs against plain row data with no DOM read, so scroll cost is a function call per row rather than a layout-triggering style read, keeping row styling in step with virtual scrolling on large datasets.
How do I style a whole row based on its data in a JavaScript grid?
Use rowClass for a class name or rowStyle for inline styles, both configured as a function that receives the row’s data and returns the class or style to apply to that row’s element. Lattice Grid re-evaluates the function whenever a row is bound during scrolling, including when the underlying DOM row is recycled for a different record, so the styling always matches the data currently shown rather than the row position.