Column Spanning
With LyteNyte Grid, cells can span multiple columns. When a cell spans, it extends into adjacent columns, and the grid skips rendering any cells it covers.
The colSpan property on a column definition controls how
many columns a cell spans. It accepts either a number or a function.
- Provide a number to apply the same span to all rows; or
- Provide a function that returns a number to vary the span per row
Cells always span toward the end of the viewport. A spanning cell covers the next column, not the previous one.
Uniform Column Spans
When colSpan is a number, every cell in the column spans the same number of
adjacent columns for all rows. The grid skips rendering any cells covered
by the span. The demo below shows the USD Price column spanning over the
adjacent GBP Price column.
Uniform Column Span
Uniform spans are uncommon and mainly appear in cases where the visual layout differs from the logical column structure. Hence, these types of column spans are used sparingly.
const columns = [// other columns,{ field: 3, id: "price", type: "number", colSpan: 2 },];
Function Column Spans
You can set colSpan to a function that returns the span for each row.
This allows dynamic spans based on row data. The demo below merges cells
for rows whose temperature values differ by less than 3.
Function Column Span
The colSpan function below shows how flexible span logic can be:
function joinSimilarTemps(x: CellSpanFnParams<MonthlyTemperature>) {if (x.row.kind !== "leaf" || !x.row.data) return 1;const temps = x.row.data.temps;const myOrder = ordering[x.colIndex - 1];const value = temps[myOrder];let count = 1;let i = x.colIndex; // Index is offset by 1 due to the year columnwhile (true) {const next = ordering[i];if (!next) break;const nextValue = temps[next];if (Math.abs(nextValue - value) < 3) {count++;i++;} else {break;}}return count;}
Column Span Scanning Distance
For performance, LyteNyte Grid does not pre-calculate all spans.
Instead, it scans backward from the first visible column based on
the scroll position. Control this lookback
using the colScanDistance property:
const grid = Grid.useLyteNyte({// Other grid propscolScanDistance: 100,});
colScanDistance must be at least the maximum possible span
in your grid. Ensure no span exceeds this value
to guarantee correct rendering.
LyteNyte Grid uses a scan distance to ensure correctness and performance. Many grids pre-calculate spans, which limits scalability, or render incorrectly during scrolling. LyteNyte Grid's model requires you to specify a maximum span, but it maintains accuracy and performance.
In most real-world cases, a scan distance of 100 is more than enough, since spans rarely approach this size.
Next Steps
- Column Base: See the default column values and how to override them.
- Column Resizing: Change column widths programmatically or through user interaction.
- Column ID & Name: Define user-friendly column names and ensure unique IDs.
- Column Field: Control how a column retrieves each cell's value.