校验

FlexGrid通过以下途径进行校验:

  1. Automatic Type Coercion: 当提交单元格编辑时,表格自动强制值以匹配列的数据类型。 它还根据列的 isRequired 属性检查空值和空值。 如果数据无效,则恢复原始单元格值.
  2. Data Maps: 如果您的表格包含只应允许某些输入值的列,请在列中添加 DataMap,以确保用户无法在单元格中输入无效内容.
  3. Event-Based Validation: 表格引发一个 cellEditEnding 事件,允许您检查编辑器的当前值,如果新值无效则取消编辑.
  4. CollectionView-Based Validation: CollectionView-Based Validation: 表格使用收集视图的 getError 方法来突出显示无效单元格,并阻止用户输入无效信息. 下面的表格演示了三个第一项。 它有一个包含数据映射的国家列,可以防止用户输入无效的国家/地区。 cellEditEnding 事件用于确保“销售”和“费用”列包含正值:
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 '@grapecity/wijmo.input'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // // create some random data var countries = 'US,Germany,UK,Japan,Sweden,Norway,Denmark'.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-based validation var theGrid = new wjGrid.FlexGrid('#theGrid', { itemsSource: data, columns: [ { binding: 'id', header: 'ID', width: 50, isReadOnly: true }, { binding: 'country', header: 'Country', isRequired: true, dataMap: countries }, { binding: 'sales', header: 'Sales', format: 'n2' }, { binding: 'expenses', header: 'Expenses', format: 'n2' }, { binding: 'overdue', header: 'Overdue' } ], cellEditEnding: function (s, e) { var col = s.columns[e.col]; if (col.binding == 'sales' || col.binding == 'expenses') { var value = wjCore.changeType(s.activeEditor.value, wjCore.DataType.Number, col.format); if (!wjCore.isNumber(value) || value < 0) { e.cancel = true; alert('Please enter a positive amount.'); } } } }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid 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 * as wjcGrid from '@grapecity/wijmo.grid'; import '@grapecity/wijmo.input'; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any[]; countries = ['US', 'Germany', 'UK' ,'Japan', 'Italy', 'Greece']; // DataSvc will be passed by derived classes constructor() { this.data = this._getData(); } initializeGrid(flex: wjcGrid.FlexGrid) { flex.cellEditEnding.addHandler((s: wjcGrid.FlexGrid, e: wjcGrid.CellEditEndingEventArgs) => { let col = s.columns[e.col]; if (col.binding == 'sales' || col.binding == 'expenses') { let value = wjcCore.changeType(s.activeEditor.value, wjcCore.DataType.Number, col.format); if (!wjcCore.isNumber(value) || value < 0) { e.cancel = true; alert('Please enter a positive amount.') } } }); } 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 }); } 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 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" (initialized)="initializeGrid(flex)"> <wj-flex-grid-column [binding]="'id'" [header]="'ID'" [width]="50" [isReadOnly]="true"></wj-flex-grid-column> <wj-flex-grid-column [binding]="'country'" [header]="'Country'" [isRequired]="true" [dataMap]="countries"></wj-flex-grid-column> <wj-flex-grid-column [binding]="'sales'" [header]="'Sales'" [format]="'n2'"></wj-flex-grid-column> <wj-flex-grid-column [binding]="'expenses'" [header]="'Expenses'" [format]="'n2'"></wj-flex-grid-column> <wj-flex-grid-column [binding]="'overdue'" [header]="'Overdue'"></wj-flex-grid-column> </wj-flex-grid> </div> .wj-flexgrid { max-height: 200px; margin-bottom: 12px; } body { margin-bottom: 24px; } <template> <div class="container-fluid"> <!-- the grid --> <wj-flex-grid :itemsSource="data" :initialized="initializeGrid"> <wj-flex-grid-column binding="id" header="ID" :width=50 :isReadOnly=true></wj-flex-grid-column> <wj-flex-grid-column binding="country" header="Country" :isRequired="true" :dataMap="countries"></wj-flex-grid-column> <wj-flex-grid-column binding="sales" header="Sales" format="n2"></wj-flex-grid-column> <wj-flex-grid-column binding="expenses" header="Expenses" format="n2"></wj-flex-grid-column> <wj-flex-grid-column binding="overdue" header="Overdue"></wj-flex-grid-column> </wj-flex-grid> </div> </template> <script> import "@grapecity/wijmo.styles/wijmo.css"; import "bootstrap.css"; import Vue from "vue"; import { WjGridModule } from '@grapecity/wijmo.vue2.grid'; import * as wjcCore from '@grapecity/wijmo'; import * as wjcGrid from '@grapecity/wijmo.grid'; import '@grapecity/wijmo.input'; new Vue({ el: "#app", data: { data: [], countries : ['US', 'Germany', 'UK' ,'Japan', 'Italy', 'Greece'] }, methods:{ initializeGrid(flex){ this.data = this._getData(); flex.cellEditEnding.addHandler((s, e) => { let col = s.columns[e.col]; if (col.binding == 'sales' || col.binding == 'expenses') { let value = wjcCore.changeType(s.activeEditor.value, wjcCore.DataType.Number, col.format); if (!wjcCore.isNumber(value) || value < 0) { e.cancel = true; alert('Please enter a positive amount.') } } }); }, _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 }); } return data; } } }); </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> 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 = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece']; this.state = { data: this.getData() }; } render() { return <div className="container-fluid"> <wjGrid.FlexGrid initialized={this.initializeGrid.bind(this)} itemsSource={this.state.data}> <wjGrid.FlexGridColumn binding="id" header="ID" width={50} isReadOnly={true}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="country" header="Country" isRequired={true} dataMap="countries"></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="sales" header="Sales" format="n2"></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="expenses" header="Expenses" format="n2"></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="overdue" header="Overdue"></wjGrid.FlexGridColumn> </wjGrid.FlexGrid> </div>; } initializeGrid(flex) { flex.cellEditEnding.addHandler((s, e) => { let col = s.columns[e.col]; if (col.binding == 'sales' || col.binding == 'expenses') { let value = wjcCore.changeType(s.activeEditor.value, wjcCore.DataType.Number, col.format); if (!wjcCore.isNumber(value) || value < 0) { e.cancel = true; alert('Please enter a positive amount.'); } } }); } 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 }); } return data; } } 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 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; }