Background
Wijmo FlexSheet displays gridlines through cell borders. In JavaScript, you can create the FlexSheet control, store the returned control instance, and toggle a custom CSS class on its host element to show or hide the gridlines dynamically.
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.styles
import '@mescius/wijmo.styles/wijmo.css';
import * as wijmo from '@mescius/wijmo';
import * as wjFlexSheet from '@mescius/wijmo.grid.sheet';
import './styles.css';
- This imports the Wijmo stylesheet, the Wijmo core utilities, and the FlexSheet control.
Create the FlexSheet data source
function getData(count) {
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 returned array is later passed to a bound sheet.
Add the FlexSheet markup
<div id="flexSheet"></div>
<br />
<br />
<button id="toggleGridLines" type="button">
Show/Hide Grid
</button>
-
The
divelement hosts the FlexSheet control. The button calls the JavaScript logic that toggles the gridlines.
Store the FlexSheet instance
let flexSheet = null;
function init() {
const source = getData(20);
flexSheet = new wjFlexSheet.FlexSheet('#flexSheet', {
showFilterIcons: false
});
flexSheet.addBoundSheet('Sheet 1', source);
}
-
The
FlexSheetconstructor creates the control and returns the control instance. Storing it inflexSheetlets you access the control later. TheshowFilterIcons: falseoption hides the filter icons.
Toggle the gridline CSS class
function toggleGridLines() {
if (!flexSheet) {
return;
}
const host = flexSheet.hostElement;
wijmo.toggleClass(host, 'no-grid', !wijmo.hasClass(host, 'no-grid'));
}
- This gets the FlexSheet host element and toggles the
no-gridCSS class. When the class is present, 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 cell borders when the
no-gridclass is applied.
With this JavaScript setup, we created a FlexSheet with a bound data source, hid the filter icons, stored the FlexSheet instance, and added a button that dynamically toggles the gridlines on and off.
Happy coding!
Andrew Peterson
