Multi-Range Selection

The FlexGrid has a ListBox selection mode that allows users to select non-contiguous ranges of rows. But it does not have a built-in selection mode that allows user to select multiple, non-contiguous ranges of cells.

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'; // // ** ExtendedSelectionManager (transpiled from TypeScript) // var ExtendedSelectionManager = (function () { function ExtendedSelectionManager(flex) { var _this = this; this._selRanges = new wjCore.ObservableArray(); this._extending = false; // start/end extended selection mode flex.addEventListener(flex.hostElement, 'mousedown', function (e) { _this._extending = e.button == 0 && (e.ctrlKey || e.metaKey); if (!_this._extending && _this._selRanges.length) { _this._selRanges.clear(); } }, true); // end extended selection mode flex.addEventListener(flex.hostElement, 'mouseup', function (e) { _this._extending = false; }); // extend selection when selection is changing flex.selectionChanging.addHandler(function (s, e) { if (_this._extending) { var xrng = _this._selRanges, curr = e.range, last = xrng.length ? xrng[xrng.length - 1] : null; if (last && curr.intersects(last)) { xrng.setAt(xrng.length - 1, curr); } else { _this._selRanges.push(curr); } } }); // clear extended selection after a selection change flex.selectionChanged.addHandler(function (s, e) { var xrng = _this._selRanges; if (!_this._extending && xrng.length) { xrng.clear(); } }); // apply selected style to a cell flex.formatItem.addHandler(function (s, e) { if (e.panel == flex.cells) { _this._updateCellStyle(e.row, e.col, e.cell); } }); // update selection styles when extended selections change this._selRanges.collectionChanged.addHandler(function () { setTimeout(function () { var rng = flex.viewRange; for (var r = rng.row; r <= rng.row2; r++) { for (var c = rng.col; c <= rng.col2; c++) { var cell = flex.cells.getCellElement(r, c); _this._updateCellStyle(r, c, cell); } } }); }); } Object.defineProperty(ExtendedSelectionManager.prototype, "selectedRanges", { // gets the array of selected ranges get: function () { return this._selRanges; }, enumerable: true, configurable: true }); // update the selected style for a given cell ExtendedSelectionManager.prototype._updateCellStyle = function (r, c, cell) { var selected = false; for (var i = 0; i < this._selRanges.length && !selected; i++) { selected = this._selRanges[i].contains(r, c); } wjCore.toggleClass(cell, 'extended-selection', selected); }; return ExtendedSelectionManager; }()); // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // // create some random data var data = []; var countries = 'Austria,Belgium,Chile,Denmark,Finland,Japan,UK'.split(','); for (var i = 0; i < 300; i++) { data.push({ id: i, from: countries[i % countries.length], to: countries[(i + 1) % countries.length], sales: Math.random() * 10000, expenses: Math.random() * 5000, amount: Math.random() * 10000, extra: Math.random() * 10000, }); } // // show the data in a grid var theGrid = new wjGrid.FlexGrid('#theGrid', { alternatingRowStep: 0, itemsSource: data }); // // add an extended selection manager to the grid var xsm = new ExtendedSelectionManager(theGrid); xsm.selectedRanges.collectionChanged.addHandler(function (s, e) { var arr = xsm.selectedRanges; console.log('selectedRanges: ' + arr.length); for (var i = 0; i < arr.length; i++) { console.log(' ' + i + wjCore.format(': ({row},{col})-({row2}-{col2})', arr[i])); } }); // // show selected ranges document.getElementById('theBtn').addEventListener('click', function () { // // get selected ranges or regular selection var ranges = xsm.selectedRanges.length ? xsm.selectedRanges : [theGrid.selection]; // // build message var msg = ranges.length > 1 ? ranges.length + ' ranges selected:' : 'Single range selected:'; ranges.forEach(function (rng, index) { msg += '\n' + index + wjCore.format(': ({row},{col})-({row2}-{col2})', rng); }); // // show the message alert(msg); }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid Multiple Selection</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> <button id="theBtn" class="btn btn-primary"> List Selected Cells </button> </div> </body> </html> .wj-flexgrid { max-height: 300px; } .extended-selection.wj-cell:not(.wj-header):not(.wj-group):not(.wj-state-selected):not(.wj-state-multi-selected) { color: white; background: #a47aff; } body { margin-bottom: 20px; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import { Component, 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 * as wjcCore from '@grapecity/wijmo'; import * as wjcGrid from '@grapecity/wijmo.grid'; import '@grapecity/wijmo.input'; import { ExtendedSelectionManager } from './app.extendedSelectionManager'; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any[]; private _xsm: ExtendedSelectionManager; // DataSvc will be passed by derived classes constructor() { this.data = this._getData(); } @ViewChild('flex') flex: wjcGrid.FlexGrid; initializeGrid(flex: wjcGrid.FlexGrid) { // add an extended selection manager to the grid this._xsm = new ExtendedSelectionManager(flex); this._xsm.selectedRanges.collectionChanged.addHandler((s: any, e: wjcCore.NotifyCollectionChangedEventArgs) => { let arr = this._xsm.selectedRanges; console.log('selectedRanges: ' + arr.length); for (let i = 0; i < arr.length; i++) { console.log(' ' + i + wjcCore.format(': ({row},{col})-({row2}-{col2})', arr[i])); } }); } listSelectedCells() { // get selected ranges or regular selection let ranges = this._xsm.selectedRanges.length ? this._xsm.selectedRanges : [ this.flex.selection ]; // build message let msg = ranges.length > 1 ? ranges.length + ' ranges selected:' : 'Single range selected:'; ranges.forEach((rng: wjcGrid.CellRange, index: number) => { msg += '\n' + index + wjcCore.format(': ({row},{col})-({row2}-{col2})', rng); }); // show the message alert(msg); } private _getData() { // create some random data let data = []; let countries = 'Austria,Belgium,Chile,Denmark,Finland,Japan,UK'.split(','); for (let i = 0; i < 300; i++) { data.push({ id: i, from: countries[i % countries.length], to: countries[(i+1) % countries.length], sales: Math.random() * 10000, expenses: Math.random() * 5000, amount: Math.random() * 10000, extra: Math.random() * 10000, }); } 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 Multiple Selection</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"> <!-- the grid --> <wj-flex-grid #flex [alternatingRowStep]="0" [(itemsSource)]="data" (initialized)="initializeGrid(flex)"> </wj-flex-grid> <button (click)="listSelectedCells()" class="btn btn-primary"> List Selected Cells </button> </div> .wj-flexgrid { max-height: 300px; } .extended-selection.wj-cell:not(.wj-header):not(.wj-group):not(.wj-state-selected):not(.wj-state-multi-selected) { color: white; background: #a47aff; } body { margin-bottom: 20px; } <template> <div class="container-fluid"> <!-- the grid --> <wj-flex-grid :alternatingRowStep="0" :itemsSource="grid.data" :initialized="initializeGrid"> </wj-flex-grid> <button @click="listSelectedCells" class="btn btn-primary"> List Selected Cells </button> </div> </template> <script> import "@grapecity/wijmo.styles/wijmo.css"; import 'bootstrap.css'; import Vue from 'vue'; import * as wjGrid from '@grapecity/wijmo.grid'; import * as wjCore from '@grapecity/wijmo'; import '@grapecity/wijmo.vue2.core'; import '@grapecity/wijmo.vue2.grid'; import {ExtendedSelectionManager} from './extendedSelectionManager'; let App = Vue.extend({ name: 'app', data: function(){ return { grid: { data: this.getData(), _xsm: null, flex: null } } }, methods: { getData: function(s, e){ // create some random data let data = []; let countries = 'Austria,Belgium,Chile,Denmark,Finland,Japan,UK'.split(','); for (let i = 0; i < 300; i++) { data.push({ id: i, from: countries[i % countries.length], to: countries[(i+1) % countries.length], sales: Math.random() * 10000, expenses: Math.random() * 5000, amount: Math.random() * 10000, extra: Math.random() * 10000, }); } return data; }, initializeGrid: function(flex){ this.flex=flex; // add an extended selection manager to the grid this._xsm = new ExtendedSelectionManager(flex); this._xsm.selectedRanges.collectionChanged.addHandler((s, e) => { let arr = this._xsm.selectedRanges; console.log('selectedRanges: ' + arr.length); for (let i = 0; i < arr.length; i++) { console.log(' ' + i + wjCore.format(': ({row},{col})-({row2}-{col2})', arr[i])); } }); }, listSelectedCells: function() { // get selected ranges or regular selection let ranges = this._xsm.selectedRanges.length ? this._xsm.selectedRanges : [ this.flex.selection ]; // build message let msg = ranges.length > 1 ? ranges.length + ' ranges selected:' : 'Single range selected:'; ranges.forEach((rng, index) => { msg += '\n' + index + wjCore.format(': ({row},{col})-({row2}-{col2})', rng); }); // show the message alert(msg); } } }) new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> .wj-flexgrid { max-height: 300px; } .extended-selection.wj-cell:not(.wj-header):not(.wj-group):not(.wj-state-selected):not(.wj-state-multi-selected) { color: white; background: #a47aff; } body { margin-bottom: 20px; } </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/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> 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 { ExtendedSelectionManager } from "./extendedSelectionManager"; class App extends React.Component { constructor(props) { super(props); this.state = { data: this.getData(), _xsm: null, flex: null }; } render() { return <div className="container-fluid"> <wjcGrid.FlexGrid showAlternatingRows={false} itemsSource={this.state.data} initialized={this.initializeGrid.bind(this)}> </wjcGrid.FlexGrid> <button onClick={this.listSelectedCells.bind(this)} className="btn btn-primary"> List Selected Cells </button> </div>; } getData() { // create some random data let data = []; let countries = 'Austria,Belgium,Chile,Denmark,Finland,Japan,UK'.split(','); for (let i = 0; i < 300; i++) { data.push({ id: i, from: countries[i % countries.length], to: countries[(i + 1) % countries.length], sales: Math.random() * 10000, expenses: Math.random() * 5000, amount: Math.random() * 10000, extra: Math.random() * 10000, }); } return data; } initializeGrid(flex) { this.setState({ flex: flex, _xsm: new ExtendedSelectionManager(flex) }, () => { // add an extended selection manager to the grid this.state._xsm.selectedRanges.collectionChanged.addHandler((s, e) => { let arr = this.state._xsm.selectedRanges; console.log('selectedRanges: ' + arr.length); for (let i = 0; i < arr.length; i++) { console.log(' ' + i + wjcCore.format(': ({row},{col})-({row2}-{col2})', arr[i])); } }); }); } listSelectedCells() { // get selected ranges or regular selection let ranges = this.state._xsm.selectedRanges.length ? this.state._xsm.selectedRanges : [this.state.flex.selection]; // build message let msg = ranges.length > 1 ? ranges.length + ' ranges selected:' : 'Single range selected:'; ranges.forEach((rng, index) => { msg += '\n' + index + wjcCore.format(': ({row},{col})-({row2}-{col2})', rng); }); // show the message alert(msg); } } 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; } .extended-selection.wj-cell:not(.wj-header):not(.wj-group):not(.wj-state-selected):not(.wj-state-multi-selected) { color: white; background: #a47aff; } body { margin-bottom: 20px; }