demo D48
Row class and row style
Styling whole rows from the record, and how it survives recycling
rowClass · rowStyle
The configuration
import * as ng from '@angular/core';
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import createLatticeGrid from '@toclocoinc/lattice-grid/modules/angular';
const { LatticeGridComponent } = createLatticeGrid({ ng, 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 */];
@Component({
selector: 'app-root',
standalone: true,
imports: [LatticeGridComponent],
template: '<lattice-grid [config]="grid" style="display:block;height:540px"></lattice-grid>',
})
export class AppComponent {
grid = {
rowKey: 'id',
rowClass: rowClass,
rowStyle: rowStyle,
columns: columns,
rows: rows,
};
}
bootstrapApplication(AppComponent);
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.