Skip to content

Pre-populate new rows with default values when users add rows to the grid.

Basic spare rows

To keep one empty row at the bottom of the grid, set minSpareRows to 1.

TypeScript
/* file: app.component.ts */
import { Component } from '@angular/core';
import { GridSettings, HotTableModule } from '@handsontable/angular-wrapper';
@Component({
selector: 'app-example1',
template: `
<hot-table [settings]="gridSettings" [data]="hotData"></hot-table>
`,
standalone: true,
imports: [HotTableModule],
})
export class AppComponent {
readonly hotData = [
['', 'Tesla', 'Nissan', 'Toyota', 'Honda'],
['2017', 10, 11, 12, 13],
['2018', 20, 11, 14, 13],
['2019', 30, 15, 12, 13],
];
readonly gridSettings: GridSettings = {
minSpareRows: 1,
height: 'auto',
autoWrapRow: true,
autoWrapCol: true,
};
}
/* end-file */
/* file: app.config.ts */
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { registerAllModules } from 'handsontable/registry';
import { HOT_GLOBAL_CONFIG, HotGlobalConfig, NON_COMMERCIAL_LICENSE } from '@handsontable/angular-wrapper';
registerAllModules();
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
{
provide: HOT_GLOBAL_CONFIG,
useValue: { license: NON_COMMERCIAL_LICENSE } as HotGlobalConfig,
},
],
};
/* end-file */
HTML
<div>
<app-example1></app-example1>
</div>

Spare rows with placeholder styling

To hint what to enter in the spare row, add a custom cell renderer that displays greyed-out placeholder text in empty cells. The renderer checks whether the whole row is empty, then shows a template value in a lighter color.

TypeScript
/* file: app.component.ts */
import { Component } from '@angular/core';
import { GridSettings, HotTableModule } from '@handsontable/angular-wrapper';
import Handsontable from 'handsontable/base';
import { textRenderer } from 'handsontable/renderers/textRenderer';
const templateValues = ['one', 'two', 'three'];
function isEmptyRow(instance: Handsontable, row: number) {
const rowData = instance.getDataAtRow(row);
for (let i = 0, ilen = rowData.length; i < ilen; i++) {
if (rowData[i] !== null) {
return false;
}
}
return true;
}
const defaultValueRenderer = (
instance: Handsontable,
td: HTMLTableCellElement,
row: number,
col: number,
prop: string | number,
value: Handsontable.CellValue,
cellProperties: Handsontable.CellProperties
) => {
if (value === null && isEmptyRow(instance, row)) {
value = templateValues[col];
td.style.color = '#999';
} else {
td.style.color = '';
}
textRenderer(instance, td, row, col, prop, value, cellProperties);
};
@Component({
selector: 'app-example2',
template: `
<hot-table [settings]="gridSettings" [data]="hotData"></hot-table>
`,
standalone: true,
imports: [HotTableModule],
})
export class AppComponent {
readonly hotData = [
['', 'Tesla', 'Nissan', 'Toyota', 'Honda'],
['2017', 10, 11, 12, 13],
['2018', 20, 11, 14, 13],
['2019', 30, 15, 12, 13],
];
readonly gridSettings: GridSettings = {
minSpareRows: 1,
height: 'auto',
autoWrapRow: true,
autoWrapCol: true,
cells() {
return { renderer: defaultValueRenderer };
},
};
}
/* end-file */
/* file: app.config.ts */
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { registerAllModules } from 'handsontable/registry';
import { HOT_GLOBAL_CONFIG, HotGlobalConfig, NON_COMMERCIAL_LICENSE } from '@handsontable/angular-wrapper';
registerAllModules();
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
{
provide: HOT_GLOBAL_CONFIG,
useValue: { license: NON_COMMERCIAL_LICENSE } as HotGlobalConfig,
},
],
};
/* end-file */
HTML
<div>
<app-example2></app-example2>
</div>

Auto-populating with template values

For full pre-population, use the beforeChange hook to fill all cells in a spare row with template values the moment the user starts editing. The isEmptyRow() helper detects whether the row is untouched, and the hook pushes changes for every column except the one the user is editing.

TypeScript
/* file: app.component.ts */
import { AfterViewInit, Component, ViewChild } from '@angular/core';
import {GridSettings, HotTableComponent, HotTableModule} from '@handsontable/angular-wrapper';
import Handsontable from 'handsontable/base';
import { textRenderer } from 'handsontable/renderers/textRenderer';
@Component({
selector: 'app-example3',
template: `
<hot-table
[settings]="hotSettings!" [data]="hotData">
</hot-table>
`,
standalone: true,
imports: [HotTableModule],
})
export class AppComponent implements AfterViewInit {
@ViewChild(HotTableComponent, {static: false}) hotTable!: HotTableComponent;
hotData = [
['', 'Tesla', 'Nissan', 'Toyota', 'Honda'],
['2017', 10, 11, 12, 13],
['2018', 20, 11, 14, 13],
['2019', 30, 15, 12, 13],
];
hotSettings: GridSettings = {};
ngAfterViewInit() {
const templateValues = ['one', 'two', 'three'];
function isEmptyRow(instance: Handsontable, row: number) {
const rowData = instance.getDataAtRow(row);
for (let i = 0, ilen = rowData.length; i < ilen; i++) {
if (rowData[i] !== null) {
return false;
}
}
return true;
}
const defaultValueRenderer = (
instance: Handsontable,
td: HTMLTableCellElement,
row: number,
col: number,
prop: string | number,
value: Handsontable.CellValue,
cellProperties: Handsontable.CellProperties
) => {
if (value === null && isEmptyRow(instance, row)) {
value = templateValues[col];
td.style.color = '#999';
} else {
td.style.color = '';
}
textRenderer(
instance,
td,
row,
col,
prop,
value,
cellProperties
);
};
this.hotSettings = {
startRows: 8,
startCols: 5,
minSpareRows: 1,
contextMenu: true,
height: 'auto',
licenseKey: 'non-commercial-and-evaluation',
cells() {
return { renderer: defaultValueRenderer };
},
beforeChange: (changes) => {
const instance = this.hotTable.hotInstance!;
const columns = instance.countCols();
const rowColumnSeen: Record<string, boolean> = {};
const rowsToFill: Record<string, boolean> = {};
const ch = changes === null ? [] : changes!;
for (let i = 0; i < ch.length; i++) {
// if oldVal is empty
if (ch[i]![2] === null && ch[i]![3] !== null) {
if (isEmptyRow(instance, ch[i]![0])) {
// add this row/col combination to the cache so it will not be overwritten by the template
rowColumnSeen[`${ch[i]![0]}/${ch[i]![1]}`] = true;
rowsToFill[String(ch[i]![0])] = true;
}
}
}
for (const r in rowsToFill) {
if (rowsToFill.hasOwnProperty(r)) {
for (let c = 0; c < columns; c++) {
// if it is not provided by user in this change set, take the value from the template
if (!rowColumnSeen[`${r}/${c}`]) {
ch.push([Number(r), c, null, templateValues[c]]);
}
}
}
}
},
autoWrapRow: true,
autoWrapCol: true,
}
}
}
/* end-file */
/* file: app.config.ts */
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { registerAllModules } from 'handsontable/registry';
import { HOT_GLOBAL_CONFIG, HotGlobalConfig, NON_COMMERCIAL_LICENSE } from '@handsontable/angular-wrapper';
// register Handsontable's modules
registerAllModules();
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
{
provide: HOT_GLOBAL_CONFIG,
useValue: { license: NON_COMMERCIAL_LICENSE } as HotGlobalConfig,
},
],
};
/* end-file */
HTML
<div>
<app-example3></app-example3>
</div>

Pre-populate from an adjacent row

You can copy values from the row above when the user inserts a new row. Use the afterCreateRow hook to read the source row’s data and write it to the newly created row.

const hot = new Handsontable(container, {
data: [
['Product A', 10, 'active'],
['Product B', 20, 'inactive'],
],
colHeaders: ['Name', 'Quantity', 'Status'],
afterCreateRow(index) {
if (index > 0) {
const sourceRow = hot.getSourceDataAtRow(index - 1);
sourceRow.forEach((value, col) => {
hot.setDataAtCell(index, col, value);
});
}
},
licenseKey: 'non-commercial-and-evaluation',
});

Pre-populate from a server-fetched default

You can fetch default row values from a server and apply them when the user adds a new row. Use the afterCreateRow hook to trigger the request and populate the row once the response arrives.

const hot = new Handsontable(container, {
data: [],
colHeaders: ['Name', 'Quantity', 'Status'],
afterCreateRow(index) {
fetch('/api/row-defaults')
.then((response) => response.json())
.then((defaults) => {
hot.setDataAtRow(index, [defaults.name, defaults.quantity, defaults.status]);
});
},
licenseKey: 'non-commercial-and-evaluation',
});

Result

Your grid keeps one or more empty rows at the bottom. Depending on the approach, spare rows show greyed-out placeholder text or auto-fill all cells with template values when the user starts editing.

Hooks