
Background
Wijmo FlexSheet displays gridlines through cell borders. In Vue, you can access the initialized FlexSheet control instance, then dynamically add or remove a CSS class from its host element to show or hide those gridlines.
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.vue2.grid.sheet @mescius/wijmo.styles
import { ref } from 'vue';
import * as wijmo from '@mescius/wijmo';
import { WjFlexSheet, WjSheet } from '@mescius/wijmo.vue2.grid.sheet';
import '@mescius/wijmo.styles/wijmo.css';
-
This imports Vue’s
ref, the Wijmo helper methods, the Vue FlexSheet components, and the Wijmo stylesheet.
Create the FlexSheet data source
const data = ref(getData(20));
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 and stores it in a Vue
refso it can be bound to the FlexSheet.
Add the FlexSheet markup
<template>
<div class="app">
<wj-flex-sheet
:initialized="initializeFlexSheet"
:showFilterIcons="false">
<wj-sheet :name="'Sheet 1'" :itemsSource="data"></wj-sheet>
</wj-flex-sheet>
<button type="button" @click="toggleGridLines">
Show/Hide Grid
</button>
</div>
</template>
-
The
wj-flex-sheetcomponent creates the FlexSheet control. Thewj-sheetcomponent binds the data source to a sheet namedSheet 1. SettingshowFilterIconstofalsehides the filter icons.
Store the FlexSheet instance
const flexSheet = ref(null);
function initializeFlexSheet(sheet) {
flexSheet.value = sheet;
}
-
The
initializedcallback provides the FlexSheet instance. Storing it lets the Vue component access the control later when the button is clicked.
Toggle the gridline CSS class
function toggleGridLines() {
if (!flexSheet.value) {
return;
}
const host = flexSheet.value.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, the gridlines are hidden; when it is removed, the 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 Vue setup, we created a FlexSheet with a bound data source, hid the filter icons, stored the initialized FlexSheet instance, and added a button that dynamically toggles the gridlines on and off.
Happy coding!
Andrew Peterson