Auto-Size Rows Asynchronously

Auto-sizing all rows on grid with a lot of data is a time-consuming process. This sample demonstrates how you can auto-size rows asynchronously to improve performance. The code listens to events and auto-sizes only rows that are currently in view and have not been auto-sized yet.

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import * as wjGrid from '@grapecity/wijmo.grid'; import { getCountries } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // // create some random data var data = []; for (var i = 0; i < 10000; i++) { data.push({ id: i, countries: getCountries(), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // // show the data in a grid with fixed height var theGrid = new wjGrid.FlexGrid('#theGrid', { autoGenerateColumns: false, columns: [ { binding: 'id', header: 'ID', isReadOnly: true }, { binding: 'countries', header: 'Countries', width: 150, wordWrap: true }, { binding: 'sales', header: 'Sales' }, { binding: 'expenses', header: 'Expenses' } ], // // auto-size all rows when they are loaded loadedRows: function (s, e) { autoSizeRowsAsync(s); }, // // auto-size all rows when a wrapping column is resized resizedColumn: function (s, e) { if (s.columns[e.col].wordWrap) { autoSizeRowsAsync(s); } }, // // auto-size a single row when a wrapping cell is edited cellEditEnded: function (s, e) { if (s.columns[e.col].wordWrap) { autoSizeRowsAsync(s, e.row); } }, // // autosize rows within view that haven't been autosized yet scrollPositionChanged: function (s, e) { var vr = s.viewRange; for (var r = vr.topRow; r <= vr.bottomRow; r++) { if (s.rows[r].height == null) { s.autoSizeRows(r, r); //console.log('autosized row ' + r) } } }, // // load some data itemsSource: data }); // // reset row heights to null and raise scrollPositionChanged event // to auto-size the rows that are in view and haven't been auto-sized function autoSizeRowsAsync(grid, rowIndex) { if (rowIndex != null) { grid.rows[rowIndex].height = null; } else { grid.rows.forEach(function (row) { row.height = null; }); } setTimeout(function () { grid.onScrollPositionChanged(); }); } // // synchronouse auto-size (to show how long it would take) document.getElementById('asSync').addEventListener('click', function () { var start = Date.now(); theGrid.autoSizeRows(); alert('AutoSized all rows in ' + (Date.now() - start) + 'ms'); }); // } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid Auto-size Rows asynchronously</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <button id="asSync" class="btn btn-default">AutoSizeRows (synchronously)</button> <div id="theGrid"></div> </div> </body> </html> export function getCountries() { var countries = 'Austria,Belgium,Canada,Denmark,Estonia,Finland,Germany,Hungary,Ireland,Japan,Korea,Lebanon,Mexico,Norway,Portugal,Qatar,Romania,Spain,Turkey,Ukraine,Venezuela,Zaire'.split(','); var c = []; var cnt = Math.round(Math.random() * 8) + 1; var start = Math.round(Math.random() * countries.length); for (var i = 0; i < cnt; i++) { c.push(countries[(i + start) % countries.length]); } return c.join(', '); } body { margin-bottom: 36pt; } .wj-flexgrid { max-height: 300px; margin: 6px 0; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wjGrid from '@grapecity/wijmo.grid'; import { Component, Inject, enableProdMode, NgModule, ViewChild } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { WjGridModule } from '@grapecity/wijmo.angular2.grid'; import { DataService } from './app.data'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { @ViewChild('flexGrid') theGrid: wjGrid.FlexGrid; data: any; // DataSvc will be passed by derived classes constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(); } // onLoadedRows(grid: wjGrid.FlexGrid) { this.autoSizeRowsAsync(grid); } // onCellEditEnded(grid: wjGrid.FlexGrid, e: wjGrid.CellEditEndingEventArgs) { if (grid.columns[e.col].wordWrap) { this.autoSizeRowsAsync(grid, e.row); } } // onScrollPositionChanged(grid: wjGrid.FlexGrid, e: wjGrid.CellRangeEventArgs) { var vr = grid.viewRange; for (var r = vr.topRow; r <= vr.bottomRow; r++) { if (grid.rows[r].height == null) { grid.autoSizeRows(r, r); //console.log('autosized row ' + r) } } } // onResizedColumn(grid: wjGrid.FlexGrid, e: wjGrid.CellRangeEventArgs) { if (grid.columns[e.col].wordWrap) { this.autoSizeRowsAsync(grid); } } // onAsSyncClick() { var start = Date.now(); this.theGrid.autoSizeRows(); alert('AutoSized all rows in ' + (Date.now() - start) + 'ms'); } // autoSizeRowsAsync(grid: wjGrid.FlexGrid, rowIndex?: number) { if (rowIndex != null) { grid.rows[rowIndex].height = null; } else { grid.rows.forEach(function (row) { row.height = null; }); } setTimeout(function () { grid.onScrollPositionChanged(); }) } } //\\ @NgModule({ imports: [WjGridModule, BrowserModule], declarations: [AppComponent], providers: [DataService], bootstrap: [AppComponent] }) export class AppModule { } // enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid Auto-size Rows asynchronously</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <button (click)="onAsSyncClick()" class="btn btn-default">AutoSizeRows (synchronously)</button> <wj-flex-grid #flexGrid [itemsSource]="data" [autoGenerateColumns]=false [quickAutoSize]=true (loadedRows)="onLoadedRows(flexGrid)" (cellEditEnded)="onCellEditEnded(flexGrid,$event)" (scrollPositionChanged)="onScrollPositionChanged(flexGrid,$event)" (resizedColumn)="onResizedColumn(flexGrid,$event)"> <wj-flex-grid-column [binding]="'id'" [header]="'ID'" [minWidth]=60 [isReadOnly]=true></wj-flex-grid-column> <wj-flex-grid-column [binding]="'countries'" [header]="'Countries'" [width]="150" [wordWrap]=true> </wj-flex-grid-column> <wj-flex-grid-column [binding]="'sales'" [header]="'Sales'"></wj-flex-grid-column> <wj-flex-grid-column [binding]="'expenses'" [header]="'Expenses'"></wj-flex-grid-column> </wj-flex-grid> </div> import { Injectable } from '@angular/core'; interface DataItem { id: number; countries: string; sales: number; expenses: number; } function getCountries(): string { var countries = 'Austria,Belgium,Canada,Denmark,Estonia,Finland,Germany,Hungary,Ireland,Japan,Korea,Lebanon,Mexico,Norway,Portugal,Qatar,Romania,Spain,Turkey,Ukraine,Venezuela,Zaire'.split(','); var c = []; var cnt = Math.round(Math.random() * 8) + 1; var start = Math.round(Math.random() * countries.length); for (var i = 0; i < cnt; i++) { c.push(countries[(i + start) % countries.length]); } return c.join(', '); } @Injectable() export class DataService { getData(): DataItem[] { var data = []; for (var i = 0; i < 10000; i++) { data.push({ id: i, countries: getCountries(), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } return data; } } body { margin-bottom: 36pt; } .wj-flexgrid { max-height: 300px; margin: 6px 0; } <template> <div class="container-fluid"> <button @click="onAsSyncClick()" class="btn btn-default">AutoSizeRows (synchronously)</button> <wj-flex-grid :initialized="initialized" :itemsSource="data" :autoGenerateColumns="false" :cellEditEnded="onCellEditEnded" :scrollPositionChanged="onScrollPositionChanged" :resizedColumn="onResizedColumn"> <wj-flex-grid-column :binding="'id'" :header="'ID'" :minWidth="60" :isReadOnly="true"></wj-flex-grid-column> <wj-flex-grid-column :binding="'countries'" :header="'Countries'" :width="150" :wordWrap="true" ></wj-flex-grid-column> <wj-flex-grid-column :binding="'sales'" :header="'Sales'"></wj-flex-grid-column> <wj-flex-grid-column :binding="'expenses'" :header="'Expenses'"></wj-flex-grid-column> </wj-flex-grid> </div> </template> <script> import "@grapecity/wijmo.styles/wijmo.css"; import "bootstrap.css"; import Vue from "vue"; import * as wjGrid from "@grapecity/wijmo.grid"; import "@grapecity/wijmo.vue2.core"; import "@grapecity/wijmo.vue2.grid"; import { getData } from "./data"; let App = Vue.extend({ name: "app", data: function() { return { data: getData() }; }, mounted: function() { this.autoSizeRowsAsync(this.theGrid); }, methods: { onLoadedRows: function(grid) { this.autoSizeRowsAsync(grid); }, onCellEditEnded: function(grid, e) { if (grid.columns[e.col].wordWrap) { this.autoSizeRowsAsync(grid, e.row); } }, onScrollPositionChanged: function(grid, e) { var vr = grid.viewRange; for (var r = vr.topRow; r <= vr.bottomRow; r++) { if (grid.rows[r].height == null) { grid.autoSizeRows(r, r); } } }, onResizedColumn: function(grid, e) { if (grid.columns[e.col].wordWrap) { this.autoSizeRowsAsync(grid); } }, onAsSyncClick: function() { var start = Date.now(); this.theGrid.autoSizeRows(); alert("AutoSized all rows in " + (Date.now() - start) + "ms"); }, autoSizeRowsAsync: function(grid, rowIndex) { if (rowIndex != null) { grid.rows[rowIndex].height = null; } else { grid.rows.forEach(function(row) { row.height = null; }); } setTimeout(function() { grid.onScrollPositionChanged(); }); }, initialized: function(theGrid) { this.theGrid = theGrid; } } }); new Vue({ render: h => h(App) }).$mount("#app"); </script> <style> .container-fluid .wj-flexgrid { max-height: 300px; margin: 6px 0; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid Auto-size Rows asynchronously</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/jszip/dist/jszip.js"></script> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"> </div> </body> </html> export function getData() { var data = []; for (var i = 0; i < 10000; i++) { data.push({ id: i, countries: getCountries(), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } return data; } function getCountries() { var countries = 'Austria,Belgium,Canada,Denmark,Estonia,Finland,Germany,Hungary,Ireland,Japan,Korea,Lebanon,Mexico,Norway,Portugal,Qatar,Romania,Spain,Turkey,Ukraine,Venezuela,Zaire'.split(','); var c = []; var cnt = Math.round(Math.random() * 8) + 1; var start = Math.round(Math.random() * countries.length); for (var i = 0; i < cnt; i++) { c.push(countries[(i + start) % countries.length]); } return c.join(', '); } import "@grapecity/wijmo.styles/wijmo.css"; import "bootstrap.css"; import "./app.css"; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; import * as wjcGrid from "@grapecity/wijmo.react.grid"; import { getData } from "./data"; class App extends React.Component { constructor(props) { super(props); this.state = { data: getData() }; } render() { return <div className="container-fluid"> <button onClick={this.onAsSyncClick.bind(this)} className="btn btn-default">AutoSizeRows (synchronously)</button> <wjcGrid.FlexGrid itemsSource={this.state.data} loadedRows={this.onLoadedRows.bind(this)} cellEditEnded={this.onCellEditEnded.bind(this)} scrollPositionChanged={this.onScrollPositionChanged.bind(this)} resizedColumn={this.onResizedColumn.bind(this)} initialized={this.initialized.bind(this)}> <wjcGrid.FlexGridColumn binding="id" header="ID" minWidth={60} isReadOnly={true}></wjcGrid.FlexGridColumn> <wjcGrid.FlexGridColumn binding="countries" header="Countries" width="*" wordWrap={true}></wjcGrid.FlexGridColumn> <wjcGrid.FlexGridColumn binding="sales" header="Sales"></wjcGrid.FlexGridColumn> <wjcGrid.FlexGridColumn binding="expenses" header="Expenses"></wjcGrid.FlexGridColumn> </wjcGrid.FlexGrid> </div>; } onLoadedRows(grid) { this.autoSizeRowsAsync(grid); } onCellEditEnded(grid, e) { if (grid.columns[e.col].wordWrap) { this.autoSizeRowsAsync(grid, e.row); } } onScrollPositionChanged(grid, e) { var vr = grid.viewRange; for (var r = vr.topRow; r <= vr.bottomRow; r++) { if (grid.rows[r].height == null) { grid.autoSizeRows(r, r); } } } onResizedColumn(grid, e) { if (grid.columns[e.col].wordWrap) { this.autoSizeRowsAsync(grid); } } onAsSyncClick() { var start = Date.now(); this.theGrid.autoSizeRows(); alert("AutoSized all rows in " + (Date.now() - start) + "ms"); } autoSizeRowsAsync(grid, rowIndex) { if (rowIndex != null) { grid.rows[rowIndex].height = null; } else { grid.rows.forEach(function (row) { row.height = null; }); } setTimeout(function () { grid.onScrollPositionChanged(); }); } initialized(theGrid) { this.theGrid = theGrid; } } ReactDOM.render(<App />, document.getElementById('app')); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Grapecity Wijmo OLAP Pivot Chart Overview</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div id="app"></div> </body> </html> .container-fluid .wj-flexgrid { max-height: 300px; margin: 6px 0; } export function getData() { var data = []; for (var i = 0; i < 10000; i++) { data.push({ id: i, countries: getCountries(), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } return data; } function getCountries() { var countries = 'Austria,Belgium,Canada,Denmark,Estonia,Finland,Germany,Hungary,Ireland,Japan,Korea,Lebanon,Mexico,Norway,Portugal,Qatar,Romania,Spain,Turkey,Ukraine,Venezuela,Zaire'.split(','); var c = []; var cnt = Math.round(Math.random() * 8) + 1; var start = Math.round(Math.random() * countries.length); for (var i = 0; i < cnt; i++) { c.push(countries[(i + start) % countries.length]); } return c.join(', '); }