编辑事件

FlexGrid有几个与编辑相关的事件.

此示例显示如何使用其中两个:

  • beginningEdit: 通过将 cancel 参数设置为true来防止编辑逾期的item.
  • 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'; // 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 }); } // // show the data in a grid 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' } ], beginningEdit: function (s, e) { var col = s.columns[e.col]; if (col.binding != 'overdue') { var item = s.rows[e.row].dataItem; if (item.overdue) { // prevent editing overdue items e.cancel = true; showMessage('Overdue items cannot be edited'); } } }, cellEditEnding: function (s, e) { showMessage('&nbsp;'); 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) { // prevent negative sales/expenses e.cancel = true; showMessage('Please enter a positive amount'); } } } }); // // show log message var elLog = document.getElementById('log'); function showMessage(msg) { elLog.innerHTML = msg; } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid Editing Events</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="log"> &nbsp; </div> <div id="theGrid"> </div> </div> </body> </html> .wj-flexgrid { max-height: 200px; margin-bottom: 12px; } #log { font-style: italic; text-align: center; margin-bottom: 10px; opacity: .5; } 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 * as wjcCore from '@grapecity/wijmo'; import * as wjcGrid from '@grapecity/wijmo.grid'; import { WjGridModule } from '@grapecity/wijmo.angular2.grid'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any[]; logText: string = 'please select a range on the grid'; countries = ['US', 'Germany', 'UK', 'Japan', 'Sweden', 'Norway', 'Denmark']; constructor() { this.data = this._getData(); } flexInitialized(flexgrid: wjcGrid.FlexGrid) { this.logText = ''; flexgrid.beginningEdit.addHandler((s: wjcGrid.FlexGrid, e: wjcGrid.CellRangeEventArgs) => { let col = s.columns[e.col]; if (col.binding != 'overdue') { let item = s.rows[e.row].dataItem; if (item.overdue) { // prevent editing overdue items e.cancel = true; this.logText = 'Overdue items cannot be edited'; } } }); flexgrid.cellEditEnding.addHandler((s: wjcGrid.FlexGrid, e: wjcGrid.CellEditEndingEventArgs) => { this.logText = ''; 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) { // prevent negative sales/expenses e.cancel = true; this.logText = '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 + 1) % 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 Editing Events</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"> <div [innerHTML]="logText" class="log"> </div> <!-- the grid --> <wj-flex-grid #flex [(itemsSource)]="data" (initialized)="flexInitialized(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; } .log { font-style: italic; text-align: center; margin-bottom: 10px; opacity: .5; } body { margin-bottom: 24px; } <template> <div class="container-fluid"> <div v-html="logText" class="log"></div> <!-- the grid --> <wj-flex-grid :itemsSource="data" :initialized="flexInitialized"> <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 '@grapecity/wijmo.vue2.core'; import '@grapecity/wijmo.vue2.grid'; import { getData } from "./data"; let App = Vue.extend({ name: "app", data: function() { return { data: getData(), logText: "please select a range on the grid", countries: [ "US", "Germany", "UK", "Japan", "Sweden", "Norway", "Denmark" ] }; }, methods: { flexInitialized: function(flexgrid) { this.logText = ""; flexgrid.beginningEdit.addHandler((s, e) => { let col = s.columns[e.col]; if (col.binding != "overdue") { let item = s.rows[e.row].dataItem; if (item.overdue) { // prevent editing overdue items e.cancel = true; this.logText = "Overdue items cannot be edited"; } } }); flexgrid.cellEditEnding.addHandler((s, e) => { this.logText = ""; let col = s.columns[e.col]; if (col.binding == "sales" || col.binding == "expenses") { let value = wijmo.changeType( s.activeEditor.value, wijmo.DataType.Number, col.format ); if (!wijmo.isNumber(value) || value < 0) { // prevent negative sales/expenses e.cancel = true; this.logText = "Please enter a positive amount"; } } }); } } }); new Vue({ render: h => h(App) }).$mount("#app"); </script> <style> .wj-flexgrid { max-height: 200px; margin-bottom: 12px; } .container-fluid .log { font-style: italic; text-align: center; margin-bottom: 10px; opacity: 0.5; } 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/jszip/dist/jszip.js"></script> <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() { // create some random data let countries = ['US', 'Germany', 'UK', 'Japan', 'Sweden', 'Norway', 'Denmark']; let data = []; for (let 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 }); } return data; } 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'; import { getData } from "./data"; class App extends React.Component { constructor(props) { super(props); this.state = { data: getData(), logText: "please select a range on the grid" }; } render() { return <div className="container-fluid"> <div className="log">{this.state.logText}</div> <wjGrid.FlexGrid initialized={this.flexInitialized.bind(this)} itemsSource={this.state.data}> <wjGrid.FlexGridColumn binding="id" header="ID" width={50} isReadOnly={true}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="country" header="Country" dataMap="countries" isRequired={true}></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>; } flexInitialized(flexgrid) { this.setState({ logText: "" }); flexgrid.beginningEdit.addHandler((s, e) => { let col = s.columns[e.col]; if (col.binding != "overdue") { let item = s.rows[e.row].dataItem; if (item.overdue) { // prevent editing overdue items e.cancel = true; this.setState({ logText: "Overdue items cannot be edited" }); } } }); flexgrid.cellEditEnding.addHandler((s, e) => { this.setState({ logTexty: "" }); 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) { // prevent negative sales/expenses e.cancel = true; this.setState({ logText: "Please enter a positive amount" }); } } }); } } 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 Drag and Drop Events</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; } .container-fluid .log { font-style: italic; text-align: center; margin-bottom: 10px; opacity: 0.5; } body { margin-bottom: 24px; } export function getData() { // create some random data let countries = ['US', 'Germany', 'UK', 'Japan', 'Sweden', 'Norway', 'Denmark']; let data = []; for (let 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 }); } return data; }