Background
Wijmo FlexSheet displays gridlines by rendering borders around its cells. In Angular, you can toggle these gridlines dynamically by getting a reference to the FlexSheet control and adding or removing a custom CSS class from the control’s host element.
Steps to Complete
- Install and import the required Wijmo packages.
- Create the FlexSheet data source.
- Add the FlexSheet markup.
- Store the FlexSheet instance.
- Toggle the gridline CSS class.
- Add CSS to hide the gridlines.
Getting Started
Install and import the required Wijmo packages
npm install @mescius/wijmo @mescius/wijmo.grid.sheet @mescius/wijmo.angular2.grid.sheet @mescius/wijmo.styles
import { Component } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import * as wijmo from '@mescius/wijmo';
import * as wjcSheet from '@mescius/wijmo.grid.sheet';
import { WjGridSheetModule } from '@mescius/wijmo.angular2.grid.sheet';
- The
WjGridSheetModulepackage provides the Angular FlexSheet components, while the core Wijmo package provides helper methods such astoggleClassandhasClass.
Create the FlexSheet data source
data = this.getData(20);
private getData(count: number) {
const countries = 'US,Germany,UK,Japan,Italy,Greece'.split(',');
const data = [];
for (let i = 0; i < count; i++) {
const condition = Boolean(Math.floor(Math.random() + 0.5));
data.push({
id: i,
country: countries[i % countries.length],
downloads: Math.random() * 100000,
mixed: condition
? ''
: countries[Math.floor(Math.random() * countries.length)],
checked: condition
});
}
return data;
}
- This creates sample data that will be displayed in the FlexSheet. The
dataarray is later bound to the sheet through theitemsSourceproperty.
Add the FlexSheet markup
<wj-flex-sheet
#flex
[showFilterIcons]="false"
(initialized)="initializeFlexSheet(flex)">
<wj-sheet [name]="'Sheet 1'" [itemsSource]="data"></wj-sheet>
</wj-flex-sheet>
<br />
<br />
<button type="button" (click)="toggleGridLines()">
Show/Hide Grid
</button>
- The
wj-flex-sheetcomponent creates the FlexSheet control. Thewj-sheetcomponent binds the data source to a sheet namedSheet 1. Setting[showFilterIcons]="false"hides the filter icons.
Store the FlexSheet instance
private flexSheet: wjcSheet.FlexSheet | null = null;
initializeFlexSheet(flex: wjcSheet.FlexSheet) {
this.flexSheet = flex;
}
- The
initializedevent passes the FlexSheet control instance to the component. Storing it lets you access the FlexSheet later from button click handlers or other component methods.
Toggle the gridline CSS class
toggleGridLines() {
if (this.flexSheet) {
const host = this.flexSheet.hostElement;
wijmo.toggleClass(host, 'no-grid', !wijmo.hasClass(host, 'no-grid'));
}
}
-
This method gets the FlexSheet host element and toggles the
no-gridCSS class. When the class is added, gridlines are hidden; when it is removed, gridlines are shown again.
Add CSS to hide the gridlines
.wj-flexsheet {
height: 500px;
}
.no-grid.wj-flexsheet .wj-cells .wj-cell {
border: none;
}
- The first rule gives the FlexSheet a visible height. The second rule removes the cell borders when the
no-gridclass is applied to the FlexSheet host element.
With this Angular setup, we created a FlexSheet with a bound data source, stored the initialized FlexSheet instance, hid the filter icons, and added a button that dynamically toggles a CSS class to show or hide the gridlines.
Happy coding!
Andrew Peterson
Technical Engagement Engineer
