Lattice Grid Buy a licence

demo D48

Row class and row style

Styling whole rows from the record, and how it survives recycling

rowClass · rowStyle

Building…
Loading a live grid…

The configuration

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/lattice-grid.min.css">

<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>

<div id="grid"></div>

<script type="module">
  import React from 'https://esm.sh/react@18';
  import { createRoot } from 'https://esm.sh/react-dom@18/client';
  import { createGrid } from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/lattice-grid.esm.min.js';
  import createLatticeGrid from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/modules/react.esm.min.js';

  const LatticeGrid = createLatticeGrid({ React, createGrid });

  // 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.
  const rowClass = (p) => {
    const status = p.data.status;
    if (status === 'ceased') return 'lg-demo-ceased';
    if (status === 'provisioning') return 'lg-demo-provisioning';
    return '';
  };

  const 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)})` };
  };

  const 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' } },
  ];

  const rows = [/* circuit records */];

  function App() {
    return (
      <LatticeGrid
        rowKey="id"
        rowClass={rowClass}
        rowStyle={rowStyle}
        columns={columns}
        rows={rows}
        style={{ height: '540px' }}
      />
    );
  }

  createRoot(document.getElementById('grid')).render(<App />);
</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.