グリッド:遅延読み込み

下のグリッドでは、折りたたまれたノードには1つのダミーの子ノードがあります。ノードが展開されると、必要に応じてノードが読み込まれます。この手法は一般的に「遅延読み込み」と呼ばれます。

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import * as wjGrid from '@grapecity/wijmo.grid'; import * as wjCore from '@grapecity/wijmo'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // // create the grid var theGrid = new wjGrid.FlexGrid('#theGrid', { childItemsPath: 'children', headersVisibility: 'Column', groupCollapsedChanged: groupCollapsedChanged, autoGenerateColumns: false, columns: [ { binding: 'name', header: '顧客名', width: '*' }, { binding: 'id', header: 'ID', align: 'center', cssClass: 'id-column' } ], itemsSource: getData(), }); // // start collapsed theGrid.collapseGroupsToLevel(0); // // update row when items are loaded function updateRowCount(grid) { document.getElementById('rowCount').textContent = wjCore.Globalize.format(grid.rows.length, 'n0'); } updateRowCount(theGrid); // // load data when collapse node is expanded function groupCollapsedChanged(s, e) { var row = s.rows[e.row], item = row.dataItem; // // did we just expand a node with a dummy child? if (!row.isCollapsed && item.children.length == 1 && item.children[0].name == '') { // // can't lazy load while updating rows if (s.rows.isUpdating) { row.isCollapsed = true; return; } // // replace the dummy child with actual nodes item.children.length = 0; var cnt = Math.round(Math.random() * 5) + 1; for (var i = 0; i < cnt; i++) { var node = createNode(); item.children.push(node); } // // refresh the view s.collectionView.refresh(); // // collapse the new item's child items for (var i = row.index + 1; i < s.rows.length; i++) { var childRow = s.rows[i]; if (childRow.level <= row.level) { break; } childRow.isCollapsed = true; } // // update row count updateRowCount(s); } } var nodeId; function createNode(dummy) { var first = '佐藤,鈴木,高橋,田中,伊藤,山本,渡辺,中村,小林,加藤,吉田,山田,佐々木,山口,松本,井上,木村,斎藤,林,清水,山崎,阿部,森,池田,橋本,山下,石川,中島,前田,藤田'.split(','), last = '浩,誠,健一,大輔,達也,翔太,浩一,哲也,剛,大介,健太,拓也,豊,修,和彦,学,直樹,健太郎,浩二,徹,隆,亮,翔,恵子,久美子,由美子,明美,直美,陽子,智子,絵美,恵,裕子,愛,真由美,由美,麻衣,美穂,愛美,彩'.split(','), name = dummy ? '' : getOneOf(first) + ' ' + getOneOf(last), children = []; if (!dummy) { children.push(createNode(true)); } if (nodeId == null) nodeId = 0; return { id: nodeId++, name: name, children: children }; } function getOneOf(arr) { return arr[Math.floor(Math.random() * arr.length)]; } // create/retrieve data function getData() { var tree = []; tree.push(createNode()); tree.push(createNode()); return tree; } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid Lazy Loading Tree Grid</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"> <div id="theGrid"> </div> <p> 現在の行数: <b id="rowCount"></b> </p> </div> </body> </html> .wj-flexgrid { max-height: 300px; margin: 12px; } .wj-flexgrid .wj-cell { padding: 8px; } .wj-flexgrid .wj-group:not(.wj-state-selected):not(.wj-state-multi-selected) { background-color: white; } .id-column { font-style: italic; } document { margin-bottom: 48pt; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wjCore from '@grapecity/wijmo'; import * as wjGrid from '@grapecity/wijmo.grid'; import { Component, Inject, enableProdMode, NgModule, ViewChild, AfterViewInit } 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 implements AfterViewInit { @ViewChild('theGrid') theGrid: wjGrid.FlexGrid; data: any; rowCountText: string; // DataSvc will be passed by derived classes constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(); } // ngAfterViewInit() { this.theGrid.collapseGroupsToLevel(0); this._updateRowCount(this.theGrid); } // onGroupCollapsedChanged(s: wjGrid.FlexGrid, e: wjGrid.CellRangeEventArgs) { var row = s.rows[e.row], item = row.dataItem; // // did we just expand a node with a dummy child? if (!row.isCollapsed && item.children.length == 1 && item.children[0].name == '') { // // can't lazy load while updating rows if (s.rows.isUpdating) { row.isCollapsed = true; return; } // // replace the dummy child with actual nodes item.children.length = 0; var cnt = Math.round(Math.random() * 5) + 1; for (var i = 0; i < cnt; i++) { var node = this.dataService.createNode(); item.children.push(node); } // // refresh the view s.collectionView.refresh(); // // collapse the new item's child items for (var i = <number>row.index + 1; i < s.rows.length; i++) { var childRow = s.rows[i]; if (childRow.level <= row.level) { break; } childRow.isCollapsed = true; } // // update row count this._updateRowCount(s); } } private _updateRowCount(grid: wjGrid.FlexGrid) { this.rowCountText = wjCore.Globalize.format(grid.rows.length, 'n0'); } } //\\ @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 Lazy Loading Tree Grid</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"> <wj-flex-grid #theGrid [itemsSource]="data" [headersVisibility]="'Column'" [autoGenerateColumns]=false [childItemsPath]="'children'" (groupCollapsedChanged)="onGroupCollapsedChanged(theGrid,$event)"> <wj-flex-grid-column [binding]="'name'" [header]="'顧客名'" [width]="'*'"></wj-flex-grid-column> <wj-flex-grid-column [binding]="'id'" [header]="'ID'" [align]="'center'" [cssClass]="'id-column'"> </wj-flex-grid-column> </wj-flex-grid> <p> 現在の行数: <b id="rowCount" [innerHTML]="rowCountText"></b> </p> </div> import { Injectable } from '@angular/core'; let nodeId: number | undefined; function getOneOf(arr: string[]) { return arr[Math.floor(Math.random() * arr.length)]; } @Injectable() export class DataService { getData() { let tree = []; tree.push(this.createNode()); tree.push(this.createNode()); return tree; } createNode(dummy?: boolean): any { let first = '佐藤,鈴木,高橋,田中,伊藤,山本,渡辺,中村,小林,加藤,吉田,山田,佐々木,山口,松本,井上,木村,斎藤,林,清水,山崎,阿部,森,池田,橋本,山下,石川,中島,前田,藤田'.split(','), last = '浩,誠,健一,大輔,達也,翔太,浩一,哲也,剛,大介,健太,拓也,豊,修,和彦,学,直樹,健太郎,浩二,徹,隆,亮,翔,恵子,久美子,由美子,明美,直美,陽子,智子,絵美,恵,裕子,愛,真由美,由美,麻衣,美穂,愛美,彩'.split(','), name = dummy ? '' : getOneOf(first) + ' ' + getOneOf(last), children = []; if (!dummy) { children.push(this.createNode(true)); } if (nodeId == null) nodeId = 0; return { id: nodeId++, name: name, children: children }; } } .wj-flexgrid { max-height: 300px; margin: 12px; } .wj-flexgrid .wj-cell { padding: 8px; } .wj-flexgrid .wj-group:not(.wj-state-selected):not(.wj-state-multi-selected) { background-color: white; } .id-column { font-style: italic; } document { margin-bottom: 48pt; } 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 wjcCore from "@grapecity/wijmo"; import * as wjcGrid from "@grapecity/wijmo.react.grid"; import { createNode, getData } from "./data"; class App extends React.Component { constructor(props) { super(props); this.state = { data: getData(), rowCountText: '' }; } render() { return <div className="container-fluid"> <wjcGrid.FlexGrid itemsSource={this.state.data} headersVisibility="Column" groupCollapsedChanged={this.onGroupCollapsedChanged.bind(this)} initialized={this.initialGrid.bind(this)} childItemsPath="children"> <wjcGrid.FlexGridColumn binding="name" header="顧客名" with="*"></wjcGrid.FlexGridColumn> <wjcGrid.FlexGridColumn binding="id" header="ID" align="center" css-class="id-column"></wjcGrid.FlexGridColumn> </wjcGrid.FlexGrid> <p> 現在の行数: <b id="rowCount"></b> </p> </div>; } componentDidMount() { this.grid.collapseGroupsToLevel(0); this._updateRowCount(this.grid); } initialGrid(grid) { this.grid = grid; } onGroupCollapsedChanged(s, e) { let row = s.rows[e.row], item = row.dataItem; // did we just expand a node with a dummy child? if (!row.isCollapsed && item.children.length == 1 && item.children[0].name == '') { // can't lazy load while updating rows if (s.rows.isUpdating) { row.isCollapsed = true; return; } // replace the dummy child with actual nodes item.children.length = 0; let cnt = Math.round(Math.random() * 5) + 1; for (let i = 0; i < cnt; i++) { let node = createNode(); item.children.push(node); } // refresh the view s.collectionView.refresh(); // collapse the new item's child items for (let i = row.index + 1; i < s.rows.length; i++) { let childRow = s.rows[i]; if (childRow.level <= row.level) { break; } childRow.isCollapsed = true; } // update row count this._updateRowCount(s); } } _updateRowCount(grid) { this.setState({ rowCountText: wjcCore.Globalize.format(grid.rows.length, 'n0') }, () => { const node = document.querySelectorAll('#rowCount')[0]; node.innerHTML = this.state.rowCountText; }); } } 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> .wj-flexgrid { max-height: 300px; margin: 12px; } .wj-flexgrid .wj-cell { padding: 8px; } .wj-flexgrid .wj-group:not(.wj-state-selected):not(.wj-state-multi-selected) { background-color: white; } .id-column { font-style: italic; } document { margin-bottom: 48pt; } var nodeId; export function getData() { var tree = []; tree.push(createNode()); tree.push(createNode()); return tree; } export function createNode(dummy) { let first = '佐藤,鈴木,高橋,田中,伊藤,山本,渡辺,中村,小林,加藤,吉田,山田,佐々木,山口,松本,井上,木村,斎藤,林,清水,山崎,阿部,森,池田,橋本,山下,石川,中島,前田,藤田'.split(','), last = '浩,誠,健一,大輔,達也,翔太,浩一,哲也,剛,大介,健太,拓也,豊,修,和彦,学,直樹,健太郎,浩二,徹,隆,亮,翔,恵子,久美子,由美子,明美,直美,陽子,智子,絵美,恵,裕子,愛,真由美,由美,麻衣,美穂,愛美,彩'.split(','), name = dummy ? '' : getOneOf(first) + ' ' + getOneOf(last), children = []; if (!dummy) { children.push(createNode(true)); } if (nodeId == null) nodeId = 0; return { id: nodeId++, name: name, children: children }; } function getOneOf(arr) { return arr[Math.floor(Math.random() * arr.length)]; } <template> <div class="container-fluid"> <wj-flex-grid :items-source="data" headers-visibility="Column" child-items-path="children" :group-collapsed-changed="onGroupCollapsedChanged" :initialized="initialGrid"> <wj-flex-grid-column binding="name" header="顧客名" width="*"></wj-flex-grid-column> <wj-flex-grid-column binding="id" header="ID" align="center" css-class="id-column"></wj-flex-grid-column> </wj-flex-grid> <p> 現在の行数: <b id="rowCount" v-html="rowCountText"></b> </p> </div> </template> <script> import "@grapecity/wijmo.styles/wijmo.css"; import "bootstrap.css"; import Vue from "vue"; import * as wjcCore from '@grapecity/wijmo'; import "@grapecity/wijmo.vue2.grid"; import { getData, createNode } from "./data"; new Vue({ el: "#app", data: function() { return { data: getData(), rowCountText: '' } }, mounted: function() { this.grid.collapseGroupsToLevel(0); this._updateRowCount(this.grid); }, methods: { initialGrid(grid) { this.grid = grid; }, onGroupCollapsedChanged(s, e) { let row = s.rows[e.row], item = row.dataItem; // did we just expand a node with a dummy child? if (!row.isCollapsed && item.children.length == 1 && item.children[0].name == '') { // can't lazy load while updating rows if (s.rows.isUpdating) { row.isCollapsed = true; return; } // replace the dummy child with actual nodes item.children.length = 0; let cnt = Math.round(Math.random() * 5) + 1; for (let i = 0; i < cnt; i++) { let node = createNode(); item.children.push(node); } // refresh the view s.collectionView.refresh(); // collapse the new item's child items for (let i = row.index + 1; i < s.rows.length; i++) { let childRow = s.rows[i]; if (childRow.level <= row.level) { break; } childRow.isCollapsed = true; } // update row count this._updateRowCount(s); } }, _updateRowCount(grid) { this.rowCountText = wjcCore.Globalize.format(grid.rows.length, 'n0'); } } }); </script> <style> .wj-flexgrid { max-height: 300px; margin: 12px; } .wj-flexgrid .wj-cell { padding: 8px; } .wj-flexgrid .wj-group:not(.wj-state-selected):not(.wj-state-multi-selected) { background-color: white; } .id-column { font-style: italic; } document { margin-bottom: 48pt; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid Lazy Loading Tree Grid</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.vue'); </script> </head> <body> <div id="app"> </div> </body> </html> var nodeId; export function getData() { var tree = []; tree.push(createNode()); tree.push(createNode()); return tree; } export function createNode(dummy) { let first = '佐藤,鈴木,高橋,田中,伊藤,山本,渡辺,中村,小林,加藤,吉田,山田,佐々木,山口,松本,井上,木村,斎藤,林,清水,山崎,阿部,森,池田,橋本,山下,石川,中島,前田,藤田'.split(','), last = '浩,誠,健一,大輔,達也,翔太,浩一,哲也,剛,大介,健太,拓也,豊,修,和彦,学,直樹,健太郎,浩二,徹,隆,亮,翔,恵子,久美子,由美子,明美,直美,陽子,智子,絵美,恵,裕子,愛,真由美,由美,麻衣,美穂,愛美,彩'.split(','), name = dummy ? '' : getOneOf(first) + ' ' + getOneOf(last), children = []; if (!dummy) { children.push(createNode(true)); } if (nodeId == null) nodeId = 0; return { id: nodeId++, name: name, children: children }; } function getOneOf(arr) { return arr[Math.floor(Math.random() * arr.length)]; }