Background
A bound Wijmo FlexSheet displays the data source’s column names in the first sheet row. To show a report date above those names, wait for FlexSheet to create the bound header, then insert a styled row at the top of the sheet.
Steps to Complete
- Install the required packages.
- Load the Wijmo stylesheet.
- Import the Angular and Wijmo dependencies.
- Create a row with header styling.
- Insert the row above the bound header.
- Write the label and date into the new row.
- Bind the data and insert the row after loading.
Getting Started
Install required packages
npm install @mescius/wijmo.grid @mescius/wijmo.grid.sheet @mescius/wijmo.angular2.grid.sheet @mescius/wijmo.styles
- mescius/wijmo.grid provides the
Rowclass, mescius/wijmo.grid.sheet provides the core FlexSheet APIs andHeaderRow, mescius/wijmo.angular2.grid.sheet provides the Angular module and<wj-flex-sheet>component, and mescius/wijmo.styles supplies the stylesheet for the header styling.
Load the Wijmo stylesheet
@import '@mescius/wijmo.styles/wijmo.css';
- Add this import to your global
src/styles.cssso thewj-headerclass applies the Wijmo theme’s header styling.
Import the Angular and Wijmo dependencies
import { Component, OnDestroy } from '@angular/core';
import { Row } from '@mescius/wijmo.grid';
import { FlexSheet, HeaderRow } from '@mescius/wijmo.grid.sheet';
import { WjGridSheetModule } from '@mescius/wijmo.angular2.grid.sheet';
WjGridSheetModulemakes<wj-flex-sheet>available in the Angular template, whileRow,FlexSheet, andHeaderRowsupport the row insertion logic.
Create a row with header styling
function createDateRow(): Row {
const row = new Row();
row.cssClass = 'wj-header';
row.align = 'left';
return row;
}
- Add this helper below the imports. It creates a row, applies the Wijmo theme’s
wj-headerclass, and aligns its contents to the left.
Insert the row above the bound header
function insertDateRow(sheet: FlexSheet): void {
if (!(sheet.rows[0] instanceof HeaderRow)) {
return;
}
sheet.rows.splice(0, 0, createDateRow());
writeDate(sheet);
}
- Check that the first row is a
HeaderRowbefore inserting the date row. This confirms the bound header is in place and prevents duplicate date rows if the handler runs again. - Inserting at index
0moves the bound header to the second row, thenwriteDatepopulates the new first row.
Write the label and date into the new row
function writeDate(sheet: FlexSheet): void {
sheet.setCellData(0, 0, 'Date:', false);
sheet.setCellData(0, 1, new Date(), false);
}
setCellDatawritesDate:to A1 and the current date to B1. Passingfalseas the fourth argument tells FlexSheet to keep the supplied values without converting them to the bound columns’ data types.
Bind the data and insert the row after loading
@Component({
selector: 'app-report-sheet',
standalone: true,
imports: [WjGridSheetModule],
template: `
<wj-flex-sheet
#sheet
style="height: 320px"
(initialized)="initializeSheet(sheet)"
(loadedRows)="handleLoadedRows(sheet)">
</wj-flex-sheet>
`,
})
export class ReportSheetComponent implements OnDestroy {
private timer?: ReturnType<typeof setTimeout>;
initializeSheet(sheet: FlexSheet): void {
sheet.addBoundSheet('Country', [
{ country: 'US', product: 'Widget', amount: 125 },
{ country: 'Germany', product: 'Gadget', amount: 240 },
]);
}
handleLoadedRows(sheet: FlexSheet): void {
clearTimeout(this.timer);
this.timer = setTimeout(() => insertDateRow(sheet), 10);
}
ngOnDestroy(): void {
clearTimeout(this.timer);
}
}
- Add this component below the helpers. The
initializedhandler binds the sample data. WhenloadedRowsfires,setTimeoutdefers the insertion so FlexSheet can finish placing the bound header.ngOnDestroycancels any pending insertion when Angular removes the component.
With this setup, Angular FlexSheet waits for the bound rows to load, inserts a styled row above the data header, and writes the current date into that row.
Happy coding!
Andrew Peterson
Technical Engagement Engineer
