グリッド:選択イベント

FlexGridのselectionプロパティは、選択したセルの範囲を含むCellRangeオブジェクトを返します。選択が変更されると、グリッドはselectionChangingselectionChangedイベントを発生させます。下のグリッドはselectionChangedイベントを使用して、選択したデータの概要を表示します。

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 some random data var countries = 'アメリカ,ドイツ,イギリス,日本'.split(','); var data = []; for (var i = 0; i < 20; i++) { data.push({ id: i, country: countries[i % countries.length], sales: Math.random() * 10000, expenses: Math.random() * 5000, overdue: i % 4 == 0 }); } // // bind a grid to the data var elLog = document.getElementById('log'); var placeholder = elLog.innerHTML; var theGrid = new wjGrid.FlexGrid('#theGrid', { autoGenerateColumns: false, columns: [ { binding: 'id', header: 'ID', width: 80 }, { binding: 'country', header: '国' }, { binding: 'sales', header: '売上', width: 80 }, { binding: 'expenses', header: '費用', width: 80 }, { binding: 'overdue', header: '期限切れ' }, ], itemsSource: new wjCore.CollectionView(data, { groupDescriptions: ['country'] // group data by country }), selectionChanged: function (s, e) { var html = placeholder; if (!theGrid.selection.isSingleCell) { var stats = getSelectionStats(theGrid); var fmt = stats.sum != null ? '平均: <b>{avg:n2}</b>、個数: <b>{cnt:n0}</b>、合計: <b>{sum:n2}</b>' : '個数: {cnt:n0}'; html = wjCore.format(fmt, stats); } elLog.innerHTML = html; } }); // // get statistics for the current selection function getSelectionStats(grid) { var sel = grid.selection, cnt = 0, ncnt = 0, sum = 0; for (var r = sel.topRow; r <= sel.bottomRow; r++) { for (var c = sel.leftCol; c <= sel.rightCol; c++) { var val = grid.cells.getCellData(r, c, false); if (val != null) { cnt++; if (wjCore.isNumber(val)) { ncnt++; sum += val; } } } } return { cnt: cnt, sum: ncnt > 0 ? sum : null, avg: ncnt > 0 ? sum / ncnt : null }; } // } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid Selection Events</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="log"> グリッド上の範囲を選択してください </div> <div id="theGrid"> </div> </div> </body> </html> .wj-flexgrid { max-height: 220px; margin:10px; } #log { font-style: italic; text-align: center; margin-bottom: 10px; opacity: .5; } body { margin-bottom: 24px; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import { Component, enableProdMode, NgModule } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import * as wjcCore from '@grapecity/wijmo'; import * as wjcGrid from '@grapecity/wijmo.grid'; import { WjGridModule } from '@grapecity/wijmo.angular2.grid'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: wjcCore.CollectionView; logText: string = 'グリッドでセル範囲を選択してください'; // DataSvc will be passed by derived classes constructor() { this.data = new wjcCore.CollectionView(this._getData(), { groupDescriptions: [ 'country' ] // group data by country }); } flexInitialized(flexgrid: wjcGrid.FlexGrid) { flexgrid.selectionChanged.addHandler(() => { if (!flexgrid.selection.isSingleCell) { let stats = this._getSelectionStats(flexgrid); let fmt = stats.sum != null ? '平均: <b>{avg:n2}</b>、個数: <b>{cnt:n0}</b>、合計: <b>{sum:n2}</b>' : '個数: {cnt:n0}'; this.logText = wjcCore.format(fmt, stats); } else { this.logText = 'グリッドでセル範囲を選択してください'; } }); } // get statistics for the current selection private _getSelectionStats(grid: wjcGrid.FlexGrid) { let sel = grid.selection, cnt = 0, ncnt = 0, sum = 0; for (let r = sel.topRow; r <= sel.bottomRow; r++) { for (let c = sel.leftCol; c <= sel.rightCol; c++) { let val = grid.cells.getCellData(r, c, false); if (val != null) { cnt++; if (wjcCore.isNumber(val)) { ncnt++; sum += val; } } } } return { cnt: cnt, sum: ncnt > 0 ? sum: null, avg: ncnt > 0 ? sum/ncnt: null } } private _getData() { // create some random data let countries = 'アメリカ,ドイツ,イギリス,日本'.split(','); let data = []; for (let i = 0; i < 20; i++) { data.push({ id: i, country: countries[i % countries.length], sales: Math.random() * 10000, expenses: Math.random() * 5000, overdue: i % 4 == 0 }); } return data; } } // @NgModule({ imports: [WjGridModule, BrowserModule], declarations: [AppComponent], 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 Selection Events</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"> <div [innerHTML]="logText" class="log"> </div> <!-- the grid --> <wj-flex-grid #flex [(itemsSource)]="data" (initialized)="flexInitialized(flex)"> <wj-flex-grid-column binding="id" header="ID" [width]="80"></wj-flex-grid-column> <wj-flex-grid-column binding="country" header="国"></wj-flex-grid-column> <wj-flex-grid-column binding="sales" header="売上" [width]="80"></wj-flex-grid-column> <wj-flex-grid-column binding="expenses" header="費用" [width]="80"></wj-flex-grid-column> <wj-flex-grid-column binding="overdue" header="期限切れ"></wj-flex-grid-column> </wj-flex-grid> </div> .wj-flexgrid { max-height: 220px; margin:10px; } .log { font-style: italic; text-align: center; margin-bottom: 10px; opacity: .5; } body { margin-bottom: 24px; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './app.css'; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; import * as wjGrid from '@grapecity/wijmo.react.grid'; import * as wjcCore from '@grapecity/wijmo'; import { getData } from "./data"; class App extends React.Component { constructor(props) { super(props); this.state = { data: new wjcCore.CollectionView(getData(), { groupDescriptions: ["country"] // group data by country }), logText: 'グリッドでセル範囲を選択してください' }; } render() { return <div className="container-fluid"> <div dangerouslySetInnerHTML={{ __html: this.state.logText }} className="log"> </div> <wjGrid.FlexGrid initialized={this.flexInitialized.bind(this)} itemsSource={this.state.data}> <wjGrid.FlexGridColumn binding="id" header="ID" width={80}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="country" header="国"></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="sales" header="売上" width={80}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="expenses" header="費用" width={80}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="overdue" header="期限切れ"></wjGrid.FlexGridColumn> </wjGrid.FlexGrid> </div>; } flexInitialized(flexgrid) { flexgrid.selectionChanged.addHandler(() => { if (!flexgrid.selection.isSingleCell) { let stats = this.getSelectionStats(flexgrid); let fmt = stats.sum != null ? "平均: <b>{avg:n2}</b>、個数: <b>{cnt:n0}</b>、合計: <b>{sum:n2}</b>" : "個数: {cnt:n0}"; this.setState({ logText: wjcCore.format(fmt, stats) }); } else { this.setState({ logText: "グリッドでセル範囲を選択してください" }); } }); } getSelectionStats(grid) { let sel = grid.selection, cnt = 0, ncnt = 0, sum = 0; for (let r = sel.topRow; r <= sel.bottomRow; r++) { for (let c = sel.leftCol; c <= sel.rightCol; c++) { let val = grid.cells.getCellData(r, c, false); if (val != null) { cnt++; if (wjcCore.isNumber(val)) { ncnt++; sum += val; } } } } return { cnt: cnt, sum: ncnt > 0 ? sum : null, avg: ncnt > 0 ? sum / ncnt : null }; } } 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 FlexGrid Keyboard Events</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: 220px; margin: 10px; } .container-fluid .log { font-style: italic; text-align: center; margin-bottom: 10px; opacity: 0.5; } body { margin-bottom: 24px; } export function getData() { // create some random data let countries = 'アメリカ,ドイツ,イギリス,日本'.split(','); let data = []; for (let i = 0; i < 20; i++) { data.push({ id: i, country: countries[i % countries.length], sales: Math.random() * 10000, expenses: Math.random() * 5000, overdue: i % 4 == 0 }); } return data; } <template> <div class="container-fluid"> <div v-html="logText" class="log"></div> <!-- the grid --> <wj-flex-grid :itemsSource="data" :initialized="flexInitialized"> <wj-flex-grid-column binding="id" header="ID" :width=80></wj-flex-grid-column> <wj-flex-grid-column binding="country" header="国"></wj-flex-grid-column> <wj-flex-grid-column binding="sales" header="売上" :width=80></wj-flex-grid-column> <wj-flex-grid-column binding="expenses" header="費用" :width=80></wj-flex-grid-column> <wj-flex-grid-column binding="overdue" header="期限切れ"></wj-flex-grid-column> </wj-flex-grid> </div> </template> <script> import "@grapecity/wijmo.styles/wijmo.css"; import 'bootstrap.css'; import Vue from 'vue'; import '@grapecity/wijmo.vue2.core'; import '@grapecity/wijmo.vue2.grid'; import { getData } from "./data"; let App = Vue.extend({ name: "app", data: function() { return { data: new wijmo.CollectionView(getData(), { groupDescriptions: ["country"] // group data by country }), logText: 'グリッドでセル範囲を選択してください' }; }, methods: { flexInitialized: function(flexgrid) { flexgrid.selectionChanged.addHandler(() => { if (!flexgrid.selection.isSingleCell) { let stats = this._getSelectionStats(flexgrid); let fmt = stats.sum != null ? "平均: <b>{avg:n2}</b>、個数: <b>{cnt:n0}</b>、合計: <b>{sum:n2}</b>" : "個数: {cnt:n0}"; this.logText = wijmo.format(fmt, stats); } else { this.logText = "グリッドでセル範囲を選択してください"; } }); }, _getSelectionStats: function(grid) { let sel = grid.selection, cnt = 0, ncnt = 0, sum = 0; for (let r = sel.topRow; r <= sel.bottomRow; r++) { for (let c = sel.leftCol; c <= sel.rightCol; c++) { let val = grid.cells.getCellData(r, c, false); if (val != null) { cnt++; if (wijmo.isNumber(val)) { ncnt++; sum += val; } } } } return { cnt: cnt, sum: ncnt > 0 ? sum : null, avg: ncnt > 0 ? sum / ncnt : null }; } } }); new Vue({ render: h => h(App) }).$mount("#app"); </script> <style> .wj-flexgrid { max-height: 220px; margin: 10px; } .container-fluid .log { font-style: italic; text-align: center; margin-bottom: 10px; opacity: 0.5; } body { margin-bottom: 24px; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>AutoComplete</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() { // create some random data let countries = 'アメリカ,ドイツ,イギリス,日本'.split(','); let data = []; for (let i = 0; i < 20; i++) { data.push({ id: i, country: countries[i % countries.length], sales: Math.random() * 10000, expenses: Math.random() * 5000, overdue: i % 4 == 0 }); } return data; }