剪切板

Clipboard 类允许您通过使用静态方法copy和paste修改其内容来自定义剪贴板的行为.

需要注意的是 Clipboard 类无法启动剪贴板操作; 在用户启动操作后,只能修改剪切板的内容.

此示例通过向剪贴板内容添加列标题来定制FlexGrid的copy操作。 要了解它是如何工作的,请从FlexGrid复制一个范围并粘贴到Excel工作表中.

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 some random data let data = getData(); // // show the data in a grid let hasHeaders = false; let theGrid = new grid.FlexGrid('#theGrid', { itemsSource: data, copying: (sender, args) => { hasHeaders = false; let includeHeaders = document.getElementById('includeHeaders').checked; if (includeHeaders) { let text = sender.getClipString(), sel = sender.selection, hdr = ''; // for (let c = sel.leftCol; c <= sel.rightCol; c++) { if (hdr) hdr += '\t'; hdr += sender.columns[c].header; } text = hdr + '\r\n' + text; // // put text with headers on the clipboard wijmo.Clipboard.copy(text); hasHeaders = true; // // prevent the grid from overwriting our clipboard content args.cancel = true; } }, pasting: (sender, args) => { if (hasHeaders) { args.cancel = true; } } }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Clipboard</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 class="form-check"> <label for="includeHeaders" class="form-check-label">Include Headers</label> <input id="includeHeaders" type="checkbox" checked="true" class="form-check-input"> </div> <div id="theGrid"> </div> </div> </body> </html> export function getData() { // create some random data let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], data = []; // for (let i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i], downloads: Math.round(Math.random() * 20000), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // return data; } .wj-flexgrid { max-height: 350px; } label { margin-right: 3px; } 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 { Component, Inject, enableProdMode, NgModule } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { DataService } from './app.data'; import { WjGridModule } from '@grapecity/wijmo.angular2.grid'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any[]; includeHeaders = true; private _hasHeaders = false; // constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(); } // copying(sender: grid.FlexGrid, args: grid.CellRangeEventArgs) { this._hasHeaders = false; if (this.includeHeaders) { let text = sender.getClipString(), sel = sender.selection, hdr = ''; // for (let c = sel.leftCol; c <= sel.rightCol; c++) { if (hdr) hdr += '\t'; hdr += sender.columns[c].header; } text = hdr + '\r\n' + text; // // put text with headers on the clipboard wijmo.Clipboard.copy(text); this._hasHeaders = true; // // prevent the grid from overwriting our clipboard content args.cancel = true; } } // pasting(sender: grid.FlexGrid, args: grid.CellRangeEventArgs) { if (this._hasHeaders) { args.cancel = true; } } } // @NgModule({ imports: [WjGridModule, BrowserModule, FormsModule], 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 Clipboard</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 class="form-check"> <label for="includeHeaders" class="form-check-label">Include Headers</label> <input id="includeHeaders" type="checkbox" class="form-check-input" [(ngModel)]="includeHeaders"> </div> <wj-flex-grid #flexGrid [itemsSource]="data" (copying)="copying(flexGrid, $event)" (pasting)="pasting(flexGrid, $event)"> </wj-flex-grid> </div> import { Injectable } from '@angular/core'; // @Injectable() export class DataService { getData() { // create some random data let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], data = []; // for (let i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i], downloads: Math.round(Math.random() * 20000), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // return data; } } .wj-flexgrid { max-height: 350px; } label { margin-right: 3px; } <template> <div class="container-fluid"> <div class="form-check"> <label for="includeHeaders" class="form-check-label">Include Headers</label> <input id="includeHeaders" type="checkbox" class="form-check-input" v-model="includeHeaders" > </div> <wj-flex-grid ref="flexGrid" :itemsSource="data" :copying="copying" :pasting="pasting"></wj-flex-grid> </div> </template> <script> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import Vue from 'vue'; import '@grapecity/wijmo.vue2.grid'; import * as wijmo from '@grapecity/wijmo'; import { getData } from './data'; let App = Vue.extend({ name: 'app', data: function() { return { data: getData(), includeHeaders: true, _hasHeaders: false }; }, methods: { copying: function(sender, args) { this._hasHeaders = false; if (this.includeHeaders) { let text = sender.getClipString(), sel = sender.selection, hdr = ''; // for (let c = sel.leftCol; c <= sel.rightCol; c++) { if (hdr) hdr += '\t'; hdr += sender.columns[c].header; } text = hdr + '\r\n' + text; // // put text with headers on the clipboard wijmo.Clipboard.copy(text); this._hasHeaders = true; // // prevent the grid from overwriting our clipboard content args.cancel = true; } }, // pasting: function(sender, args) { if (this._hasHeaders) { args.cancel = true; } } } }); new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> .container-fluid .wj-flexgrid { max-height: 350px; } label { margin-right: 3px; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Clipboard</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() { // create some random data let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], data = []; // for (let i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i], downloads: Math.round(Math.random() * 20000), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // 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 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.copying = (sender, args) => { this.setState({ hasHeaders: false }); if (this.state.includeHeaders) { let text = sender.getClipString(), sel = sender.selection, hdr = ''; // for (let c = sel.leftCol; c <= sel.rightCol; c++) { if (hdr) hdr += '\t'; hdr += sender.columns[c].header; } text = hdr + '\r\n' + text; // // put text with headers on the clipboard wijmo.Clipboard.copy(text); this.setState({ hasHeaders: true }); // // prevent the grid from overwriting our clipboard content args.cancel = true; } }; this.pasting = (sender, args) => { if (this.state.hasHeaders) { args.cancel = true; } }; this.updateIncludeHeadersStatus = (target) => { this.setState({ includeHeaders: target.checked }); }; this.state = { data: getData(), includeHeaders: true, hasHeaders: false }; } ; render() { return <div className="container-fluid"> <div className="form-check"> <label htmlFor="includeHeaders" className="form-check-label">Include Headers</label> <input id="includeHeaders" type="checkbox" className="form-check-input" defaultChecked={true} onChange={e => this.updateIncludeHeadersStatus(e.target)}/> </div> <wjGrid.FlexGrid itemsSource={this.state.data} copying={this.copying} pasting={this.pasting}> </wjGrid.FlexGrid> </div>; } } 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>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'); </script> </head> <body> <div id="app"></div> </body> </html> .wj-flexgrid { max-height: 350px; } label { margin-right: 3px; } export function getData() { // create some random data let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], data = []; // for (let i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i], downloads: Math.round(Math.random() * 20000), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // return data; }