单元格级别绑定

SpreadJS 支持单元格级别绑定,你可以将数据源的数据绑定到单元格上。

你必须创建一个单元格级别绑定的数据源, 然后使用 setBindingPath 方法来设置绑定路径到指定的单元格,并将数据源设置到表单上。例如: 你可以使用 getBindingPath 方法来获取指定单元格的绑定信息。
import { Component, NgModule, enableProdMode } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import '@grapecity-software/spread-sheets-resources-zh'; GC.Spread.Common.CultureManager.culture("zh-cn"); import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { SpreadSheetsModule } from '@grapecity-software/spread-sheets-angular'; import GC from '@grapecity-software/spread-sheets'; import './styles.css'; let person = { name: "Wang Feng", age: 25, sex: "Male", address: { postcode: "710075" } }, sheet: GC.Spread.Sheets.Worksheet; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { spread: GC.Spread.Sheets.Workbook; name = 'Wang Feng'; age = 25; sex = 'Male'; path = ""; postcode = '710075'; hostStyle = { width: 'calc(100% - 280px)', height: '100%', overflow: 'hidden', float: 'left' }; updateName(value: any) { if (person && sheet) { person.name = value.target.value; sheet.repaint(); } }; updateAge(value: any) { if (person && sheet) { person.age = parseInt(value.target.value); sheet.repaint(); } }; updateSex(value: any) { if (person && sheet) { person.sex = value.target.value; sheet.repaint(); } }; updatePostcode(value: any) { if (person && sheet) { person.address.postcode = value.target.value; sheet.repaint(); } }; initSpread($event: any) { this.spread = $event.spread; let spread = this.spread; let spreadNS = GC.Spread.Sheets; let self = this; sheet = spread.sheets[0]; sheet.suspendPaint(); sheet.setValue(0, 0, 'cell-binding'); sheet.setValue(2, 1, 'Name'); sheet.setValue(3, 1, 'Age'); sheet.setValue(4, 1, 'Sex'); sheet.setValue(5, 1, 'Address.postcode'); let source = new spreadNS.Bindings.CellBindingSource(person); sheet.setBindingPath(2, 2, "name"); sheet.setBindingPath(3, 2, "age"); sheet.setBindingPath(4, 2, "sex"); sheet.setBindingPath(5, 2, "address.postcode"); sheet.setDataSource(source); sheet.setSelection(2, 2, 1, 1); let path = sheet.getBindingPath(2, 2); sheet.getRange(2, 2, 4, 1).backColor("rgb(208,206,206)"); this.path = path || ""; sheet.bind(spreadNS.Events.SelectionChanged, function() { let activeCell = sheet.getSelections()[0]; let path = sheet.getBindingPath(activeCell.row, activeCell.col); self.path = path || ""; }); sheet.bind(spreadNS.Events.CellChanged, function() { self.age = person.age || 0; self.name = person.name || ""; self.postcode = person.address.postcode || ""; self.sex = person.sex || ""; }); sheet.resumePaint(); } } @NgModule({ imports: [BrowserModule, SpreadSheetsModule], declarations: [AppComponent], exports: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule);
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/angular/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <!-- Polyfills --> <script src="$DEMOROOT$/zh/angular/node_modules/core-js/client/shim.min.js"></script> <script src="$DEMOROOT$/zh/angular/node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="$DEMOROOT$/zh/angular/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('$DEMOROOT$/zh/lib/angular/license.ts'); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html>
<div class="sample-tutorial"> <gc-spread-sheets [hostStyle]="hostStyle" (workbookInitialized)="initSpread($event)"> <gc-worksheet> <gc-column width=120></gc-column> <gc-column width=120></gc-column> <gc-column width=120></gc-column> </gc-worksheet> </gc-spread-sheets> <div class="options-container"> <div class="option-row"> <label style="background-color:#F4F8EB;">Select one of the gray cells to get its binding. You can also change the Person object below to see it change in the Spread instance: change any of the text in the text boxes below.</label> </div> <div class="option-row"> <label>Name:&nbsp;&nbsp;</label> <input type="text" (keyup)="updateName($event)" [value]="name"/> <label>Age:&nbsp;&nbsp;</label> <input type="text" (keyup)="updateAge($event)" [value]="age"/> <label>Sex:&nbsp;&nbsp;</label> <input type="text" (keyup)="updateSex($event)" [value]="sex"/> <label>Postcode:&nbsp;&nbsp;</label> <input type="text" (keyup)="updatePostcode($event)" [value]="postcode"/> <label>GetBindingPath:&nbsp;&nbsp;</label> <label style="color:darkgreen">{{path}}</label> </div> </div> </div>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 280px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; padding: 5px; margin-top: 10px; } label { display: block; margin-bottom: 6px; } input { padding: 4px 6px; } input[type=button] { margin-top: 6px; display: block; } input[type=text] { margin-top: 6px; margin-bottom: 6px; display: block; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }
(function(global) { System.config({ transpiler: 'ts', typescriptOptions: { tsconfig: true }, meta: { 'typescript': { "exports": "ts" }, '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { 'core-js': 'npm:core-js/client/shim.min.js', 'zone': 'npm:zone.js/dist/zone.min.js', 'rxjs': 'npm:rxjs/bundles/rxjs.umd.min.js', '@angular/core': 'npm:@angular/core/bundles/core.umd.min.js', '@angular/common': 'npm:@angular/common/bundles/common.umd.min.js', '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.min.js', '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.min.js', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.min.js', '@angular/http': 'npm:@angular/http/bundles/http.umd.min.js', '@angular/common/http': 'npm:@angular/common/bundles/common-http.umd.min.js', '@angular/router': 'npm:@angular/router/bundles/router.umd.min.js', '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.min.js', 'jszip': 'npm:jszip/dist/jszip.min.js', 'typescript': 'npm:typescript/lib/typescript.js', 'ts': 'npm:plugin-typescript/lib/plugin.js', 'css': 'npm:systemjs-plugin-css/css.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build': 'npm:systemjs-plugin-babel/systemjs-babel-browser.js', '@grapecity-software/spread-sheets': 'npm:@grapecity-software/spread-sheets/index.js', '@grapecity-software/spread-sheets-resources-zh': 'npm:@grapecity-software/spread-sheets-resources-zh/index.js', '@grapecity-software/spread-sheets-angular': 'npm:@grapecity-software/spread-sheets-angular/bundles/grapecity-software-spread-sheets-angular.umd.js', '@grapecity-software/jsob-test-dependency-package/react-components': 'npm:@grapecity-software/jsob-test-dependency-package/react-components/index.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'ts' }, rxjs: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' }, } }); })(this);