グリッド:CollectionViewの検証

FlexGridは、CollectionViewクラスと連携して、項目およびコレクションレベルの検証を行います。この機能を使用するには、検証対象のデータ項目と検証するプロパティを含む2つのパラメータを取る関数をgetErrorプロパティに設定し、エラー条件を説明する文字列(エラーがない場合はnull)を返します。下のグリッドは、項目の「売上」と「費用」プロパティが正の値を含むことを保証するgetError関数を実装するデータソースとして持っています。検証の仕組みを確認するには、いずれかの列に負の値を入力してみてください。

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import * as wjCore from '@grapecity/wijmo'; import * as wjGrid from '@grapecity/wijmo.grid'; import '@grapecity/wijmo.input'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // // create some random data var countries = 'アメリカ,ドイツ,イギリス,日本,スウェーデン,ノルウェー,デンマーク'.split(','); var data = []; for (var i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i], sales: Math.random() * 10000, expenses: Math.random() * 5000, overdue: (i + 1) % 4 == 0 }); } // // CollectionView with validation var view = new wjCore.CollectionView(data, { getError: function (item, prop) { if (prop == 'sales' && item.sales < 0) { return '正の値を入力してください。'; } if (prop == 'expenses' && item.expenses < 0) { return '正の値を入力してください。'; } } }); // // show the data in a grid var theGrid = new wjGrid.FlexGrid('#theGrid', { itemsSource: view, columns: [ { binding: 'id', header: 'ID', width: 50, isReadOnly: true }, { binding: 'country', header: '国', isRequired: true, dataMap: countries }, { binding: 'sales', header: '売上' }, { binding: 'expenses', header: '費用' }, { binding: 'overdue', header: '期限切れ' } ] }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid CollectionView Validation</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> .wj-flexgrid { max-height: 200px; margin-bottom: 12px; } 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 { WjGridModule } from '@grapecity/wijmo.angular2.grid'; import * as wjcCore from '@grapecity/wijmo'; import '@grapecity/wijmo.input'; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: wjcCore.CollectionView; countries = ['アメリカ', 'ドイツ', 'イギリス', '日本', 'スウェーデン', 'ノルウェー', 'デンマーク']; // DataSvc will be passed by derived classes constructor() { this.data = this._getData(); } private _getData() { // create some random data let data = []; for (let i = 0; i < this.countries.length; i++) { data.push({ id: i, country: this.countries[i], sales: Math.random() * 10000, expenses: Math.random() * 5000, overdue: i % 4 == 0 }); } let view = new wjcCore.CollectionView(data, { getError: (item: any, prop: any) => { if (prop == 'sales' && item.sales < 0) { return '正の値を入力してください。'; } if (prop == 'expenses' && item.expenses < 0) { return '正の値を入力してください。'; } } }); return view; } } @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 CollectionView Validation</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 [(itemsSource)]="data"> <wj-flex-grid-column [binding]="'id'" [header]="'ID'" [width]="50" [isReadOnly]="true"></wj-flex-grid-column> <wj-flex-grid-column [binding]="'country'" [header]="'国'" [isRequired]="true" [dataMap]="countries"></wj-flex-grid-column> <wj-flex-grid-column [binding]="'sales'" [header]="'売上'"></wj-flex-grid-column> <wj-flex-grid-column [binding]="'expenses'" [header]="'費用'"></wj-flex-grid-column> <wj-flex-grid-column [binding]="'overdue'" [header]="'期限切れ'"></wj-flex-grid-column> </wj-flex-grid> </div> .wj-flexgrid { max-height: 200px; margin-bottom: 12px; } 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'; class App extends React.Component { constructor(props) { super(props); this.countries = ['アメリカ', 'ドイツ', 'イギリス', '日本', 'スウェーデン', 'ノルウェー', 'デンマーク']; this.state = { data: this.getData() }; } render() { return <div className="container-fluid"> <wjGrid.FlexGrid itemsSource={this.state.data}> <wjGrid.FlexGridColumn binding="id" header="ID" width={50}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="country" header="国" isRequired={true}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="sales" header="売上"></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="expenses" header="費用"></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="overdue" header="期限切れ"></wjGrid.FlexGridColumn> </wjGrid.FlexGrid> </div>; } getData() { let data = []; for (let i = 0; i < this.countries.length; i++) { data.push({ id: i, country: this.countries[i], sales: Math.random() * 10000, expenses: Math.random() * 5000, overdue: i % 4 == 0 }); } let view = new wjcCore.CollectionView(data, { getError: (item, prop) => { if (prop == 'sales' && item.sales < 0) { return '正の値を入力してください。'; } if (prop == 'expenses' && item.expenses < 0) { return '正の値を入力してください。'; } } }); return view; } } 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 CollectionView Validation</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: 200px; margin-bottom: 12px; } body { margin-bottom: 24px; } <template> <div class="container-fluid"> <!-- the grid --> <wj-flex-grid :initialized="initializeGrid" :itemsSource="data"> <wj-flex-grid-column binding="id" header="ID" :width=50></wj-flex-grid-column> <wj-flex-grid-column binding="country" header="国" :isRequired=true :dataMap="countries"></wj-flex-grid-column> <wj-flex-grid-column binding="sales" header="売上"></wj-flex-grid-column> <wj-flex-grid-column binding="expenses" header="費用"></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.grid"; import '@grapecity/wijmo.input'; import * as wjcCore from '@grapecity/wijmo'; new Vue({ el: "#app", data: { data: null, countries: ['アメリカ', 'ドイツ', 'イギリス', '日本', 'スウェーデン', 'ノルウェー', 'デンマーク'] }, methods:{ initializeGrid(flex){ this.data = this._getData(); }, _getData() { // create some random data let data = []; for (let i = 0; i < this.countries.length; i++) { data.push({ id: i, country: this.countries[i], sales: Math.random() * 10000, expenses: Math.random() * 5000, overdue: i % 4 == 0 }); } let view = new wjcCore.CollectionView(data, { getError: (item, prop) => { if (prop == 'sales' && item.sales < 0) { return '正の値を入力してください。'; } if (prop == 'expenses' && item.expenses < 0) { return '正の値を入力してください。'; } } }); return view; } } }); </script> <style> .wj-flexgrid { max-height: 200px; margin-bottom: 12px; } 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/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>