グリッド:マウスイベント

マウスイベントを処理するには、グリッドのhostElementにリスナーを追加し、hitTestメソッドを使用して、クリックされたグリッドパネルとセルを特定します。以下のグリッドは「mousemove」イベントにアタッチされたハンドラを持ち、マウスの下の要素に関する情報を表示します。

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 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 }), formatItem: function (s, e) { if (e.panel == s.cells) { if (s.columns[e.col].binding == 'country' && !(s.rows[e.row] instanceof wjGrid.GroupRow)) { if (s.editRange != null && s.editRange.contains(e.row, e.col)) { let spanEle = document.createElement('span'); spanEle.className = 'my-button'; spanEle.innerHTML = '&#x2b24;'; s.activeEditor.style.width = '75%'; e.cell.insertBefore(spanEle, s.activeEditor); } else { let html = '<span class="my-button">&#x2b24;</span>' + e.cell.innerHTML; e.cell.innerHTML = html; } } } } }); // // monitor and log mouse moves var logEl = document.getElementById('log'); theGrid.addEventListener(theGrid.hostElement, 'mousemove', function (e) { var ht = theGrid.hitTest(e); var logText = wjCore.format('パネル: <b>{cellType}</b>、行: <b>{row}</b>、列: <b>{col}</b>', { cellType: wjGrid.CellType[ht.cellType], row: ht.row, col: ht.col }); if (e.target.classList.contains('my-button')) { logText += ' (ボタン)'; } else if (e.target.tagName == 'INPUT' && e.target.type == 'checkbox') { logText += ' (チェックボックス)'; } else if (ht.panel == theGrid.cells) { if (theGrid.rows[ht.row] instanceof wjGrid.GroupRow) { logText += ' (グループ行)'; } else { logText += ' (値: <b>' + theGrid.cells.getCellData(ht.row, ht.col, true) + '</b>)'; } } logEl.innerHTML = logText; }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid Mouse 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; } #log { font-style: italic; text-align: center; margin-bottom: 10px; opacity: .5; } .my-button { opacity: .5; margin: 0 6px; } .my-button:hover { color: orange; } 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; // DataSvc will be passed by derived classes constructor() { this.data = new wjcCore.CollectionView(this._getData(), { groupDescriptions: [ 'country' ] // group data by country }); } formatFlexGrid(s: wjcGrid.FlexGrid, e: wjcGrid.FormatItemEventArgs) { // add 'button' to country cells if (e.panel == s.cells) { if (s.columns[e.col].binding == 'country' && !(s.rows[e.row] instanceof wjcGrid.GroupRow)) { if (s.editRange != null && s.editRange.contains(e.row, e.col)) { let spanEle = document.createElement('span'); spanEle.className = 'my-button'; spanEle.innerHTML = '&#x2b24;'; s.activeEditor.style.width = '75%'; e.cell.insertBefore(spanEle, s.activeEditor); } else { let html = '<span class="my-button">&#x2b24;</span>' + e.cell.innerHTML; e.cell.innerHTML = html; } } } } flexInitialized(flexgrid: wjcGrid.FlexGrid) { flexgrid.addEventListener(flexgrid.hostElement, 'mousemove', (e: MouseEvent) => { let ht = flexgrid.hitTest(e); let logText = wjcCore.format('パネル: <b>{cellType}</b>、行: <b>{row}</b>、列: <b>{col}</b>', { cellType: wjcGrid.CellType[ht.cellType], row: ht.row, col: ht.col }); if ((<HTMLElement>e.target).classList.contains('my-button')) { logText += ' (ボタン)'; } else if ((<HTMLElement>e.target).tagName == 'INPUT' && (<HTMLInputElement>e.target).type == 'checkbox') { logText += ' (チェックボックス)'; } else if (ht.panel == flexgrid.cells) { if (flexgrid.rows[ht.row] instanceof wjcGrid.GroupRow) { logText += ' (グループ行)'; } else { logText += ' (値: <b>' + flexgrid.cells.getCellData(ht.row, ht.col, true) + '</b>)'; } } document.querySelector('#log').innerHTML = logText }); } 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 Mouse 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 id="log"> グリッドの上にマウスを移動してください </div> <!-- the grid --> <wj-flex-grid #flex [(itemsSource)]="data" (formatItem)="formatFlexGrid(flex, $event)" (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; } #log { font-style: italic; text-align: center; margin-bottom: 10px; opacity: .5; } .my-button { opacity: .5; margin: 0 6px; } .my-button:hover { color: orange; } 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 wijmo from '@grapecity/wijmo'; import * as grid from '@grapecity/wijmo.grid'; import * as wjGrid from '@grapecity/wijmo.react.grid'; import { getData } from "./data"; class App extends React.Component { constructor(props) { super(props); this._data = new wijmo.CollectionView(getData(), { groupDescriptions: ['country'] // group data by country }); } render() { return <div className="container-fluid"> <div id="log">グリッドの上にマウスを移動してください</div> <wjGrid.FlexGrid initialized={this.flexInitialized.bind(this)} formatItem={this.formatFlexGrid.bind(this)} itemsSource={this._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(sender) { sender.addEventListener(sender.hostElement, 'mousemove', e => { let ht = sender.hitTest(e), logText = `パネル: <b>${grid.CellType[ht.cellType]}</b>、行: <b>${ht.row}</b>、列: <b>${ht.col}</b>`; if (e.target.classList.contains('my-button')) { logText += ' (ボタン)'; } else if (e.target.tagName == 'INPUT' && e.target.type == 'checkbox') { logText += ' (チェックボックス)'; } else if (ht.panel == sender.cells) { if (sender.rows[ht.row] instanceof grid.GroupRow) { logText += ' (グループ行)'; } else { logText += ' (値: <b>' + sender.cells.getCellData(ht.row, ht.col, true) + '</b>)'; } } document.querySelector('#log').innerHTML = logText; }); } formatFlexGrid(sender, e) { // add 'button' to country cells if (e.panel == sender.cells) { if (sender.columns[e.col].binding == 'country' && !(sender.rows[e.row] instanceof grid.GroupRow)) { if (sender.editRange != null && sender.editRange.contains(e.row, e.col)) { let spanEle = document.createElement('span'); spanEle.className = 'my-button'; spanEle.innerHTML = '&#x2b24;'; sender.activeEditor.style.width = '75%'; e.cell.insertBefore(spanEle, sender.activeEditor); } else { let html = '<span class="my-button">&#x2b24;</span>' + e.cell.innerHTML; e.cell.innerHTML = html; } } } } } 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; } #log { font-style: italic; text-align: center; margin-bottom: 10px; opacity: 0.5; } .my-button { opacity: 0.5; margin: 0 6px; } .my-button:hover { color: orange; } body { margin-bottom: 24px; } export function getData() { let countries = ['アメリカ', 'ドイツ', 'イギリス', '日本'], 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 id="log">グリッドの上にマウスを移動してください</div> <!-- the grid --> <wj-flex-grid :itemsSource="data" :formatItem="formatFlexGrid" :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 * as wjcGrid from '@grapecity/wijmo.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 }) }; }, methods: { formatFlexGrid: function(s, e) { // add 'button' to country cells if (e.panel == s.cells) { if (s.columns[e.col].binding == 'country' && !(s.rows[e.row] instanceof wjcGrid.GroupRow)) { if (s.editRange != null && s.editRange.contains(e.row, e.col)) { let spanEle = document.createElement('span'); spanEle.className = 'my-button'; spanEle.innerHTML = '&#x2b24;'; s.activeEditor.style.width = '75%'; e.cell.insertBefore(spanEle, s.activeEditor); } else { let html = '<span class="my-button">&#x2b24;</span>' + e.cell.innerHTML; e.cell.innerHTML = html; } } } }, flexInitialized: function(flexgrid) { flexgrid.addEventListener(flexgrid.hostElement, "mousemove", e => { let ht = flexgrid.hitTest(e); let logText = wijmo.format( "パネル: <b>{cellType}</b>、行: <b>{row}</b>、列: <b>{col}</b>", { cellType: wjcGrid.CellType[ht.cellType], row: ht.row, col: ht.col } ); if (e.target.classList.contains("my-button")) { logText += " (ボタン)"; } else if ( e.target.tagName == "INPUT" && e.target.type == "checkbox" ) { logText += " (チェックボックス)"; } else if (ht.panel == flexgrid.cells) { if (flexgrid.rows[ht.row] instanceof wjcGrid.GroupRow) { logText += " (グループ行)"; } else { logText += " (値: <b>" + flexgrid.cells.getCellData(ht.row, ht.col, true) + "</b>)"; } } document.querySelector("#log").innerHTML = logText; }); } } }); new Vue({ render: h => h(App) }).$mount("#app"); </script> <style> .wj-flexgrid { max-height: 220px; } #log { font-style: italic; text-align: center; margin-bottom: 10px; opacity: 0.5; } .my-button { opacity: 0.5; margin: 0 6px; } .my-button:hover { color: orange; } 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>GrapeCity Wijmo FlexGrid Mouse Events</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; }