Checkbox-based Selection

This grid implements a custom checkbox-based scheme for row selection. Click the checkbox in the top-left cell to select or deselect all rows.

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'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // var theGrid = new wjGrid.FlexGrid('#theGrid', { deferResizing: true, alternatingRowStep: 0, headersVisibility: 'Column', selectionMode: 'Cell', itemsSource: getData(100), // // create select all button on the column header formatItem: function (s, e) { if (e.panel == s.columnHeaders) { if (s.columns[e.col].binding == 'sel') { e.cell.innerHTML = '<input class="select-all" tabindex="-1" type="checkbox">'; updateSelectAllBox(s); } } else if (e.panel == s.cells) { wjCore.setAttribute(e.cell.parentElement, 'aria-selected', s.rows[e.row].dataItem.sel); } } }); // // initialize column widths theGrid.autoSizeColumns(null, null, null, 18); // // select/de-select all items when user clicks the check box on the header theGrid.hostElement.addEventListener('click', function (e) { if (wjCore.hasClass(e.target, 'select-all') && e.target instanceof HTMLInputElement) { theGrid.deferUpdate(function () { theGrid.collectionView.items.forEach(function (item) { item.sel = e.target.checked; }); }); } }); // // update the select all checkbox state function updateSelectAllBox(grid) { var cb = grid.hostElement.querySelector('.select-all'); if (cb instanceof HTMLInputElement) { var items = grid.collectionView.items, last = null, indeterminate = false; for (var i = 0; i < items.length; i++) { if (last != null && items[i].sel != last) { indeterminate = true; break; } last = items[i].sel; } cb.checked = last; if (indeterminate) { cb.indeterminate = true; } } } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid Checkbox 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> </div> </body> </html> import { DateTime } from '@grapecity/wijmo'; // some random data export function getData(cnt) { var data = [], today = new Date(); for (var i = 0; i < cnt; i++) { data.push({ sel: false, title: getOneOf(['Lunch Tomorrow', 'Meeting on Friday', 'Quarterly Review', 'Evaluation']), from: getOneOf(['joe@joe.com', 'mark@smith.com', 'todd@schick.com', 'sun@wong.com']), priority: getOneOf([1, 2, 3]), immediate: Math.random() > .75, date: DateTime.addDays(today, i) }); } return data; } // function getOneOf(arr) { return arr[Math.floor(Math.random() * arr.length)]; } /* fixed grid height */ .wj-flexgrid { height: 350px; } /* some extra cell padding */ .wj-flexgrid .wj-cell { padding: 12px; } /* replace selection highlight with a dotted border */ .wj-flexgrid .wj-cells .wj-cell.wj-state-selected { background: inherit; color: inherit; } .wj-flexgrid.wj-state-focused .wj-cells .wj-cell.wj-state-selected { border: 2px solid #7099de; padding: 10px; } /* selected rows */ .wj-flexgrid:not(.wj-state-focused) .wj-cells .wj-row[aria-selected=true] .wj-cell, .wj-flexgrid .wj-cells .wj-row[aria-selected=true] .wj-cell:not(.wj-state-selected) { background: rgba(0, 0, 200, .5); color: white; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import { Component, enableProdMode, NgModule, Inject } from '@angular/core'; import { FormsModule } from '@angular/forms'; 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 { DataService } from './app.data'; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any[]; quickEdit: boolean = true; // DataSvc will be passed by derived classes constructor(@Inject(DataService) dataSvc: DataService) { this.data = dataSvc.getData(100); } initializeGrid(flex: wjcGrid.FlexGrid) { flex.formatItem.addHandler((s: wjcGrid.FlexGrid, e: wjcGrid.FormatItemEventArgs) => { if (e.panel == s.columnHeaders) { if (s.columns[e.col].binding == 'sel') { e.cell.innerHTML = '<input class="select-all" tabindex="-1" type="checkbox">'; this._updateSelectAllBox(s); } } else if (e.panel == s.cells) { wjcCore.setAttribute( e.cell.parentElement, 'aria-selected', s.rows[e.row].dataItem.sel ); } }); // initialize column widths flex.autoSizeColumns(null, null, null, 18); // select/de-select all items when user clicks the check box on the header flex.hostElement.addEventListener('click', (e: MouseEvent) => { if (wjcCore.hasClass(<HTMLInputElement>e.target, 'select-all') && e.target instanceof HTMLInputElement) { flex.deferUpdate(() => { flex.collectionView.items.forEach((item: any) => { item.sel = (<HTMLInputElement>e.target).checked; }); }); } }); } // update the select all checkbox state private _updateSelectAllBox(grid: wjcGrid.FlexGrid) { let cb = grid.hostElement.querySelector('.select-all'); if (cb instanceof HTMLInputElement) { let items = grid.collectionView.items, last = null, indeterminate = false; for (let i = 0; i < items.length; i++) { if (last != null && items[i].sel != last) { indeterminate = true; break; } last = items[i].sel; } cb.checked = last; if (indeterminate) { cb.indeterminate = true; } } } } @NgModule({ imports: [WjGridModule, FormsModule, BrowserModule], providers: [DataService], 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 Checkbox 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 [deferResizing]="true" [alternatingRowStep]="0" [headersVisibility]="'Column'" [selectionMode]="'Cell'" [(itemsSource)]="data" (initialized)="initializeGrid(flex)"> </wj-flex-grid> </div> import { Injectable } from '@angular/core'; import * as wjcCore from '@grapecity/wijmo'; @Injectable() export class DataService { getData(cnt: number): any[] { let data = [], today = new Date(); for (let i = 0; i < cnt; i++) { data.push({ sel: false, title: this.getOneOf(['Lunch Tomorrow', 'Meeting on Friday', 'Quarterly Review', 'Evaluation']), from: this.getOneOf(['joe@joe.com', 'mark@smith.com', 'todd@schick.com', 'sun@wong.com']), priority: this.getOneOf([1, 2, 3]), immediate: Math.random() > .75, date: wjcCore.DateTime.addDays(today, i) }); } return data; } getOneOf(arr: any[]): string { return arr[Math.floor(Math.random() * arr.length)]; } } /* fixed grid height */ .wj-flexgrid { height: 350px; } /* some extra cell padding */ .wj-flexgrid .wj-cell { padding: 12px; } /* replace selection highlight with a dotted border */ .wj-flexgrid .wj-cells .wj-cell.wj-state-selected { background: inherit; color: inherit; } .wj-flexgrid.wj-state-focused .wj-cells .wj-cell.wj-state-selected { border: 2px solid #7099de; padding: 10px; } /* selected rows */ .wj-flexgrid:not(.wj-state-focused) .wj-cells .wj-row[aria-selected=true] .wj-cell, .wj-flexgrid .wj-cells .wj-row[aria-selected=true] .wj-cell:not(.wj-state-selected) { background: rgba(0, 0, 200, .5); color: white; } <template> <div class="container-fluid"> <!-- the grid --> <wj-flex-grid :deferResizing="true" :alternatingRowStep="0" :headersVisibility="'Column'" :selectionMode="'Cell'" :itemsSource="data" :initialized="initializeGrid"> </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 wjCore from '@grapecity/wijmo'; import { getData } from './data'; let App = Vue.extend({ name: 'app', data: function(){ return { data: getData(100) } }, methods: { initializeGrid: function(flex){ flex.formatItem.addHandler((s, e) => { if (e.panel == s.columnHeaders) { if (s.columns[e.col].binding == 'sel') { e.cell.innerHTML = '<input class="select-all" tabindex="-1" type="checkbox">'; this._updateSelectAllBox(s); } } else if (e.panel == s.cells) { wjCore.setAttribute( e.cell.parentElement, 'aria-selected', s.rows[e.row].dataItem.sel ); } }); // initialize column widths flex.autoSizeColumns(null, null, null, 18); // select/de-select all items when user clicks the check box on the header flex.hostElement.addEventListener('click', (e) => { if (wjCore.hasClass(e.target, 'select-all') && e.target instanceof HTMLInputElement) { flex.deferUpdate(() => { flex.collectionView.items.forEach((item) => { item.sel = (e.target).checked; }); }); } }); }, _updateSelectAllBox: function(grid) { let cb = grid.hostElement.querySelector('.select-all'); if (cb instanceof HTMLInputElement) { let items = grid.collectionView.items, last = null, indeterminate = false; for (let i = 0; i < items.length; i++) { if (last != null && items[i].sel != last) { indeterminate = true; break; } last = items[i].sel; } cb.checked = last; if (indeterminate) { cb.indeterminate = true; } } } } }) new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> /* fixed grid height */ .wj-flexgrid { height: 350px; } /* some extra cell padding */ .wj-flexgrid .wj-cell { padding: 12px; } /* replace selection highlight with a dotted border */ .wj-flexgrid .wj-cells .wj-cell.wj-state-selected { background: inherit; color: inherit; } .wj-flexgrid.wj-state-focused .wj-cells .wj-cell.wj-state-selected { border: 2px solid #7099de; padding: 10px; } /* selected rows */ .wj-flexgrid:not(.wj-state-focused) .wj-cells .wj-row[aria-selected=true] .wj-cell, .wj-flexgrid .wj-cells .wj-row[aria-selected=true] .wj-cell:not(.wj-state-selected) { background: rgba(0, 0, 200, .5); color: white; } </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 {DateTime} from '@grapecity/wijmo'; // some random data export function getData(cnt) { var data = [], today = new Date(); for (var i = 0; i < cnt; i++) { data.push({ sel: false, title: getOneOf(['Lunch Tomorrow', 'Meeting on Friday', 'Quarterly Review', 'Evaluation']), from: getOneOf(['joe@joe.com', 'mark@smith.com', 'todd@schick.com', 'sun@wong.com']), priority: getOneOf([1, 2, 3]), immediate: Math.random() > .75, date: DateTime.addDays(today, i) }); } return data; } // function getOneOf(arr) { return arr[Math.floor(Math.random() * arr.length)] } 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 { getData } from "./data"; class App extends React.Component { constructor(props) { super(props); this.state = { data: getData(100) }; } render() { return <div className="container-fluid"> <wjcGrid.FlexGrid deferResizing={true} alternatingRowStep={0} headersVisibility="Column" selectionMode="Cell" itemsSource={this.state.data} initialized={this.initializeGrid.bind(this)}> </wjcGrid.FlexGrid> </div>; } initializeGrid(flex) { flex.formatItem.addHandler((s, e) => { if (e.panel == s.columnHeaders) { if (s.columns[e.col].binding == 'sel') { e.cell.innerHTML = '<input class="select-all" tabindex="-1" type="checkbox">'; this._updateSelectAllBox(s); } } else if (e.panel == s.cells) { wjcCore.setAttribute(e.cell.parentElement, 'aria-selected', s.rows[e.row].dataItem.sel); } }); // initialize column widths flex.autoSizeColumns(null, null, null, 18); // select/de-select all items when user clicks the check box on the header flex.hostElement.addEventListener('click', (e) => { if (wjcCore.hasClass(e.target, 'select-all') && e.target instanceof HTMLInputElement) { flex.deferUpdate(() => { flex.collectionView.items.forEach((item) => { item.sel = (e.target).checked; }); }); } }); } _updateSelectAllBox(grid) { let cb = grid.hostElement.querySelector('.select-all'); if (cb instanceof HTMLInputElement) { let items = grid.collectionView.items, last = null, indeterminate = false; for (let i = 0; i < items.length; i++) { if (last != null && items[i].sel != last) { indeterminate = true; break; } last = items[i].sel; } cb.checked = last; if (indeterminate) { cb.indeterminate = true; } } } } 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> /* fixed grid height */ .wj-flexgrid { height: 350px; } /* some extra cell padding */ .wj-flexgrid .wj-cell { padding: 12px; } /* replace selection highlight with a dotted border */ .wj-flexgrid .wj-cells .wj-cell.wj-state-selected { background: inherit; color: inherit; } .wj-flexgrid.wj-state-focused .wj-cells .wj-cell.wj-state-selected { border: 2px solid #7099de; padding: 10px; } /* selected rows */ .wj-flexgrid:not(.wj-state-focused) .wj-cells .wj-row[aria-selected=true] .wj-cell, .wj-flexgrid .wj-cells .wj-row[aria-selected=true] .wj-cell:not(.wj-state-selected) { background: rgba(0, 0, 200, .5); color: white; } import { DateTime } from '@grapecity/wijmo'; // some random data export function getData(cnt) { var data = [], today = new Date(); for (var i = 0; i < cnt; i++) { data.push({ sel: false, title: getOneOf(['Lunch Tomorrow', 'Meeting on Friday', 'Quarterly Review', 'Evaluation']), from: getOneOf(['joe@joe.com', 'mark@smith.com', 'todd@schick.com', 'sun@wong.com']), priority: getOneOf([1, 2, 3]), immediate: Math.random() > .75, date: DateTime.addDays(today, i) }); } return data; } // function getOneOf(arr) { return arr[Math.floor(Math.random() * arr.length)]; }