CollectionView:グループ化

CollectionViewはフィールドに基づいて項目をグループ化できます。groupDescriptionsプロパティはグループ化された項目のリストを含みます。このサンプルは、groupDescriptionsプロパティに項目を追加または削除する方法を示し、FlexGridにグループ化された項目を表示します。

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wijmo from '@grapecity/wijmo'; import * as grid from '@grapecity/wijmo.grid'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // create a CollectionView let view = new wijmo.CollectionView(getData(), { currentItem: null, sortDescriptions: [new wijmo.SortDescription('sales', false)] }); // // show the data in a grid let flex = new grid.FlexGrid('#theGrid', { autoGenerateColumns: false, columns: [ { binding: 'id', header: 'ID', width: 80 }, { binding: 'country', header: '国' }, { binding: 'product', header: '商品' }, { binding: 'sales', header: '売上', width: 80 }, { binding: 'expenses', header: '費用', width: 80 }, ], itemsSource: view, alternatingRowStep: 0, headersVisibility: 'Column' }); // // change the grouping document.addEventListener('change', e => { // remove the old groups view.groupDescriptions.clear(); // // add the new groups let filter = e.target; if (filter.value) { filter.value.split(',').forEach((prop) => { // group sales by value ranges let pgd; if (prop == 'sales') { pgd = new wijmo.PropertyGroupDescription(prop, (item) => { if (item.sales > 8000) return '多い'; if (item.sales > 4000) return '普通'; return '少ない'; }); } else { // group others by value pgd = new wijmo.PropertyGroupDescription(prop); } view.groupDescriptions.push(pgd); }); } }); // // dump grouped data to console (no grid) document.querySelector('#btnDump').addEventListener('click', () => { if (!view.groups) { console.log('*** グループ化なし'); } else { console.log('*** ' + view.groups.length + '個のグループ:'); view.groups.forEach(group => dumpGroup(group, '')); } }); // function dumpGroup(group, level) { // show information for this group let propName = group.groupDescription['propertyName'], groupName = group.name, groupInfo = propName + ' > ' + groupName; // group summary // groupInfo += '(' + group.items.length + '個の項目)'; // item count groupInfo += ' 売上合計: ' + wijmo.Globalize.format(group.getAggregate(wijmo.Aggregate.Sum, 'sales'), 'c'); console.log(level + groupInfo); // // show subgroups if (group.groups) { group.groups.forEach(child => dumpGroup(child, level + ' ')); } } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity CollectionView Grouping</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"> <label> <input type="radio" name="filter" value="" checked="checked" /> グループ化なし </label> <label> <input type="radio" name="filter" value="country" /> 国でグループ化 </label> <label> <input type="radio" name="filter" value="country,product" /> 国、商品でグループ化 </label> <label> <input type="radio" name="filter" value="country,product,sales" /> 国、商品、売上でグループ化 </label> <p> <button id="btnDump" class="btn btn-default"> 結果をコンソールに表示 </button> </p> <div id="theGrid"> </div> </div> </body> </html> // export function getData() { let countries = ['アメリカ', 'ドイツ', 'イギリス', '日本', 'イタリア', 'ギリシャ'], products = ['ピアノ', 'バイオリン', 'フルート', 'ギター', 'チェロ'], data = []; // for (let i = 0; i < 1000; i++) { data.push({ id: i, country: countries[i % countries.length], product: products[i % products.length], sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // return data; } .wj-flexgrid { max-height: 250px; } label { width: 100% } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wijmo from '@grapecity/wijmo'; // import { Component, Inject, enableProdMode, NgModule } 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, TDataItem } from './app.data'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { private view: wijmo.CollectionView; // constructor(@Inject(DataService) private dataService: DataService) { this.view = new wijmo.CollectionView(dataService.getData(), { currentItem: null, sortDescriptions: [new wijmo.SortDescription('sales', false)] }); // // change the grouping document.addEventListener('change', e => { // remove the old groups this.view.groupDescriptions.clear(); // // add the new groups let filter = e.target as HTMLInputElement; if (filter.value) { filter.value.split(',').forEach((prop: string) => { // group sales by value ranges let pgd: wijmo.PropertyGroupDescription; if (prop == 'sales') { pgd = new wijmo.PropertyGroupDescription(prop, (item: TDataItem) => { if (item.sales > 8000) return '多い'; if (item.sales > 4000) return '普通'; return '少ない'; }); } else { // group others by value pgd = new wijmo.PropertyGroupDescription(prop); } this.view.groupDescriptions.push(pgd); }); } }); } // dumpData() { if (!this.view.groups) { console.log('*** グループ化なし'); } else { console.log('*** ' + this.view.groups.length + '個のグループ:'); this.view.groups.forEach(group => this.dumpGroup(group, '')); } } // private dumpGroup(group: wijmo.CollectionViewGroup, level: string) { // show information for this group let propName = group.groupDescription['propertyName'], groupName = group.name, groupInfo = propName + ' > ' + groupName; // group summary // groupInfo += '(' + group.items.length + '個の項目)'; // item count groupInfo += ' 売上合計: ' + wijmo.Globalize.format(group.getAggregate(wijmo.Aggregate.Sum, 'sales'), 'c2'); console.log(level + groupInfo); // // show subgroups if (group.groups) { group.groups.forEach(child => this.dumpGroup(child, level + ' ')); } } } // @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 CollectionView Grouping</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"> <label> <input type="radio" name="filter" value="" checked="true" /> グループ化なし </label> <label> <input type="radio" name="filter" value="country" /> 国でグループ化 </label> <label> <input type="radio" name="filter" value="country,product" /> 国、商品でグループ化 </label> <label> <input type="radio" name="filter" value="country,product,sales" /> 国、商品、売上でグループ化 </label> <p> <button class="btn btn-default" (click)="dumpData()"> 結果をコンソールに表示 </button> </p> <wj-flex-grid [itemsSource]="view" [alternatingRowStep]="0" [headersVisibility]="'Column'"> <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="product" 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> </div> import { Injectable } from '@angular/core'; // export type TDataItem = { id: number; country: string; product: string; sales: number; expenses: number; } // @Injectable() export class DataService { getData() { let countries = ['アメリカ', 'ドイツ', 'イギリス', '日本', 'イタリア', 'ギリシャ'], products = ['ピアノ', 'バイオリン', 'フルート', 'ギター', 'チェロ'], data: TDataItem[] = []; // for (let i = 0; i < 1000; i++) { data.push({ id: i, country: countries[i % countries.length], product: products[i % products.length], sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // return data; } } .wj-flexgrid { max-height: 250px; } label { width: 100% } 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 wjGrid from "@grapecity/wijmo.react.grid"; import { getData } from './data'; class App extends React.Component { constructor(props) { super(props); this.dumpData = () => { if (!this.state.view.groups) { console.log('*** グループ化なし'); } else { console.log('*** ' + this.state.view.groups.length + '個のグループ:'); this.state.view.groups.forEach((group) => this.dumpGroup(group, '')); } }; this.dumpGroup = (group, level) => { // show information for this group let propName = group.groupDescription['propertyName'], groupName = group.name, groupInfo = propName + ' > ' + groupName; // group summary // groupInfo += '(' + group.items.length + '個の項目)'; // item count groupInfo += ' 売上合計: ' + wijmo.Globalize.format(group.getAggregate(wijmo.Aggregate.Sum, 'sales'), 'c2'); console.log(level + groupInfo); // // show subgroups if (group.groups) { group.groups.forEach(child => this.dumpGroup(child, level + ' ')); } }; this.state = { view: new wijmo.CollectionView(getData(), { currentItem: null, sortDescriptions: [new wijmo.SortDescription('sales', false)] }) }; } render() { return <div className="container-fluid"> <label> <input type="radio" name="filter" value="" defaultChecked={true}/> グループ化なし </label> <label> <input type="radio" name="filter" value="country"/> 国でグループ化 </label> <label> <input type="radio" name="filter" value="country,product"/> 国、商品でグループ化 </label> <label> <input type="radio" name="filter" value="country,product,sales"/> 国、商品、売上でグループ化 </label> <p> <button className="btn btn-default" onClick={this.dumpData}> 結果をコンソールに表示 </button> </p> <wjGrid.FlexGrid itemsSource={this.state.view} showAlternatingRows={false} headersVisibility="Column"> <wjGrid.FlexGridColumn binding="id" header="ID" width={80}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="country" header="国"></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="product" header="商品"></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="sales" header="売上" width={80}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="expenses" header="費用" width={80}></wjGrid.FlexGridColumn> </wjGrid.FlexGrid> </div>; } componentDidMount() { // change the grouping document.addEventListener('change', (e) => { // remove the old groups this.state.view.groupDescriptions.clear(); // // add the new groups let filter = e.target; if (filter.value) { filter.value.split(',').forEach((prop) => { // group sales by value ranges let pgd; if (prop == 'sales') { pgd = new wijmo.PropertyGroupDescription(prop, (item) => { if (item.sales > 8000) return '多い'; if (item.sales > 4000) return '普通'; return '少ない'; }); } else { // group others by value pgd = new wijmo.PropertyGroupDescription(prop); } this.state.view.groupDescriptions.push(pgd); }); } }); } } 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 CollectionView Grouping</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: 250px; } label { width: 100% } export function getData() { let countries = ['アメリカ', 'ドイツ', 'イギリス', '日本', 'イタリア', 'ギリシャ'], products = ['ピアノ', 'バイオリン', 'フルート', 'ギター', 'チェロ'], data = []; // for (let i = 0; i < 1000; i++) { data.push({ id: i, country: countries[i % countries.length], product: products[i % products.length], sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // return data; } <template> <div class="container-fluid"> <label> <input type="radio" name="filter" value="" checked="true" /> グループ化なし </label> <label> <input type="radio" name="filter" value="country" /> 国でグループ化 </label> <label> <input type="radio" name="filter" value="country,product" /> 国、商品でグループ化 </label> <label> <input type="radio" name="filter" value="country,product,sales" /> 国、商品、売上でグループ化 </label> <p> <button class="btn btn-default" v-on:click="dumpData"> 結果をコンソールに表示 </button> </p> <wj-flex-grid :items-source="view" :show-alternating-rows=false headers-visibility="Column"> <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="product" 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> </div> </template> <script> import "bootstrap.css"; import "@grapecity/wijmo.styles/wijmo.css"; import Vue from 'vue'; import * as wijmo from '@grapecity/wijmo'; import { getData } from './data'; import { WjGridModule } from '@grapecity/wijmo.vue2.grid'; new Vue({ el: '#app', data:{ view: new wijmo.CollectionView(getData(), { currentItem: null, sortDescriptions: [new wijmo.SortDescription('sales', false)] }) }, methods:{ dumpData() { if (!this.view.groups) { console.log('*** グループ化なし'); } else { console.log('*** ' + this.view.groups.length + '個のグループ:'); this.view.groups.forEach(group => this.dumpGroup(group, '')); } }, dumpGroup(group, level) { // show information for this group let propName = group.groupDescription['propertyName'], groupName = group.name, groupInfo = propName + ' > ' + groupName; // group summary // groupInfo += '(' + group.items.length + '個の項目)'; // item count groupInfo += ' 売上合計: ' + wijmo.Globalize.format(group.getAggregate(wijmo.Aggregate.Sum, 'sales'), 'c2'); console.log(level + groupInfo); // // show subgroups if (group.groups) { group.groups.forEach(child => this.dumpGroup(child, level + ' ')); } } }, mounted: function(){ // change the grouping document.addEventListener('change', e => { // remove the old groups this.view.groupDescriptions.clear(); // // add the new groups let filter = e.target; if (filter.value) { filter.value.split(',').forEach((prop) => { // group sales by value ranges let pgd; if (prop == 'sales') { pgd = new wijmo.PropertyGroupDescription(prop, (item) => { if (item.sales > 8000) return '多い'; if (item.sales > 4000) return '普通'; return '少ない'; }); } else { // group others by value pgd = new wijmo.PropertyGroupDescription(prop); } this.view.groupDescriptions.push(pgd); }); } }); } }) </script> <style> .container-fluid .wj-flexgrid { max-height: 250px; } label { width: 100% } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity CollectionView Grouping</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> export function getData(callback, delay) { let countries = ['アメリカ', 'ドイツ', 'イギリス', '日本', 'イタリア', 'ギリシャ'], products = ['ピアノ', 'バイオリン', 'フルート', 'ギター', 'チェロ'], data = []; // for (let i = 0; i < 1000; i++) { data.push({ id: i, country: countries[i % countries.length], product: products[i % products.length], sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // return data; }